1

我得到了错误:

过程或函数 usp_User_Info3 指定的参数过多

当我运行程序时。我不知道 SP 或 C# 代码中的错误。我必须Vendor_ID在用户单击提交按钮后显示。这里哪里出了问题?

表结构:

         CREATE TABLE User_Info3 
         ( 
         SNo int Identity (2000,1) ,  
         Vendor_ID AS 'VEN' + CAST(SNo as varchar(16)) PERSISTED PRIMARY KEY, 
         UserName VARCHAR(16) NOT NULL, 
         User_Password VARCHAR(12) NOT NULL, 
         User_ConPassword VARCHAR(12) NOT NULL, 
         User_FirstName VARCHAR(25) NOT NULL, 
         User_LastName VARCHAR(25) SPARSE NULL, 
         User_Title VARCHAR(35) NOT NULL, 
         User_EMail VARCHAR(35) NOT NULL, 
         User_PhoneNo VARCHAR(14) NOT NULL, 
         User_MobileNo VARCHAR(14)NOT NULL, 
         User_FaxNo VARCHAR(14)NOT NULL, 
         UserReg_Date DATE DEFAULT GETDATE() 
         )

存储过程:

         ALTER PROCEDURE [dbo].[usp_User_Info3]
         @SNo INT OUTPUT,
         @Vendor_ID VARCHAR(10) OUTPUT, 
         @UserName VARCHAR(30),
         @User_Password VARCHAR(12),
         @User_ConPassword VARCHAR(12),
         @User_FirstName VARCHAR(25),
         @User_LastName VARCHAR(25),
         @User_Title VARCHAR(35),
         @User_OtherEmail VARCHAR(30),
         @User_PhoneNo VARCHAR(14),
         @User_MobileNo VARCHAR(14),
         @User_FaxNo VARCHAR(14)
         AS
         BEGIN
          SET NOCOUNT ON;
         INSERT INTO User_Info3 (UserName,User_Password,User_ConPassword,User_FirstName,     
         User_LastName,User_Title,User_OtherEmail,User_PhoneNo,User_MobileNo,User_FaxNo)
               VALUES  (@UserName,@User_Password,@User_ConPassword,@User_FirstName,@User_LastName,
               @User_Title,@User_OtherEmail,@User_PhoneNo,@User_MobileNo,@User_FaxNo)
        SET @SNo = Scope_Identity()
        SELECT Vendor_ID From User_Info3 WHERE SNo = @SNo
        END

C#代码:

         protected void BtnUserNext_Click(object sender, EventArgs e)
         {
         cmd.CommandType = CommandType.StoredProcedure;
         cmd.CommandText = "usp_User_Info3";
         System.Data.SqlClient.SqlParameter SNo=cmd.Parameters.Add("@SNo",System.Data.SqlDbType.Int);
         System.Data.SqlClient.SqlParameter Vendor_ID=cmd.Parameters.Add("@Vendor_ID",  
                                                                   System.Data.SqlDbType.VarChar,10);
         cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = txtUserName.Text;
         cmd.Parameters.Add("@User_Password", SqlDbType.VarChar).Value = txtRegPassword.Text;
         cmd.Parameters.Add("@User_ConPassword", SqlDbType.VarChar).Value = txtRegConPassword.Text;
         cmd.Parameters.Add("@User_FirstName", SqlDbType.VarChar).Value = txtRegFName.Text;
         cmd.Parameters.Add("@User_LastName", SqlDbType.VarChar).Value = txtRegLName.Text;
         cmd.Parameters.Add("@User_Title", SqlDbType.VarChar).Value = txtRegTitle.Text;
         cmd.Parameters.Add("@User_OtherEmail", SqlDbType.VarChar).Value = txtOtherEmail.Text;
         cmd.Parameters.Add("@User_PhoneNo", SqlDbType.VarChar).Value =txtRegTelephone.Text;
         cmd.Parameters.Add("@User_MobileNo", SqlDbType.VarChar).Value =txtRegMobile.Text;
         cmd.Parameters.Add("@User_FaxNo", SqlDbType.VarChar).Value =txtRegFax.Text;
         cmd.Connection = SqlCon;
         try
         {
          Vendor_ID.Direction = System.Data.ParameterDirection.Output;
          SqlCon.Open();
          cmd.ExecuteNonQuery();
          string VendorID = cmd.ExecuteScalar() as string; 
         }
         catch (Exception ex)
         {
           throw new Exception(ex.Message);
         }
         finally
         {
          string url = "../CompanyBasicInfo.aspx?Parameter=" + Server.UrlEncode(" + VendorID + ");
          SqlCon.Close();
         }
        }
4

5 回答 5

3
  • 您没有设置@SNo参数的方向
  • 你调用了两次命令 -ExecuteScalar如果你想要返回值,只需调用它。
  • 您没有@Vendor_ID在存储过程中设置输出参数的值。
于 2012-08-28T09:19:22.143 回答
1

如果我不得不猜测,我会打赌它cmd正在被重复使用,并且其中包含来自先前调用的参数。一种选择是调用cmd.Parameters.Clear(),但坦率地说,我认为重用这个SqlCommand实例没什么意义——每次都使用一个新命令会更好:

using(var cmd = SqlCon.CreateCommand()) {
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "usp_User_Info3";
    // TODO: add parameters
    // TODO: call one of the Execute* methods
}
于 2012-08-28T09:19:35.067 回答
0

在所有这些伟大思想的回答之后,我不敢相信你仍然被困住。以下是我的建议

在你的 PROC

'maintain this in your parameter
@Vendor_ID VARCHAR(10) OUTPUT,

更新为 Select VendorIDinto @VendorID。见第二行

    SET @SNo = Scope_Identity()
    SELECT @Vendor_ID=VendorID From User_Info3 WHERE SNo = @SNo

