Click here to Skip to main content
15,895,786 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
I have a grid view and an update button outside the grid view. When the user make some changes to the records inside the grid view,the records in the database will also be updated.
Here's my code:
C#
protected void Button2_Click(object sender, EventArgs e)
   {
       string connectionString = "Server=192.168.100.252;Database=AMICASSA_Checklist;Trusted_Connection=true";

       SqlConnection myConnection = new SqlConnection(connectionString);
       myConnection.Open();
       SqlCommand cmd = new SqlCommand("UPDATE Sample ", myConnection);
       SqlDataReader red = cmd.ExecuteReader();
       red.Close();
       myConnection.Close();
       BindData();

   }


Can Anyone help me?
Posted
Updated 3-Aug-14 21:20pm
v2
Comments
SRK90 4-Aug-14 3:08am    
I dont see you are passing any value to your database. if i am right, then your database procedure should have parameters. So you have to pass those parameters from here.
DarkDreamer08 4-Aug-14 3:13am    
i do not put parameters inside my query because there is no specific row that the user will change the record.the user can change what ever they like in any rows. That is why my query is like that. Is that possible to update the database after changing all the records and clicking the update button outside the grid view? Is there any way to do that?
SRK90 4-Aug-14 3:17am    
else put all the changes in session while the user do the changes in grid view and get the values from session after the button click and then clear the session.
u mani 4-Aug-14 3:13am    
if u want to update every record then write the update query in loop .. then it will update every record . .
DarkDreamer08 4-Aug-14 3:22am    
what do you mean by that? could you show me a sample- a bit of codes? I mean not totally all the codes. Let me do that. so that I can get what you want me to do :) Thanks

Please, read my comment to the question.

Have a look here: Updating Data Sources with DataAdapters[^]
If you would like to update data using sql statement, you should use UPDATE[^] statement.
 
Share this answer
 
SqlCommand cmd = new SqlCommand("UPDATE Sample ", myConnection);

Your command text is incomplete. What exactly you need to update? You need to use the command text something like this


SQL
SqlConnection myConnection = new SqlConnection(connectionString);
        myConnection.Open();
        SqlCommand cmd = new SqlCommand("UPDATE Sample set    
                                samplecode="+yourvalue , myConnection);

        cmd.CommandType = CommandType.Text;
 
Share this answer
 
v2
Bulk Update using Sql Merge will be useful for you
Try with it
upsert to sql[^]
Merge in Sql[^]
bulk upsert to sql server[^]
 
Share this answer
 

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