错误:无法将参数值从字符串转换为Int32

时间:2012-09-24 06:36:59

标签: .net sql-server

当我填写网络表单中的所有数据时,以下存储过程正常工作,但是如果我留下一些列,如AwardName2,Authority2,AwardYear2和Remarks2(我在表格中将这些列称为NULL ),程序抛出错误“无法将参数值从字符串转换为Int 32”。我在存储过程中需要做哪些更改?请帮忙。

    ALTER PROCEDURE [dbo].[usp_RegPage2]  
     @Vendor_ID VARCHAR(16), @EmpDetailsDocPath VARCHAR(200), @EmpTotalNo INT,
     @EmpAbove5Years INT, @EmpEnggDesign INT, @EmpProContract INT, @QADocPath VARCHAR(200), 
     @QAStandard VARCHAR(50), @QADocument CHAR(3), @QAControl CHAR(3), @HSEDocPath VARCHAR(200),
     @HSEStandard VARCHAR(15), @HSEDocument CHAR(3), @HSEControlManual CHAR(3),
     @AuthorityDocPath VARCHAR(200), @AwardName1 VARCHAR(50), @Authority1 VARCHAR(50),  
     @AwardYear1 INT,@Remarks1 VARCHAR(70),@AwardName2 VARCHAR(50),
     @Authority2 VARCHAR(50), @AwardYear2 INT, @Remarks2 VARCHAR(70), @AwardName3 VARCHAR(50), 
     @Authority3 VARCHAR(50), @AwardYear3 INT, @Remarks3 VARCHAR(70),
     @AwardName4 VARCHAR(50), @Authority4 VARCHAR(50), @AwardYear4 INT, @Remarks4 VARCHAR(70),
     @BritishStandard CHAR(3), @AmericanStandard CHAR(3), @OtherStandard VARCHAR(50),      
     @REManpower CHAR(3), @RESubContract CHAR(3), @RELabourPath VARCHAR(200),
     @REEquipmentPath VARCHAR(200), @REShop CHAR(3), @REAssemblyyard CHAR(3), 
     @RESizeofShop INT,@REHouse CHAR(3),@REOutSource CHAR(3),@ProjectDocPath VARCHAR(200),      
     @CmpVision VARCHAR(200), @CmpFullRegDate DATE = NULL
     AS   SET @CmpFullRegDate  = COALESCE(@CmpFullRegDate,GETDATE());
     BEGIN   
   INSERT INTO dbo.RegPage2     
   (Vendor_ID,EmpDetailsDocPath,EmpTotalNo,EmpAbove5Years,EmpEnggDesign,EmpProContract,
    QADocPath,QAStandard,QADocument,QAControl,HSEDocPath,HSEStandard,HSEDocument,
    HSEControlManual,AuthorityDocPath,AwardName1,Authority1,AwardYear1,Remarks1,
    AwardName2,Authority2,AwardYear2,Remarks2,AwardName3,Authority3,AwardYear3,Remarks3,
    AwardName4,Authority4,AwardYear4,Remarks4,BritishStandard,AmericanStandard,      
    OtherStandard,REManpower,RESubContract,RELabourPath,REEquipmentPath,REShop,
    REAssemblyyard,RESizeofShop,REHouse,REOutSource,ProjectDocPath,CmpVision,CmpFullRegDate) 
    VALUES       
    (@Vendor_ID,@EmpDetailsDocPath,@EmpTotalNo,@EmpAbove5Years,@EmpEnggDesign,@EmpProContract,
     @QADocPath,@QAStandard,@QADocument,@QAControl,@HSEDocPath,@HSEStandard,@HSEDocument,
   @HSEControlManual,@AuthorityDocPath,@AwardName1,@Authority1,@AwardYear1,@Remarks1,@AwardName2,
   @Authority2,@AwardYear2,@Remarks2,@AwardName3,@Authority3,@AwardYear3,@Remarks3,
   @AwardName4,@Authority4,@AwardYear4,@Remarks4,@BritishStandard,@AmericanStandard,   
   @OtherStandard,@REManpower,@RESubContract,@RELabourPath,@REEquipmentPath,@REShop,
   @REAssemblyyard,@RESizeofShop,@REHouse,@REOutSource,@ProjectDocPath,
   @CmpVision,@CmpFullRegDate)    
   END

