2017-04-22 108 views
0

我想插入僱員的詳細信息到數據庫使用按鈕,我使用的是asp.net,sql server 2016.數據不會插入它既不會給出任何錯誤。指定和部門是下拉列表。數據沒有插入

protected void btnCreate_Click(object sender, EventArgs e) 
    { 
     SqlConnection conn = new SqlConnection(@"connectionstring"); 

      try 
      { 

       conn.Open(); 
      //SqlCommand cmd = new SqlCommand("Insert into Employee values (@EmpID, @FirstName, @LastName, @Email, @Password,@DesigID, @DeptID, @Address, @Contact)", conn); 
      SqlCommand cmd = new SqlCommand("insertemployee", conn); 
      cmd.CommandType = System.Data.CommandType.StoredProcedure; 

       // int ddldedig = Convert.ToInt32(DropDownListDesig.SelectedValue); 
       //int ddldept = Convert.ToInt32(DropDownListDept.SelectedValue); 

       cmd.Parameters.AddWithValue("@EmpID", TextBoxEmpID.Text); 

       cmd.Parameters.AddWithValue("@FirstName", TextBoxFirstName.Text); 
       cmd.Parameters.AddWithValue("@LastName", TextBoxLastName.Text); 
       //cmd.Parameters.AddWithValue("BankID", TextBoxBankID.Text); 

       cmd.Parameters.AddWithValue("@Email", TextBoxEmail.Text); 
       cmd.Parameters.AddWithValue("@Password", TextBoxPassword.Text); 

       cmd.Parameters.AddWithValue("@DesigID", DropDownListDesig.SelectedValue); 
       cmd.Parameters.AddWithValue("@DeptID", DropDownListDept.SelectedValue); 
       cmd.Parameters.AddWithValue("@Address", TextBoxAddress.Text); 
       cmd.Parameters.AddWithValue("@Contact", TextBoxContact.Text); 

       cmd.ExecuteNonQuery(); 
       ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Employee Account Successfully Create')", true); 

      } 

      catch (SqlException ex) 
      { 
       Console.WriteLine("value added"); 
      } 
      finally 
      { 
       conn.Close(); 
      } 


    } 

以下是存儲過程:

ALTER PROCEDURE [dbo].[insertemployee] 
     -- Add the parameters for the stored procedure here 
     @EmpID nchar(10), 
     @FirstName varchar(50), 
     @LastName varchar(50), 
     @Email varchar(50), 
     @Password varchar(50), 
     @BankID int , 
     @DesigID int , 
     @DeptID int , 
     @Address varchar(50), 
     @Contact varchar(50) 
    AS 
    BEGIN 
     -- SET NOCOUNT ON added to prevent extra result sets from 
     -- interfering with SELECT statements. 
     SET NOCOUNT ON; 

     -- Insert statements for procedure here 
     insert into Employee (EmpID,FirstName, LastName, Email,"Password",DesigID,DeptID,"Address", Contact) values (@EmpID,@FirstName, @LastName, @Email, @Password,@DesigID,@DeptID,@Address,@contact) 
    END 
+0

爲什麼您評論BankId的代碼?如果不需要,那麼也從SQL查詢代碼中刪除代碼。我認爲錯誤是在您的SQL命令不在C#代碼中。對於BankID拋出異常,將null轉換爲int。嘗試手動執行SQL查詢以提供相同的值。 –

回答

0

如果EmployeID的變量類型是int,所以你需要轉換的txtBoxEmpId的價值爲int,希望幫助。 cmd.Parameters.AddWithValue("@EmpID", Convert.ToInt32(TextBoxEmpID.Text));