How do you Update datbase using data set? RRS feed

  • Question

  • Pliz Someone help I have almost finished my project but i cant save changes.
    Examples given does not explain each single line what it does.
    Below is my code:

       System.Data.SqlClient.SqlCommandBuilder cb;
                    cb = new System.Data.SqlClient.SqlCommandBuilder(da);
                    System.Data.DataRow dRow2 = ds1.Tables["Workers"].Rows[inc];
                    dRow2[2] = textBox1.Text;
                    dRow2[3] = textBox2.Text;
                    da.Update(ds1, "Workers");
                    MessageBox.Show("Records Updated");
                ds1[0][1] = textBox1.Text;


    It prints out the following message when I try to update;

    Dynamic SQL generation for the UpdateCommand is not Supported
    against a selectCommand that does not return any Key colmn information.
    • Edited by Eng_George Wednesday, August 19, 2009 12:57 PM
    Wednesday, August 19, 2009 12:39 PM


  • Hi

    This is exactly what it says on the tin.
    You need to have a primary key column in the select statement for the datadapter.


    ".NET Framework Class Library - SqlCommandBuilder Class"

      "The SelectCommand must also return at least one primary key or unique column. If none are present, an InvalidOperation exception is generated, and the commands are not generated."

    So check your SqlDataAdapter.SelectCommand and make sure you are filling dataset with at least one primary key column.


    This posting is provided "AS IS" with no warranties.
    Wednesday, August 19, 2009 2:23 PM