Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am getting error while passing TVP to a stored procedure
My code is
SqlConnection con=new SqlConnection();
con.Open();
SqlCommand cmd=new SqlCommand("SPName",con);
DataTable dataTable = new DataTable("vpTableGuid");
dataTable.Columns.Add("id1", typeof(Guid));
dataTable.Columns.Add("rowNum", typeof(Int32));
dataTable.Rows.Add(Guid.NewGuid(),1);
dataTable.Rows.Add(Guid.NewGuid(),2);
dataTable.Rows.Add(Guid.NewGuid(),3);
SqlParameter param = new SqlParameter("#evaluatorList", dataTable);
param.SqlDbType = SqlDbType.Structured;
param.TypeName = "dbo.vpTableGuid";
command.Parameters.Add(param);
command.ExecuteNonQuery();
The above code throws an error:
INSERT into an identity column not allowed on table variables.
The data for table-valued parameter "#evaluatorList" doesn't conform to the table type of the parameter.
You cannot set the identity column manually in SQL.
Define your primary key column in SQL as such:
id1 UNIQUEIDENTIFIER DEFAULT NEWID() PRIMARY KEY
Remove this line of code:
dataTable.Columns.Add("id1", typeof(Guid));
And change your vpTableGuid definition to not include the id1 column.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I tried to write a code which will count rows in MySQL , same as combobox-selected text.
For example in my MySQL database,i have a table(ogrencikayit) and in this table i have several columns.In my combobox there are 2 different selection which allows to select student class.When user select the class in combobox , label has to show count of total student in selected class.Here is my code;
DB database = new DB();
int kayitsayisi = -1;
MySqlCommand cmd = new MySqlCommand("Select count(*) from ogrencikayit Where ogrsinif ="+comboBox3.Text.ToUpper()+"" , database.baglanti);
database.baglanti.Open();
kayitsayisi = Convert.ToInt32(cmd.ExecuteScalar());
string kayitt = kayitsayisi.ToString();
label24.Text = kayitt;
Shortly; i try to find a code that it will read the name of the class name from combobox than it will search in database that how many student belongs to that class and it will show it to the label.
It says Invalid column "class name" in where clause.
that's cause you are missing single quote around the value and thus it's taking it as a column name. it should be like below
Where ogrsinif ='"+comboBox3.Text.ToUpper()+"'"
Again, always use parameterized query instead of concatenating your user input.
MySqlCommand cmd = new MySqlCommand("Select count(*) from ogrencikayit Where ogrsinif = #ogrsinif" , database.baglanti);
cmd.Parameters.Add("#ogrsinif", SqlDbType.VarChar).Value=comboBox3.Text;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a database called "users" where user information is saved. When you login with your name and pass, your name is send to the homeform, where i want to get the "to-do list" that's saved in the database with your info. Here's my code so far:
db_connection();
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "SELECT * FROM `user` WHERE `username`=#username AND `todo`=#todo";
So basically i need to get the todo of the user (which is sent to this form as _name) lets say admin, and display it in tbTodo.
if it is in the same form you can try using
select `user`.todo from `user` where `username` = #username
considering username is unique
If the main problem is parameters - u can use property Parameters:
cmd.Parameters.AddWithValue("#username", user);
There is an example in msdn:
SqlCommand command = new SqlCommand(commandText, connection);
command.Parameters.Add("#ID", SqlDbType.Int);
command.Parameters["#ID"].Value = customerID;
// Use AddWithValue to assign Demographics.
// SQL Server will implicitly convert strings into XML.
command.Parameters.AddWithValue("#demographics", demoXml);
try
{
connection.Open();
Int32 rowsAffected = command.ExecuteNonQuery();
Console.WriteLine("RowsAffected: {0}", rowsAffected);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I still new to C# and just have 3 months plus of learning process. I would like to seeking advice on how to extract data value from my DataTable I have create for validation checking.
private void checker
{
string sqlSelect2 = "SELECT a.AccNo, a.CompanyName , a.CreditLimit, FROM Debtor a JOIN";
}
which i have named TableCehecker, i do not need to put it to gridview, just for checking purposes.
In the private void process how can I extract dataTable TableCehecker and the value?
Thank you,
Brian
You need to follow the Docs from MSDN:
SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
cmd.CommandText = "SELECT * FROM Customers";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
reader = cmd.ExecuteReader();
// Data is accessible through the DataReader object here.
DataTable dt = new DataTable();
dt.Load(reader);
sqlConnection1.Close();
You can then check whatever you want of data inside the DataTable either by querying or by looping through the rows and checking the column values.
Based on your comments, to extract values from single row:
DataRow drow = dt.Rows[0];
string value = drow.Field<string>("CompanyName");
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
SqlConnection con = new SqlConnection("Data Source=ALIZEE_TROTT\\SQLEXPRESS;Initial Catalog=UrduStemmer;Persist Security Info=false; User ID=sa;Password=password");
SqlDataAdapter sda = new SqlDataAdapter("select * from stop_word_list where word_list='کو' ", con);
DataTable dt = new System.Data.DataTable();
sda.Fill(dt);
if (dt.Rows.Count == 1)
{
MessageBox.Show("Ok");
}
else
{
MessageBox.Show("not ok");
}
First make sure that your column for word_list is one of following type
nchar
nvarchar
ntext
You must precede all Unicode strings with a prefix N when you deal with Unicode string constants in SQL Server
SELECT * FROM stop_word_list WHERE word_list = N'کو'
Hope that helps.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
This method below goes into a table and deletes all of the table. But in the datatable "table" below in the first column there are dates which i would like to be deleted from the database. how would i go about doing this
Datatable table = new DataTable()
string sqlConnectionString =
"Server = 100.720.8.196; Database = testDat; User Id = sa; Password = ";
// Copy the DataTable to SQL Server
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
{
string deleteSting =
"delete from dbo.testTable where clientId=1212";
conn.Open();
using (SqlCommand cmd = new SqlCommand(deleteSting, conn))
{
cmd.ExecuteNonQuery();
}
conn.Close();
}
I'm just guessing here, you probably mean to make the date column empty. If that's the case then you use UPDATE, like:
string updateString =
"UPDATE dbo.testTable SET datecol = #datecol where clientId=#clientID";
conn.Open();
using (SqlCommand cmd = new SqlCommand(updateString, conn))
{
cmd.Parameters.Add("#datecol", SqlDbType.Date).Value = DbNull.Value;
cmd.Parameters.Add("#clientID", SqlDbType.Int).Value = 1212;
cmd.ExecuteNonQuery();
}
conn.Close();
Well, there are a few options. You could loop through your table and create a string containing your dates in a comma separated list. With that, you can create a SQL Statement like delete from table where dateCol in (date1, date2, date3, etc...)
Or you could just loop through your data table, and do one delete per loop iteration. Either way will work.
However you decide to proceed, make sure to use parameters instead of string concatenation to prevent issues like SQL Injections:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx