0

我正在使用 ASP.NET 4.0 和 SQL Server 2008 R2。我想使用“用户注册”网页的存储过程插入数据。每当我插入数据时,错误显示为“过程或函数'usp_User_Info2'需要参数'@UserName',未提供”。但我输入了除SNo(身份列)和Vendor_ID(计算列)之外的所有必填字段。什么解决方案?而且我还希望插入后在消息框中显示“Vendor_ID”。

    CREATE TABLE User_Info2
    (
     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()
    )

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[usp_User_Info2]
    @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_Info2 (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)
    END

    protected void BtnUserNext_Click(object sender, EventArgs e)
    {
        SqlConnection SqlCon = new SqlConnection(GetConnectionString());
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "usp_User_Info2";
        cmd.Parameters.Add("@User_Email", SqlDbType.VarChar).Value = txtUserName.Text.Trim();
        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.Trim();
        cmd.Parameters.Add("@User_LastName",SqlDbType.VarChar).Value = txtRegLName.Text.Trim();
        cmd.Parameters.Add("@User_Title",SqlDbType.VarChar).Value = txtRegTitle.Text.Trim();
        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.Trim();
        cmd.Parameters.Add("@User_FaxNo", SqlDbType.VarChar).Value = txtRegFax.Text.Trim();
        cmd.Connection = SqlCon;
        try
        {
            SqlCon.Open();
            cmd.ExecuteScalar();
        }
        finally
        { string url = "../CompanyBasicInfo.aspx?Parameter=" + Server.UrlEncode
          ("+ Vendor_ID  +"); 
              ClientScript.RegisterStartupScript(this.GetType(), "callfunction",
              "alert('Login created successfully for "+ Vendor_ID +"');
              window.location.href = '"  + url + "';", true); 
              SqlCon.Close();
        }        
       }
4

1 回答 1

2
   cmd.Parameters.Add("@User_Email", SqlDbType.VarChar).Value = txtUserName.Text.Trim();

应该:

   cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = txtUserName.Text.Trim();

要获取标识列,请将其添加到存储过程的底部:

SELECT SCOPE_IDENTITY() 'Id'

并从中获取值ExecuteScalar

Vendor_ID = "VEN" + ((int)cmd.ExecuteScalar()).ToString();
于 2012-08-15T08:41:10.457 回答