C#代码

      protected void BtnRegPage2Save_Click(object sender, EventArgs e)
      {
      SqlConnection SqlCon = new SqlConnection(GetConnectionString());
      SqlCommand cmd = new SqlCommand();
      cmd.CommandType = CommandType.StoredProcedure;
      cmd.CommandText = "usp_RegPage2";
      cmd.Parameters.Add("@Vendor_ID", SqlDbType.VarChar).Value = lblPage2ID.Text;
      cmd.Parameters.Add("@EmpDetailsDocPath", SqlDbType.VarChar).Value = AFU7.FileName;
      cmd.Parameters.Add("@EmpTotalNo", SqlDbType.Int).Value = txtTotalEmp.Text.Trim();
      cmd.Parameters.Add("@EmpAbove5Years", SqlDbType.Int).Value = txtstability.Text.Trim();
      cmd.Parameters.Add("@EmpEnggDesign", SqlDbType.Int).Value = txtEnggDesign.Text.Trim();
      cmd.Parameters.Add("@EmpProContract", SqlDbType.Int).Value = txtProContract.Text.Trim();
      cmd.Parameters.Add("@QADocPath", SqlDbType.VarChar).Value = AFU8.FileName;
      cmd.Parameters.Add("@QAStandard", SqlDbType.VarChar).Value = ddlQACertificate.Text.Trim();
      cmd.Parameters.Add("@QADocument", SqlDbType.Char).Value = ddlQSDocument.Text.Trim();
      cmd.Parameters.Add("@QAControl", SqlDbType.Char).Value = ddlQCManual.Text.Trim();
      cmd.Parameters.Add("@HSEStandard", SqlDbType.VarChar).Value = ddlHSECertificate.Text;
      cmd.Parameters.Add("@HSEDocument", SqlDbType.Char).Value = ddlHSEDocument.Text;
      cmd.Parameters.Add("@HSEControlManual", SqlDbType.Char).Value = ddlHSEManual.Text;
      cmd.Parameters.Add("@HSEDocPath", SqlDbType.VarChar).Value = AFU9.FileName;
      cmd.Parameters.Add("@AuthorityDocPath", SqlDbType.VarChar).Value = AFU3.FileName;
      cmd.Parameters.Add("@AwardName1", SqlDbType.VarChar).Value = txtAward1.Text.Trim();
      cmd.Parameters.Add("@Authority1", SqlDbType.VarChar).Value = txtAuthority1.Text.Trim();
      cmd.Parameters.Add("@AwardYear1", SqlDbType.Int).Value = txtYear1.Text.Trim();
      cmd.Parameters.Add("@Remarks1", SqlDbType.VarChar).Value = txtRemarks1.Text.Trim();
      cmd.Parameters.Add("@AwardName2", SqlDbType.VarChar).Value = txtAward2.Text.Trim();
      cmd.Parameters.Add("@Authority2", SqlDbType.VarChar).Value = txtAuthority2.Text.Trim();
      cmd.Parameters.Add("@AwardYear2", SqlDbType.Int).Value = txtYear2.Text.Trim();
      cmd.Parameters.Add("@Remarks2", SqlDbType.VarChar).Value = txtRemarks2.Text.Trim();
      cmd.Parameters.Add("@AwardName3", SqlDbType.VarChar).Value = txtAward3.Text.Trim();
      cmd.Parameters.Add("@Authority3", SqlDbType.VarChar).Value = txtAuthority3.Text.Trim();
      cmd.Parameters.Add("@AwardYear3", SqlDbType.Int).Value = txtYear3.Text.Trim();
      cmd.Parameters.Add("@Remarks3", SqlDbType.VarChar).Value = txtRemarks3.Text.Trim();
      cmd.Parameters.Add("@AwardName4", SqlDbType.VarChar).Value = txtAward4.Text.Trim();
      cmd.Parameters.Add("@Authority4", SqlDbType.VarChar).Value = txtAuthority4.Text.Trim();
      cmd.Parameters.Add("@AwardYear4", SqlDbType.Int).Value = txtYear4.Text.Trim();
      cmd.Parameters.Add("@Remarks4", SqlDbType.VarChar).Value = txtRemarks4.Text.Trim();
      cmd.Parameters.Add("@BritishStandard", SqlDbType.Char).Value = ddlBritishStd.Text.Trim();
      cmd.Parameters.Add("@AmericanStandard", SqlDbType.Char).Value = ddlAmericanStd.Text.Trim();
      cmd.Parameters.Add("@OtherStandard", SqlDbType.VarChar).Value = txtOthers.Text.Trim();
      cmd.Parameters.Add("@REManpower", SqlDbType.Char).Value = ddlOwnManpower.Text.Trim();
      cmd.Parameters.Add("@RESubContract", SqlDbType.Char).Value = ddlSubContract.Text.Trim();
      cmd.Parameters.Add("@RELabourPath", SqlDbType.VarChar).Value = AFU4.FileName;
      cmd.Parameters.Add("@REEquipmentPath", SqlDbType.Char).Value = AFU5.FileName;
      cmd.Parameters.Add("@REShop", SqlDbType.Char).Value = ddlAtShop.Text;
      cmd.Parameters.Add("@REAssemblyyard", SqlDbType.Char).Value = ddlAssembly.Text;
      cmd.Parameters.Add("@RESizeofShop", SqlDbType.Int).Value = txtAssemblysize.Text;
      cmd.Parameters.Add("@REHouse", SqlDbType.Char).Value = ddlInHouse.Text;
      cmd.Parameters.Add("@REOutSource", SqlDbType.Char).Value = ddlOutSourcing.Text;
      cmd.Parameters.Add("@ProjectDocPath", SqlDbType.VarChar).Value = AFU6.FileName;
      cmd.Parameters.Add("@CmpVision", SqlDbType.VarChar).Value = txtVision.Text.Trim();
      cmd.Connection = SqlCon;
      try
      {
         SqlCon.Open();
         cmd.ExecuteNonQuery();
      }
      catch (Exception ex)
      {
         throw new Exception(ex.Message);
      }
      finally
      {
         SqlCon.Close();
         SqlCon.Dispose();
      }
     }

2 个答案:

答案 0 :(得分:1)

在表中使列成为NULL并不意味着您的PROC会自动为与该列对应的参数指定null

罪魁祸首可能是@AwardYear和其他int参数。您需要从代码中传递DBNull

if(String.IsNullOrEmpty(AwardYear))
   command.Parameters.AddWithValue("@AwardYear", DBNull.Value);
else
   command.Parameters.AddWithValue("@AwardYear", Convert.ToInt32(AwardYear));

答案 1 :(得分:0)

只需交叉检查您在proc中传递的所有数据类型与表中列的数据类型相同.Becaues我猜您表中的某些列是int,对于它们,您在sproc中传递varchar参数。