Generally i need to delete row from datatable in dataset.my table have primary key.
i had verified many times but while excuting below code get the error like
Table doesn't have a primary key.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.MissingPrimaryKeyException: Table doesn't have a primary key.
Source Error:
Line 78: {
Line 79: DataTable dt = ds.Tables[0];
Line 80: DataRow dr = dt.Rows.Find(0);
Line 81: dr.Delete();
Line 82: dt.AcceptChanges();
DataRow dr = dt.Rows.Find(0);-----> this statment get the error.
insert,update worked correctly but delete not work
Mycode:
protected void Button4_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=AMMA-PC\\SQLEXPRESS;Initial
Catalog=E_learn;Integrated Security=True");
string query="select * from Student where sid="+TextBox1.Text;
SqlDataAdapter da = new SqlDataAdapter(query, con);
SqlCommandBuilder br = new SqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds, "Student");
if (ds.Tables[0].Rows.Count > 0)
{
DataTable dt = ds.Tables[0];
DataRow dr = dt.Rows.Find(0);
dr.Delete();
dt.AcceptChanges();
da.Update(ds, "Student");
Response.Write("deleted successfully");
}
}
please help me.
thank u.
What I have tried:
Table doesn't have a primary key.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.MissingPrimaryKeyException: Table doesn't have a primary key.
Source Error:
Line 78: {
Line 79: DataTable dt = ds.Tables[0];
Line 80: DataRow dr = dt.Rows.Find(0);
Line 81: dr.Delete();
Line 82: dt.AcceptChanges();
DataRow dr = dt.Rows.Find(0);-----> this statment get the error.