Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to update my database using windows application i have already created connection string,
i want only update selected rows

My update codes
// da is as data adapter

SQL
da.UpdateCommand = new SqlCommand("UPDATE complain SET Name = @NAME,Email = @EMAIL,Message= @MESSAGE", con);
           da.UpdateCommand.Parameters.Add("@NAME", SqlDbType.NVarChar).Value = nameTextBox.Text;
           da.UpdateCommand.Parameters.Add("@EMAIL", SqlDbType.NVarChar).Value = emailTextBox.Text;
           da.UpdateCommand.Parameters.Add("@MESSAGE", SqlDbType.NVarChar).Value = messageTextBox.Text;

           con.Open();
           da.UpdateCommand.ExecuteNonQuery();
           con.Close();


it does update but it effect to whole database so help me please correct me if I'm wrong
Thank you
Posted
Updated 11-Aug-21 4:24am

You have not provided any Where clause in your query.
If you don't provide any Where clause, it will update whole table.
 
Share this answer
 
Comments
[no name] 8-Sep-11 8:47am    
Good :)
your update statement will update all records...
you also need to add a where clause... something like..

"UPDATE complain SET Name = @NAME,Email = @EMAIL,Message= @MESSAGE WHERE primary_key= ????"
 
Share this answer
 
Comments
[no name] 8-Sep-11 8:47am    
Good catch
Member 11548228 26-Mar-15 4:43am    
Line 32: TextBox1.Text = query;
Line 33: SqlCommand cmd = new SqlCommand(query, con);
Line 34: int row=cmd.ExecuteNonQuery();// this place Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '='.
Line 35: con.Close();
Line 36: }
HI,
you just missed WHERE clause in your update statement.
and my another suggestion is that use stored procedure rather than direct sql query on class file.
 
Share this answer
 
Comments
nagendrathecoder 8-Sep-11 8:51am    
:)
First thing you have not provide Where clause in update command thats why its updating whole table.

Why are you using DataAdapter here.
You can simply use SqlCommand like this:
SQL
SqlCommand UpdateCommand = new SqlCommand("UPDATE complain SET Name = @NAME,Email = @EMAIL,Message= @MESSAGE", con);
           UpdateCommand.Parameters.Add("@NAME", SqlDbType.NVarChar).Value = nameTextBox.Text;
           UpdateCommand.Parameters.Add("@EMAIL", SqlDbType.NVarChar).Value = emailTextBox.Text;
           UpdateCommand.Parameters.Add("@MESSAGE", SqlDbType.NVarChar).Value = messageTextBox.Text;

           con.Open();
           UpdateCommand.ExecuteNonQuery();
           con.Close();
 
Share this answer
 
v2
Comments
[no name] 8-Sep-11 8:46am    
first read the question check the specification what he/she did and then answer
Suresh Suthar 8-Sep-11 8:49am    
Thanks for your comment. Next time I will take care.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900