can you tell me that how to delete or update data using dataadapter in visual c#
i am giving you the code:
the delete module:
SqlCommandBuilder cb;
cb = new SqlCommandBuilder(da);
dset.Tables["micky"].Rows[inc].Delete();
MaxRow--;
inc = 0;
display();
da.Update(dset, "micky");
MessageBox.Show("record deleted");
con.Close();
the update module:
SqlCommandBuilder cb;
cb = new SqlCommandBuilder(da);
DataRow drow= dset.Tables["micky"].Rows[inc];
drow[1] = textBox1.Text;
drow[2] = textBox2.Text;
drow[3] = textBox3.Text;
da.Update(dset, "micky");
MessageBox.Show("data updated");
con.Close();
any suggestion will be welcomed
my email id is [email protected]
Loading
ajay rajuPosted Jul 24, 2010, 5:44 AM
in Update no need to take DataRow class because datarow class is used only for insert. DataRow class is it add a one empty row for the table.
Try this i am taking sample table i.e., Emp
Code for Update
String str = "UPDATE Emp SET Ename = '" + txtEname.Text + "',Esal = '" + txtSal.Text + "' WHERE EmpID = '" + txtEno.Text + "'" ;
da = New SqlDataAdapter(str, cn);
da.Fill(ds, "Emp");
cb = New SqlCommandBuilder(da);
da.Update(ds, "Emp");
MessageBox.Show("Record Updated Successfully");
Code For Delete
String str = "DELETE EMP WHERE EmpID = " + txtEno.Text ;
da = New SqlDataAdapter(str, cn);
da.Fill(ds, "Emp");
cb = New SqlCommandBuilder(da);
da.Update(ds, "Emp");
MessageBox.Show("Record Deleted Successfully");
here da is sqldataadapter and ds is dataset. I hope it works you.
If my answer is helps please mark as accept answer.
Thanks.