Fail update database from DataGridView
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Fail update database from DataGridView

Hybrid View

  1. #1
    Join Date
    Oct 2006
    Posts
    15

    Question Fail update database from DataGridView

    I am working at winforms project and i have a datagridview from which i am trying to update a database from it when the user changes something. Here is the code:

    DataSet ds = new DataSet();
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["EbosPr.Properties.Settings.Database1ConnectionString1"].ConnectionString);
    SqlCommand scmd = new SqlCommand("Select * From CustCalls ", conn);
    SqlDataAdapter sda = new SqlDataAdapter(scmd);

    sda.Fill(ds);

    DataTable dt = ds.Tables[0];
    this.dataGridView1.BindingContext[dt].EndCurrentEdit();
    SqlCommandBuilder myBuilder = new SqlCommandBuilder(sda);

    myBuilder.GetUpdateCommand();

    sda.UpdateCommand = myBuilder.GetUpdateCommand();
    sda.Update(dt);

    The above code doesnt update the database. Any idea?

  2. #2
    Join Date
    Dec 2011
    Posts
    41

    Re: Fail update database from DataGridView

    Did you check the below?

    Before to your Update command ou have to give

    DataGridView1.EndEdit();

    And after to your update command you have to give

    sda.Acceptchanges
    Last edited by MAHEY; February 17th, 2012 at 12:39 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center