Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

Please help me in solving following query:

I want to update table at backend in database throughmakeing changes in datagridview.

What I want is, when User makes changes in datagridview and clicks save or update button, the changes which are made in datagridview should get reflect in table in databse.

For ex:

I am having Datagridview, and Save button on WinForm and table EmpDetails at backend in databse. I am filling Datagridview with table EmpDetails. After making changes in Datagridview when user clicked Save button, whichever changes made in datagridview should get reflect at backend in table EmpDetails.


I am using visual studio2008 with c# and MS Access as database.
Posted
Updated 16-Apr-12 0:14am
v2
Comments
Member 8233601 16-Apr-12 6:15am    
right now i m using ti code:


private void Teacher_Routine_Load(object sender, EventArgs e)
{
OleDbDataAdapter adpt = new OleDbDataAdapter("select * from ClassAdmin Order by Class_name", conn);
OleDbCommandBuilder cbld = new OleDbCommandBuilder(adpt);
conn.Open();


adpt.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
//conn.Close();



}

private void button1_Click(object sender, EventArgs e)
{
adpt.Update(ds);
}
Member 8233601 16-Apr-12 6:22am    
I am getting this error:

Update requires a valid UpdateCommand when passed DataRow collection with modified rows.
Bala Selvanayagam 16-Apr-12 7:42am    
Do you have a unique primary key on "ClassAdmin"
Member 8233601 16-Apr-12 8:05am    
no
[no name] 16-Apr-12 6:54am    
The error is pretty clear.... you need to write an UPDATE statement. What is the problem?

1 solution

Hope you have a unique key in the table "ClassAdmin" & this should work and let me know any error msgs

public partial class Form1 : Form
   {

       OleDbDataAdapter adpt;
       DataSet ds = new DataSet();

       public Form1()
       {
           InitializeComponent();

           try
           {

               OleDbConnection conn = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source=C:\\test.mdb;");
               adpt = new OleDbDataAdapter("select * from ClassAdmin Order by Class_name", conn);

               conn.Open();
               adpt.Fill(ds);
               OleDbCommandBuilder cbld = new OleDbCommandBuilder(adpt);
               dataGridView1.DataSource = ds.Tables[0]; //conn.Close();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }


       private void button3_Click(object sender, EventArgs e)
       {
           try
           {
               adpt.Update(ds);
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }
   }
 
Share this answer
 
Comments
Member 8233601 17-Apr-12 1:07am    
its not working...still the same error..
Member 8233601 17-Apr-12 1:41am    
thanks 4 every thing...
Member 8233601 17-Apr-12 3:03am    
its work fine we cannot made multiple changes
Bala Selvanayagam 17-Apr-12 7:33am    
It should work for multiple lines and please check.

There is a usefull tip for performance boast when SqlDataAdapter update is to
set updatebatchsize=0 just above the update line and in your case


private void button3_Click(object sender, EventArgs e)
{
try
{
adpt.updatebatchsize=0
adpt.Update(ds);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

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