Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I want to update sql database from a editted DataGrid only by clicking a Button.
The editted cell values should be updated in Database.
Posted

simply use:

SqlCommandBuilder build = new SqlCommandBuilder(adapter);


for building the update query and on save record button click event write the below line:

adapter.Update(dt);


here adapter is a object of SqlDataAdapter and dt is object of DataTable. And make sure your table contains Primary key

refer this to learn more:

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommandbuilder.aspx?ppud=4[^]

hope this helps :)

for further queries comment here!!
 
Share this answer
 
v4
Comments
Vivek Krishnamurthy 27-Jun-11 11:49am    
The answer is not complete.

Specifying the SelectCommand is important. And the select command should return atleast one pk or unique column.

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommandbuilder.aspx

You can get more details from above link
Uday P.Singh 27-Jun-11 11:53am    
yes you are right Vivek, Thanks I updated the answer.
SqlDataAdapter adapter = null;

form_load()
{
SqlConnection connection = new SqlConnection("some connection string.");
adapter = new SqlDataAdapter("Select * from someTable",connection,);
adapter.Fill(dataTable);
}

btnUpdate()
{
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(adapter);
adapter.UpdateCommand = commandBuilder.GetUpdateCommand();
adapter.Update(dataTable);
}
 
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