2012-08-15 153 views
0

我正在使用ASP.NET 4.0和SQL Server 2008 R2.I想要使用存儲過程爲「用戶註冊」網頁插入數據。每當我插入數據,錯誤顯示爲「過程或函數'usp_User_Info2'期望參數'@UserName',這是沒有提供」。但我輸入除SNo(標識列)和Vendor_ID(計算列)以外的所有必填字段。這個解決方案?而且我希望插入後在消息框中顯示「Vendor_ID」。過程或函數'usp_User_Info2'需要參數'@UserName',它沒有提供

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(); 
     }   
     } 
+1

你爲什麼要存儲確認的密碼?要麼匹配,要麼不匹配。並閱讀密碼散列。 – podiluska 2012-08-15 09:02:00

回答

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(); 
+0

你只回答了一半問題(無論如何參數問題+1) – Manatherin 2012-08-15 08:46:27

+0

不完全確定另一半是什麼。 「我如何從存儲過程獲取標識列?」也許 – 2012-08-15 08:48:39

+0

AS Manatherin說,你只回答我的第一個問題。謝謝。我如何在消息框中顯示「Vendor_ID」?它不是標識列? – Hari 2012-08-15 10:03:42

相關問題