在 C# 中,不要调用ExecuteScalaruseExecuteNonQuery

string newVendorID = "";
try
{
      Vendor_ID.Direction = System.Data.ParameterDirection.Output;
      SqlCon.Open();
      cmd.ExecuteNonQuery();
      if(Vendor_ID.Value != null)
          newVendorID = Vendor_ID.Value.ToString();
}

您可以将Vendor_ID参数重命名为更有意义的名称,例如VendorIDParam. 希望这可以帮助?

于 2012-08-29T17:36:47.087 回答
0

表中不存在User_OtherEmail

正确的存储过程

Create PROCEDURE [dbo].[usp_User_Info3]
     @SNo INT OUTPUT,
     @Vendor_ID VARCHAR(10) OUTPUT, 
     @UserName VARCHAR(30),
     @User_Password VARCHAR(12),
     @User_ConPassword VARCHAR(12),
     @User_FirstName VARCHAR(25),
     @User_LastName VARCHAR(25),
     @User_Title VARCHAR(35),
      @User_Email VARCHAR(30),
     @User_PhoneNo VARCHAR(14),
     @User_MobileNo VARCHAR(14),
     @User_FaxNo VARCHAR(14)
     AS
     BEGIN
      SET NOCOUNT ON;
     INSERT INTO User_Info3 (UserName,User_Password,User_ConPassword,User_FirstName,     
     User_LastName,User_Title,User_Email,User_PhoneNo,User_MobileNo,User_FaxNo)
           VALUES  (@UserName,@User_Password,@User_ConPassword,@User_FirstName,@User_LastName,
           @User_Title,@User_Email,@User_PhoneNo,@User_MobileNo,@User_FaxNo)
    SET @SNo = Scope_Identity()
    SELECT Vendor_ID From User_Info3 WHERE SNo = @SNo
    END

以及相应修改的 c# 代码

于 2012-08-28T09:21:47.293 回答
0

感谢您的所有回复。在我根据您的想法多次修改 C# 代码和 SP 之后,我终于得到了我的问题的答案。我从我的 SP 中删除了“Vendor_ID”,并在我的代码中添加了一些代码,如下所示。

存储过程:

      ALTER PROCEDURE [dbo].[usp_User_Info3]
      @SNo INT OUTPUT,
      @UserName VARCHAR(30),
      @User_Password VARCHAR(12),
      @User_ConPassword VARCHAR(12),
      @User_FirstName VARCHAR(25),
      @User_LastName VARCHAR(25),
      @User_Title VARCHAR(35),
      @User_OtherEmail VARCHAR(30),
      @User_PhoneNo VARCHAR(14),
      @User_MobileNo VARCHAR(14),
      @User_FaxNo VARCHAR(14)
      AS
      BEGIN
      SET NOCOUNT ON;
      INSERT INTO User_Info3 (UserName,User_Password,User_ConPassword,User_FirstName,User_Title,User_OtherEmail,User_PhoneNo,User_MobileNo,User_FaxNo)
           VALUES (@UserName,@User_Password,@User_ConPassword,@User_FirstName,@User_LastName,@User_Title,@User_OtherEmail,@User_PhoneNo,@User_MobileNo,@User_FaxNo)
  SET @SNo = Scope_Identity() 
      END

C#代码:

    protected void BtnUserNext_Click(object sender, EventArgs e)
    {
    SqlCon.Open();
    SqlCommand cmd2 = new SqlCommand("usp_User_Info3", SqlCon);
    cmd2.CommandType = CommandType.StoredProcedure;
    cmd2.CommandText = "usp_User_Info3";
    System.Data.SqlClient.SqlParameter SNo=cmd2.Parameters.Add("@SNo", System.Data.SqlDbType.Int);
    cmd2.Parameters.Add("@UserName", SqlDbType.VarChar).Value = txtUserName.Text.Trim();
    cmd2.Parameters.Add("@User_Password", SqlDbType.VarChar).Value = txtRegPassword.Text.Trim();
    cmd2.Parameters.Add("@User_ConPassword", SqlDbType.VarChar).Value = txtRegConPassword.Text;
    cmd2.Parameters.Add("@User_FirstName", SqlDbType.VarChar).Value = txtRegFName.Text.Trim();
    cmd2.Parameters.Add("@User_LastName", SqlDbType.VarChar).Value = txtRegLName.Text.Trim();
    cmd2.Parameters.Add("@User_Title", SqlDbType.VarChar).Value = txtRegTitle.Text.Trim();
    cmd2.Parameters.Add("@User_OtherEmail", SqlDbType.VarChar).Value = txtOtherEmail.Text.Trim();
    cmd2.Parameters.Add("@User_PhoneNo", SqlDbType.VarChar).Value = txtRegCode1.Text;
    cmd2.Parameters.Add("@User_MobileNo", SqlDbType.VarChar).Value = txtRegCode2.Text;
    cmd2.Parameters.Add("@User_FaxNo", SqlDbType.VarChar).Value = txtRegCode3.Text;
    cmd2.Connection = SqlCon;
    try
    {
       SNo.Direction = System.Data.ParameterDirection.Output;
       cmd2.ExecuteScalar();
       string VendorID = "VEN" + cmd2.Parameters["@SNo"].Value.ToString();
    }
    finally
    {
    string url = "../CompanyBasicInfo.aspx?Parameter=" + Server.UrlEncode(" + VendorID + ");
    ClientScript.RegisterStartupScript(this.GetType(), "callfunction", "alert('Login created successfully');window.location.href = '" + url + "';", true);
     SqlCon.Close();
     }
    }
于 2012-09-01T10:10:40.603 回答