none
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.
    thx.
    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;

                da.Update(ds1);

    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

Answers

  • 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.

    From:

    ".NET Framework Class Library - SqlCommandBuilder Class"
    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommandbuilder.aspx

      "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.

    HTH
    //Michael


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