deleting data from gridview it's shows the following error? [Resolved]

Posted by Sudheep.grandhe under C# on 1/10/2012 | Points: 10 | Views : 1117 | Status : [Member] | Replies : 3
Hi,

I am developing windows applications.it shows the following error?
Error:
The DELETE statement conflicted with the REFERENCE constraint "FK_tbl_Report1_tbl_Report2". The conflict occurred in database "Sudheeptest", table "dbo.tbl_Report1", column 'InvoiceNo'.
The statement has been terminated.

Here is my code for delete.how can i delete the row from gridview please give me suggesstions in my code.please do the needful.

private void btn_delete_Click_1(object sender, EventArgs e)
{
if (txt_customeraddress .Text == "" || txt_invoicedate.Text == "")
{
MessageBox.Show("Please Select Record From Grid", "Image Impact Services(IIS)", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");
SqlCommand cmd = new SqlCommand("delete from tbl_report2 where invoiceno=" + invoiceno + "", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Open();
DataTable dt = new DataTable();
da.Fill(dt);
cmd.ExecuteNonQuery();
con.Close();
GridView1.DataSource = dt;

SqlConnection conn = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");
SqlCommand cmd1 = new SqlCommand("delete from tbl_report1 where invoiceno=" + invoiceno + "", conn);
SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
conn.Open();
DataTable dt1 = new DataTable();
da1.Fill(dt1);
cmd1.ExecuteNonQuery();
con.Close();
GridView1.DataSource = dt1;
griddisplay();
MessageBox.Show("Record Delete Successfully","Image Impact Services(IIS)",MessageBoxButtons .OK ,MessageBoxIcon.Information );

}
}

Best,
Sudheep.



Responses

Posted by: Blessyjees on: 1/10/2012 [Member] Bronze | Points: 50

Up
0
Down

Resolved
Hi,

If you need to remove the parent row, you must delete all child rows. This error specify that the child table exist records with this id reference.

Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 1/10/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

i want to display combobox inside the gridview or search button .can you please send me the sample code.please do the needful.

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ogipansrk on: 1/12/2012 [Member] Starter | Points: 25

Up
0
Down
Sudheep,

It is not a good practice writing all the code.
You can use Mircrosoft Application Blocks , this will reduce your code and valuable time.

Hope this !

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response