How do i update a database from dataset.

SqlDataAdapter.Update()

is not working

Recommended Answers

All 10 Replies

Try using the SqlCommandBuilder Class like this

SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(mySqlDataAdapter);

 mySqlDataAdapter.Update(myDataSet, "myTableName");

Tried that but its not working. Do i have to set the update,delete and insert command?

Try using the SqlCommandBuilder Class like this

SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(mySqlDataAdapter);

 mySqlDataAdapter.Update(myDataSet, "myTableName");

Can u post your full code here.

Here is the code

SqlDataAdapter da = new SqlDataAdapter("select * from mytable",strCon);
        DataSet ds = new DataSet();
        da.Fill(ds);

        GridView2.DataSource = ds;
        GridView2.DataBind();
        SqlCommandBuilder m_cd = new SqlCommandBuilder(da);
        string UpdateCommand = m_cd.GetUpdateCommand().CommandText;
             
        da.Update(ds, "mytable");

Try this:

SqlDataAdapter da = new SqlDataAdapter("select * from mytable",strCon);
SqlCommandBuilder m_cd = new SqlCommandBuilder(da);
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;

        DataSet ds = new DataSet();
        da.Fill(ds);

        GridView2.DataSource = ds;
        GridView2.DataBind();
        
        string UpdateCommand = m_cd.GetUpdateCommand().CommandText;
             
        da.Update(ds, "mytable");

Throwing an error as

Update unable to find TableMapping['mytable'] or DataTable 'mytable'

Try this:

SqlDataAdapter da = new SqlDataAdapter("select * from mytable",strCon);
SqlCommandBuilder m_cd = new SqlCommandBuilder(da);
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;

        DataSet ds = new DataSet();
        da.Fill(ds);

        GridView2.DataSource = ds;
        GridView2.DataBind();
        
        string UpdateCommand = m_cd.GetUpdateCommand().CommandText;
             
        da.Update(ds, "mytable");

da.Update(ds, "mytable");

Here "mytable" should be replace by your tablename. I have given it just as an example. Since your Dataset contains only one table you can use this instead.

da.Update(ds.Tables[0]);

i had replaced it with my table name.Still it was showing that error. while changing it to

da.Update(ds.Tables[0]);

as you suggested, no updation was done on the table

finally i hv found the solution. i was actually tryng to update MS access. for that you have to check the permissions of the table and give proper permissions

can show the solution u do?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.