tags:

views:

22

answers:

1

i am using the following code to insert a datatable to an existing table of database but it's giving exception "Update requires a valid InsertCommand when passed DataRow collection with new rows"

where query is select * from placed_student

  public Boolean insert(string query, DataTable dt)
        {
            try
            {
                SqlDataAdapter sqlDA = new SqlDataAdapter(query, _sqlCon);
                DataTable dtValues = new DataTable();
                sqlDA.Fill(dtValues);
                sqlDA.Update(dt);
                return false;
            }
            catch(Exception ex)
            {
                logger.Error("Error when executing Query ", ex);
                return false;
            }
        }
A: 

Use SqlCommandBuilder to generate the InsertCommand for your DA.

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommandbuilder.aspx

Jerome