Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
yamoo
Starting Member
11 Posts |
Posted - 2013-08-05 : 16:56:21
|
public partial class _Default : System.Web.UI.Page{ SqlConnection con = new SqlConnection(); SqlCommand com = new SqlCommand(); SqlDataAdapter dap = new SqlDataAdapter(); DataSet ds = new DataSet(); DataTable dt = new DataTable(); DataTable dt1 = new DataTable(); DataRow dr; protected void Page_Load(object sender, EventArgs e) { load(); } private void load() { con.ConnectionString = ConfigurationManager.ConnectionStrings["north"].ConnectionString; com.CommandText = "select employeeid,FirstName, LastName from employees order by employeeid"; com.Connection = con; dap.SelectCommand = com; dt.TableName = "employees"; ds.Tables.Add(dt); dap.Fill(ds.Tables["employees"]); cobedi.Items.Clear(); cobdel.Items.Clear(); for (int i = 0; i < dt.Rows.Count; i++) { cobedi.Items.Add(dt.Rows[i][0].ToString()); cobdel.Items.Add(dt.Rows[i][0].ToString()); }} protected void btndel_Click(object sender, EventArgs e) { con.ConnectionString = ConfigurationManager.ConnectionStrings["north"].ConnectionString; com.CommandText = "delete from employees where employeeid=@id2"; com.Parameters.Add("@id2", SqlDbType.Int).Value = cobdel.SelectedItem.ToString(); com.Connection = con; dap.DeleteCommand = com; dr = ds.Tables["employees"].Rows[cobdel.SelectedIndex]; dr.Delete(); dap.Update(ds.Tables["employees"]); view(); }when i delete row .... message appear in web page :-Server Error in '/R.Ado' Application.The DELETE statement conflicted with the REFERENCE constraint "FK_Orders_Employees". The conflict occurred in database "Northwind", table "dbo.Orders", column 'EmployeeID'.The statement has been terminated.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.SqlClient.SqlException: The DELETE statement conflicted with the REFERENCE constraint "FK_Orders_Employees". The conflict occurred in database "Northwind", table "dbo.Orders", column 'EmployeeID'.The statement has been terminated.Source Error:Line 132: dr = ds.Tables["employees"].Rows[cobdel.SelectedIndex]; //select row to deleteLine 133: dr.Delete();Line 134: dap.Update(ds.Tables["employees"]);Line 135: view();Line 136: }Source File: f:\C# APPLICATION\asp\R.Ado\Default.aspx.cs Line: 134 |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-08-05 : 17:45:49
|
The foreign key constraint violation means that there are rows in the Orders table that belong to the employees that you are trying to delete. If you do want to delete that employee, first delete the rows in the order table using:DELETE FROM Orders WHERE employeeid=@id2 Then delete the employee from the employees table. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
|
|
|
|