Click here to Skip to main content
14,732,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I update a modified row of my datagridview to the db?
i had tried a lot...
but nothing happened....

i asked this question 2 days before....
can any one help me....

tried following code: but...
private void showButton_Click(object sender, EventArgs e)
{
con =  new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\ETMS\\ETMS\\ETMS\\etms.accdb");
String query = "SELECT * FROM Categories";
da = new OleDbDataAdapter(query, con);
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
con.Close();
}
private void UpdateButton_Click(object sender, EventArgs e)
{
da.Update(ds);
}

if i run the code
i am getting the following error
"update requires a valid update command when pressed datarow collection with modified rows"
Please help me...
thank you
Posted
Updated 13-Jun-11 7:03am
v4

This may help you.,
private void showButton_Click(object sender, EventArgs e)
{
    con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\ETMS\\ETMS\\ETMS\\etms.accdb");
    String query = "SELECT * FROM Categories";
    da = new OleDbDataAdapter(query, con); 
    OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
    ds = new DataSet();
    da.Fill(ds);
    dataGridView1.DataSource = ds.Tables[0];
    con.Close();
}
private void UpdateButton_Click(object sender, EventArgs e)
{
    DataSet ds1 = ((DataSet)dataGridView1.DataSource).GetChanges();
    da.Update(ds1);
}
   
Comments
codesharper 13-Jun-11 8:15am
   
This is better idia to affect only changes into database but you must create insert and update command to affect the record in database.
you can do this way:

private void showButton_Click(object sender, EventArgs e)
{
con =  new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\ETMS\\ETMS\\ETMS\\etms.accdb");
String query = "SELECT * FROM Categories";
da = new OleDbDataAdapter(query, con);
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
dt=new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
private void UpdateButton_Click(object sender, EventArgs e)
{
da.Update(dt);
}


for further info refer to following links:

Edit Almost Anything in a DataGrid[^]

Auto Saving DataGridView Rows to a SQL Server Database[^]

hope this helps :)

[Edit] by Tadit
Corrected the second link's html.
As it was showing the whole html where new window icon is shown.
[/Edit]
   
v3
Comments
Ragi Gopi 14-Jun-11 2:45am
   
@Uday P.Singh
Thanku buddy....
Its Works.......
Uday P.Singh 14-Jun-11 2:52am
   
welcome :)
This link might be of some help.
   
Comments
Ragi Gopi 13-Jun-11 7:35am
   
following error is displayed....
"update requires a valid update command when pressed datarow collection with modified rows"
Ragi Gopi 13-Jun-11 7:36am
   
@RakeshMeena
i tried ur linq but again the same error is generated...
Hope this[^] will help you..
   
please check you table and put a primary key if you don't have.
and if the error is still persist try to use the solution of Uday P.Singh :)
Happy Coding!!!!
   
Comments
Ragi Gopi 14-Jun-11 2:46am
   
@Joemar Valete
i tried solution of Uday P.Singh..
its works :)
Joemar Valete 14-Jun-11 2:51am
   
Good for you :)

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