简体   繁体   English

过程或函数需要参数,该参数未提供。 可空列抛出异常

[英]Procedure or function expects parameter, which was not supplied. A nullable column throwing exception

I am creating/updating a record in sql db with a stored procedure. 我正在使用存储过程在sql db中创建/更新记录。 I am supplying around 30 parameters from my C# Data Access Layer. 我从C#数据访问层提供了大约30个参数。 sql table has all the columns null able apart from its primary key column. sql表具有除其主键列之外的所有列null。 here when i supply a null value to a null able column, it is throwing an exception of "Procedure or function 'spFullUpdate' expects parameter '@App1TAPAYears', which was not supplied.". 这里当我向null able列提供一个空值时,它抛出了一个异常“过程或函数”spFullUpdate'期望参数'@ App1TAPAYears',这是未提供的。“ In my c# code I can clearly see the column is supplied with a null value. 在我的c#代码中,我可以清楚地看到该列提供了一个空值。 Could please anyone tell me how I can rectify this issue. 可以请任何人告诉我如何纠正这个问题。 Following is my code snippet. 以下是我的代码段。 Setting value to the data object is as follow 设置数据对象的值如下

Dt.TimeAtPreviousAddressYears = int.TryParse(TimeatPreviousAddressYears.Text, out intOut) ? intOut : (int?)null;

following is the nullable property in my entity class 以下是我的实体类中的可空属性

        public int? TimeAtPreviousAddressYears { get; set; }

My data access layer adding parameter code is as follow 我的数据访问层添加参数代码如下

cmd.Parameters.Add("@App1TAPAYears", SqlDbType.Int).Value = dataObject.TimeAtPreviousAddressYears;
SqlDataAdapter da = new SqlDataAdapter(cmd);
                conn.Open();
                cmd.ExecuteNonQuery();

It can clearly be seen that the parameter is added and null value is supplied to a null able column but it still producing exception. 可以清楚地看到,添加了参数并将空值提供给nullable列,但它仍然产生异常。 Anyone's help will really be appreciated. 任何人的帮助将非常感激。

Kind Regardds 亲切的Regardds

nullable != DBNull.Value

So you can't pass (int?)null to the parameter value but instead pass DBNull.Value 所以你不能将(int?)null传递给参数值,而是传递DBNull.Value

Like: 喜欢:

if (dataObject.TimeAtPreviousAddressYears.HasValue) 
{ 
    cmd.Parameters.Add("@App1TAPAYears", SqlDbType.Int).Value =dataObject.TimeAtPreviousAddressYears;
}else
{
    cmd.Parameters.Add("@App1TAPAYears", SqlDbType.Int).Value = DBNull.Value;
}

This is a really annoying problem, which threw me out recently because I'd not come across it for a long time. 这是一个非常恼人的问题,最近我把它扔了出去,因为我很长时间没有遇到它。

I settled on adding this in my RepositoryBase class 我决定在我的RepositoryBase类中添加它

    protected object ValueOrNull(object value)
    {
        return value ?? DBNull.Value;
    }

and this this in my actual repo code for any optional items 这是我在任何可选项目的实际回购代码中

cmd.Parameters.Add(new SqlParameter("@VoucherCode", SqlDbType.VarChar)).Value = ValueOrNull(promo.VoucherCode);

You should try with 你应该试试

if (dataObject.TimeAtPreviousAddressYears.HasValue) 
{ 
    cmd.Parameters.Add("@App1TAPAYears", SqlDbType.Int).Value =dataObject.TimeAtPreviousAddressYears;
}else
{
    cmd.Parameters.Add("@App1TAPAYears", SqlDbType.Int).Value = (object)DBNull.Value;
}

The issue is that (as the error message indicates) the conditional expression needs identical types on both branches. 问题是(如错误消息所示)条件表达式在两个分支上都需要相同的类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 '程序或 function '...' 需要参数 '...',但未提供。 - 'Procedure or function '...' expects parameter '...', which was not supplied.' “过程或函数需要未提供的参数。” - “Procedure or function expects parameter which was not supplied.” 即使提供了参数,也会引发异常“过程或函数需要参数,但未提供。” - Exception “Procedure or function expects parameter, which was not supplied.” thrown even after supplying the parameter 过程或函数期望未提供的参数。 ExecuteNonQuery - Procedure or function expects parameter which was not supplied. ExecuteNonQuery ASPxGridView-“过程或函数“可删除”需要参数“ @id”,但未提供。” - ASPxGridView - “Procedure or function 'deletetable' expects parameter '@id', which was not supplied.” 我不断收到这个“'Procedure or function'sp_UPDATEAdmin'需要参数'@Email',但没有提供。' - I keep getting this "'Procedure or function 'sp_UPDATEAdmin' expects parameter '@Email', which was not supplied.' SQL server stored proc“过程或函数需要参数,这是未提供的。” - SQL server stored proc “Procedure or function expects parameter, which was not supplied.” 过程或函数“uspExportGetMailinfoTest”需要未提供的参数“@CUSTOMER”。 我错过了什么? - Procedure or function 'uspExportGetMailinfoTest' expects parameter '@CUSTOMER', which was not supplied. What am I missing? 我如何解决此过程或函数“SelectFollow”需要未提供的参数“@userid”。 - How do i resolve this procedure or function 'SelectFollow' expects parameter '@userid', which was not supplied.'? 我收到错误消息“过程或函数'stSelect'期望参数'@stNo',但未提供。” - I get error “Procedure or function 'stSelect' expects parameter '@stNo', which was not supplied.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM