2012-07-29 62 views
-1

當我嘗試使用上一步的TextBox控件的值時,ASP.NET嚮導中出現此錯誤。在嚮導中保存「步驟」中的值ASP.NET

錯誤:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Contact_Emp". 
The conflict occurred in database "KKSTech", table "dbo.Emp", column 'EmpID'. 

是它訪問的不同步驟控制的價值觀出了問題?

+0

由於外鍵值不正確,此問題即將到來。你調試了代碼並得到了什麼文本框? – 2012-07-29 06:25:16

+0

步驟1中的TextBox1正在插入dbo.Emp(EmpID PK),但未插入dbo.contacts,這是一個FK .. – Girish 2012-07-29 06:50:42

+0

[如何在ASP.NET嚮導中的步驟之間存儲值?](http: //stackoverflow.com/questions/11690960/how-to-store-values-between-steps-in-asp-net-wizard) – Aristos 2012-07-29 07:20:55

回答

0

外鍵確保它在該列中的值不在被引用表的主鍵列中。

在你的情況,你將EmpIDcontact表中不存在的EmpIDEmp表的引用表。

+0

EmpID正在插入到dbo.Emp表中,它是一個PK ..我用單獨的類來插入Employees的細節,但同樣沒有插入到其他表中。 – Girish 2012-07-29 06:49:40

+0

您是否檢查過第1步中的TextBox1.Text在插入聯繫人表時是否具有相同的值..... – 2012-07-29 07:02:33

+0

否我沒有檢查過,因爲在嚮導中我瞭解到值不會被遺忘。我現在該怎麼做?我完全困惑.. :( – Girish 2012-07-29 07:05:38

1

這是第一類插入到dbo.Emp表

public void InsertInfo() 
     { 
      String KKStech = @"Data Source=USER-PC\SQLEXPRESS;Initial Catalog=KKSTech;Integrated Security=True"; 
      SqlConnection conn = new SqlConnection(KKStech); 
      String insertstring = @"insert into Emp (EmpID, FirstName, LastName, MiddleName, Mob1, Mob2, Phone, Email1, Email2, EmpDesc) 
          values (@EmpID, @FirstName, @LastName, @MiddleName, @Mob1, @Mob2)"; 
      SqlCommand cmd = new SqlCommand(insertstring, conn); 
      cmd.CommandText = insertstring; 
      cmd.CommandType = CommandType.Text; 

      try 
      { 
       conn.Open(); 
       cmd.Parameters.AddWithValue("@EmpID", TextBox1.Text); 
       cmd.Parameters.AddWithValue("@FirstName", TextBox2.Text); 
       cmd.Parameters.AddWithValue("@LastName", TextBox3.Text); 
       cmd.Parameters.AddWithValue("@MiddleName", TextBox4.Text); 
       cmd.Parameters.AddWithValue("@Mob1", TextBox5.Text); 
       cmd.Parameters.AddWithValue("@Mob2", TextBox6.Text); 
       cmd.ExecuteNonQuery(); 
      } 

      finally 
      { 
       conn.Close(); 
      } 
     } 

而這是一個其中I「米插入其中的EmpID是FK

public void Insertaddress() 
{ 
    String KKStech = @"Data Source=USER-PC\SQLEXPRESS;Initial Catalog=KKSTech;Integrated Security=True"; 
    SqlConnection conn = new SqlConnection(KKStech); 
    String str = @"insert into Contact (Addressline1, Addressline2, CityID, EmpID) 
           values(@Addressline1, @Addressline2, @CityID, @EmpID)"; 
    SqlCommand cmd = new SqlCommand(str, conn); 
    cmd.CommandText = str; 
    cmd.CommandType = CommandType.Text; 

    try 
    { 
     conn.Open(); 
     cmd.Parameters.AddWithValue("@Addressline1", TextBox15.Text); 
     cmd.Parameters.AddWithValue("@Addressline2", TextBox17.Text); 
     cmd.Parameters.AddWithValue("@CityID", DropDownList2.SelectedValue); 
     cmd.Parameters.AddWithValue("@EmpID", TextBox1.Text); 
     cmd.ExecuteNonQuery(); 
    } 
    catch (System.Data.SqlClient.SqlException ex) 
    { 
     string msg = "Insert Error:"; 
     msg += ex.Message; 
     throw new Exception(msg); 
    } 

    finally 
    { 
     conn.Close(); 
    } 

} 

這是表我的問題。