Friday, February 24, 2012

Help me pls - with database and Insert statement

I Have an error:

Server Error in '/quanlythietbi' Application.

INSERT statement conflicted with COLUMN FOREIGN KEY constraint 'FK_yeucau_nhanvien'. The conflict occurred in database 'equipment', table 'nhanvien', column 'manv'. The statement has been terminated.

Source Error:

Line 129:mycommand.Parameters.Add(new SqlParameter("@.noidung_yc1",System.Data.SqlDbType.Text));Line 130:mycommand.Parameters["@.noidung_yc1"].Value = TextBox1.Text;Line 131:int i = mycommand.ExecuteNonQuery();Line 132:if (i>0)Line 133:{

and this is my code:

string sqlstring = "Select * from yeucau where ngayGiaiQuyetxong='"+ Label8.Text +"' and date_yc='" + Label7.Text + "' and manv_yc='"+ TextBox2.Text + "' and noidung_yc='"+ TextBox1.Text+ "'";

myconnection =new SqlConnection(stringconn);

mycommand =new SqlCommand(sqlstring,myconnection);

myconnection.Close();

myconnection.Open();

mycommand =new SqlCommand(insertquery,myconnection);

mycommand.Parameters.Add(new SqlParameter("@.ngayGiaiQuyetxong1",System.Data.SqlDbType.Char,10));

mycommand.Parameters["@.ngayGiaiQuyetxong1"].Value = Label8.Text;

mycommand.Parameters.Add(new SqlParameter("@.date_yc1",System.Data.SqlDbType.SmallDateTime));

mycommand.Parameters["@.date_yc1"].Value = Label7.Text;

mycommand.Parameters.Add("@.manv_yc1",System.Data.SqlDbType.Char,10);

mycommand.Parameters["@.manv_yc1"].Value = TextBox2.Text;

mycommand.Parameters.Add(new SqlParameter("@.noidung_yc1",System.Data.SqlDbType.Text));

mycommand.Parameters["@.noidung_yc1"].Value = TextBox1.Text;

int i = mycommand.ExecuteNonQuery();

if (i>0)

{

lbcheck.Text = "?ã C?p Nh?t Yêu C?u.";

}

---------------------------------------

I don't know what I must do to repair it :(

How does insertquery look like? The error comes from an INSERT command, so you should check the INSERT command to see whether it tries to insert a row which conficts with the FK constraint. Such error will be raised if you try to insert a row with a field value that does not exist in the PRIMARY KEY (in another table) referenced by the FOREIGN KEY. To learn more about FOREIGN KEY constraint, you can refer to:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/createdb/cm_8_des_04_8ypg.asp

|||

thank a lot :) I will try to fix it now

No comments:

Post a Comment