我收到了错误:
过程或函数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();
}
}
答案 0 :(得分:3)
@SNo
参数ExecuteScalar
调用它。@Vendor_ID
输出参数的值。答案 1 :(得分: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
}
答案 2 :(得分: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#代码。
答案 3 :(得分:0)
经过所有这些伟大思想的回答,我无法相信你仍然被卡住了。以下是我的建议
在您的PROC中
'maintain this in your parameter
@Vendor_ID VARCHAR(10) OUTPUT,
更新以选择VendorID
进入@VendorID
。见第二行
SET @SNo = Scope_Identity()
SELECT @Vendor_ID=VendorID From User_Info3 WHERE SNo = @SNo
在C#中,请勿致电ExecuteScalar
使用ExecuteNonQuery
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
。希望这有帮助吗?
答案 4 :(得分: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();
}
}