-2

所以在这里我正在创建一个使用 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

我很困惑,有什么错误,我在这里缺少什么的任何建议?

4

2 回答 2

1

错误消息告诉您缺少存储过程的参数,具体而言@CreatedDate

您需要添加:

cmd.Parameters.Add(new SqlParameter("CreatedDate", DateTime.Now));

注意:您必须提供您缺少的所有其他参数,否则您将遇到相同的错误。

于 2013-06-17T07:05:24.873 回答
1

异常文本非常清楚:

过程或函数“spMSTransaction_Insert”需要未提供的参数“@CreatedDate”

您的存储过程需要您不提供的这些参数...

@CreatedDate datetime,
@CreatedBy varchar(50),
@ModifiedDate datetime,
@ModifiedBy varchar(50)

...并且您正在传递一个它不期望的参数。

cmd.Parameters.Add(new SqlParameter("Username", Helper.GetUserName()));
于 2013-06-17T07:05:31.247 回答