Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
0
down vote
favorite

I have a TextBox with id=txtPlace and a gridview. If a user selects a row an put some Information in the TextBox and click on save. The Information should replace older records in the database(SQL)


But It does not work. When I click on save, nothing happens.


What I have tried:

C#
protected void btnSave_Click(object sender, EventArgs e)
   {
       var id = GridView2.SelectedDataKey.Values[0].ToString();
       string constr = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
       using (SqlConnection conn = new SqlConnection(constr))
       {

           string query = "UPDATE user SET place= @place where ID = @D ";
           SqlCommand cmd = new SqlCommand(query, conn);
           cmd.Parameters.AddWithValue("@place", txtPlace.Text);
           cmd.Parameters.AddWithValue("@ID", id);

       }
   }
Posted
Updated 14-Feb-17 3:17am
Comments
Richard Deeming 14-Feb-17 9:57am    
Looks like this question has already been solved on StackOverflow:
C# - How to update selected row in Gridview? - StackOverflow[^]

1 solution

I think you have missed below line of code :

cmd.ExecuteNonQuery();


:)
 
Share this answer
 
Comments
Richard Deeming 14-Feb-17 9:56am    
Also missing conn.Open();, and the parameter name is wrong. :)
[no name] 14-Feb-17 10:10am    
OMG he couldn't even copy/paste his code correctly.

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