There are two tables [UserData
] and [HotelData
] I've linked them with a foreign key. which is "Username
" and I want to delete which ever Username is entered and delete its data on the 2nd table as well. I don't know how to write the sql command or c#.
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: The DELETE statement conflicted with the REFERENCE constraint "FKHotelData". The conflict occurred in database "E:\GRADED UNIT DEV\BLACKMARCH\BLACKMARCH\BIN\DEBUG\DATABASEBM.MDF", table "dbo.HotelData", column 'Username'.
The statement has been terminated.
private void btnDelete_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=E:\Graded unit Dev\BlackMarch\BlackMarch\bin\Debug\DataBaseBM.mdf;Integrated Security=True;Connect Timeout=30");
string sqlStatement = "DELETE FROM UserData WHERE Username = @Username";
con.Open();
SqlCommand cmd = new SqlCommand(sqlStatement, con);
cmd.Parameters.AddWithValue("@Username", txtUsernameUser.Text);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
con.Close();
}