4
Answers

updating database from dataset

sahil kumar

sahil kumar

11y
1k
1
DataSet ds1 = new DataSet();
 SqlDataAdapter da1 = new SqlDataAdapter("select * from registrar_packets", con);
 da1.Fill(ds1);
foreach(somthing)
{

 ds1.Tables[0].Rows.Add(filename, s1, flag);   //filename, s1,flag is variable
                    ds1.AcceptChanges();

}
//here all data is filled in dataset ds1

SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(da1);
                da1.Update(ds1.Tables[0]);     // database not updated



this is not updating my sqlserver database.
what wrong i am doing???

Answers (4)
0
Sanjeeb Lenka

Sanjeeb Lenka

NA 22.1k 1.3m 11y
hi,

i modified this line check it

ds1.Tables[0].Rows.Add(null,filename, s1, flag);

here identity is automatically increment so its showing error because this line trying to insert the in identity column  with the filename field. so just pass the null value it will automatically increment the identity.


any problem reply

Mark it as answered if it helps you
Accepted
0
sahil kumar

sahil kumar

NA 14 5.9k 11y
thanks
0
sahil kumar

sahil kumar

NA 14 5.9k 11y
thanks it working.
cud you let me know the reason y ds1.AcceptChanges(); was not working


and one more query

if my database has a identity column   (id,colum1,colum2,colum3)

 ds1.Tables[0].Rows.Add(filename, s1, flag);   //filename, s1,flag is variable      gives error as it expects numeric value for identity column at filename. how can i update data in datatable so that it has identity value from database and on update its updates on database
0
Sanjeeb Lenka

Sanjeeb Lenka

NA 22.1k 1.3m 11y
hi,

  i modified your code just check it.

DataSet ds1 = new DataSet();
 SqlDataAdapter da1 = new SqlDataAdapter("select * from registrar_packets", con);
 da1.Fill(ds1);
foreach(somthing)
{
 ds1.Tables[0].Rows.Add(filename, s1, flag);  //filename, s1,flag is variable 

}
//here all data is filled in dataset ds1
SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(da1);
da1.Update(ds1.Tables[0]); 
ds1.AcceptChanges();


any problem reply

if it helps you mark it as answered.