How can i use data grid view to update/insert data in database using C#.

Posted by Shasseb under C# on 6/13/2012 | Points: 10 | Views : 45230 | Status : [Member] | Replies : 3
How can i update data from data grid view into SQL database using C#. Actually i m working on windows form app and i have to write update code in click event of the button.I am trying to update single row of data grid view into database. I have tried many sql queries for it but its not working :(
please help me :(

I have tried this code under a button like this
private void button9_Click(object sender, EventArgs e)
{
DataSet ds;
ds = new DataSet();
// 1. Create connection.
MySqlCommand cmd = con.CreateCommand();
// 2. init SqlDataAdapter with select command and connection
MySqlDataAdapter da = new MySqlDataAdapter("Select * from model", con);
// 3. fill in insert, update, and delete commands
MySqlCommandBuilder cmdBldr = new MySqlCommandBuilder(da);
da.Fill(ds, "model");
dataGridView2.DataSource = ds;
//dataGridView2.DataMember = "Model";
da.Update(ds, "model");
}

*but when i change a record in the datagrid and press this button the datagrid becomes empty and nothing is updated to the database. please check it.




Responses

Posted by: Sabarimahesh on: 6/14/2012 [Member] Bronze | Points: 25

Up
0
Down
http://www.dotnetfunda.com/codes/code2661-insert-directly-from-application-to-database.aspx

Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Shasseb, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shasseb on: 6/16/2012 [Member] Starter | Points: 25

Up
0
Down
Thanx for reply. but this code is getting values from text box and i have to get values from grid view. Please help me in this regard :(

Shasseb, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gemamgall on: 6/27/2012 [Member] Starter | Points: 25

Up
0
Down
use the templates to create controls in gridview and that can be loaded with values for updating / inserting / deleting.

Let me know if you need a sample code for the same

Thanks and Regards,

Vinay Gandhi
www.quadtechindia.com

Shasseb, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response