如何在asp.net中设置过程或函数中的参数错误
本文关键字:函数 错误 参数 过程 设置 asp net | 更新日期: 2023-09-27 18:18:51
所以这里我创建了一个使用sql server数据库连接的web应用程序,这里我有一个存储过程:
ALTER PROC [dbo].[spMSTransaction_Insert]
@OrgID int,
@SiteID int,
@TransactionCategoryID int,
@TransactionDesc varchar(300),
@IsActive bit,
@CreatedDate datetime,
@CreatedBy varchar(50),
@ModifiedDate datetime,
@ModifiedBy varchar(50)
AS
SET NOCOUNT ON
SET XACT_ABORT ON
BEGIN TRAN
INSERT INTO [dbo].[MSTransaction]
(
[OrgID],
[SiteID],
[TransactionCategoryID],
[TransactionDesc],
[IsActive],
[CreatedDate],
[CreatedBy],
[ModifiedDate],
[ModifiedBy]
)
SELECT @OrgID,
@SiteID,
@TransactionCategoryID,
@TransactionDesc,
@IsActive,
@CreatedDate,
@CreatedBy,
@ModifiedDate,
@ModifiedBy
COMMIT
后面的代码是:
protected void btnSave_Click(object sender, EventArgs e)
{
logID = Helper.GetLogID();
try
{
LKLog.Write(logID, "Transaction.aspx.cs", "btnSave_Click", "Start Method", string.Empty, Helper.GetUserName());
orgID = 1;
siteID = 1;
if (string.IsNullOrEmpty(hfOrgID.Value))
{
clsTransaction.InsertTransaction(Helper.LogID, Helper.OrgID, Helper.SiteID,
CommonFunctions.StringToInt(ddlTransactionCategory.SelectedValue), txtTransactionDesc.Text, true);
}
else
{
int transactionID = CommonFunctions.StringToInt(hfOrgID.Value);
clsTransaction.UpdateTransaction(logID, orgID, siteID, 1,
CommonFunctions.StringToInt(ddlTransactionCategory.SelectedValue), txtTransactionDesc.Text, true);
}
string message = string.Format(Constants.SaveMessage, "Transaction");
LKLog.Write(logID, "Transaction.aspx.cs", "btnSave_Click", "End Method", string.Empty, Helper.GetUserName());
ScriptManager.RegisterStartupScript(this, this.GetType(), "Info", "alert('" + message + "');window.location ='Transaction.aspx';", true);
}
catch (Exception ex)
{
LKLog.Write(logID, "Transaction.aspx.cs", "btnSave_Click", ex.StackTrace, ex.Message, Helper.GetUserName());
throw;
}
}
,类如下:
public static void InsertTransaction(decimal logID, int orgID, int siteID, int TransactionCategoryID, string TransactionDesc, bool isActive)
{
LKLog.Write(logID, "clsTransaction.cs", "InsertTransaction", "Start Method", string.Empty, Helper.GetUserName());
using (SqlConnection conn = new SqlConnection(CommonFunctions.GetAppDBConnection(Constants.AppID, Constants.TMDDBConnection)))
{
try
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "spMSTransaction_Insert";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("orgID", orgID));
cmd.Parameters.Add(new SqlParameter("siteID", siteID));
cmd.Parameters.Add(new SqlParameter("TransactionCategoryID", TransactionCategoryID));
cmd.Parameters.Add(new SqlParameter("TransactionDesc", TransactionDesc));
cmd.Parameters.Add(new SqlParameter("IsActive", siteID));
cmd.Parameters.Add(new SqlParameter("Username", Helper.GetUserName()));
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
LKLog.Write(logID, "clsTransaction.cs", "InsertTransaction", ex.StackTrace, ex.Message, Helper.GetUserName());
throw;
}
finally
{
conn.Close();
}
}
LKLog.Write(logID, "clsTransaction.cs", "InsertTransaction", "End Method", string.Empty, Helper.GetUserName());
}
但是当我尝试插入数据时,错误指向
过程或函数'spMSTransaction_Insert'期望参数'@CreatedDate',该参数未提供。
和错误行是(其中第73行被标记为红色):
Line 71: {
Line 72: LKLog.Write(logID, "clsTransaction.cs", "InsertTransaction", ex.StackTrace, ex.Message, Helper.GetUserName());
Line 73: throw;
Line 74: }
Line 75: finally
我很困惑,是什么错误,我在这里错过了什么建议?
错误消息告诉您,您缺少存储过程的参数,具体为@CreatedDate
你需要添加:
cmd.Parameters.Add(new SqlParameter("CreatedDate", DateTime.Now));
注意:您必须提供您缺少的所有其他参数,否则您将遇到相同的错误。
异常文本非常清楚:
过程或函数'spMSTransaction_Insert' 期望参数'@CreatedDate',该参数未提供。
您的存储过程需要这些您没有提供的参数…
@CreatedDate datetime,
@CreatedBy varchar(50),
@ModifiedDate datetime,
@ModifiedBy varchar(50)
…你传递了一个它不期望的参数
cmd.Parameters.Add(new SqlParameter("Username", Helper.GetUserName()));