开发者

SQL Server Express ... Stored Proceedure "Null Parameter" error

开发者 https://www.devze.com 2023-02-03 21:57 出处:网络
Using Visual Studio 2010 I am putting together a quick .NET application that needs to record some data.I am using the built in facility of SQL Server Express and have created a Stored Proceedure for t

Using Visual Studio 2010 I am putting together a quick .NET application that needs to record some data. I am using the built in facility of SQL Server Express and have created a Stored Proceedure for the data insertion. I have setup the database table to allows Nulls on the field in question, but am generating a runtime exception "Procedure or function 'XYZ' expects parameter '@ErrorCode', which was not supplied."

I have tried creating the SqlParameters the "plain way":

command.Parameters.Add(new SqlParameter("ErrorCode", state.ErrorCode));

I have also tried creating them by setting more of the properties:

SqlParameter errorCodeParam = new SqlParameter("ErrorCode", SqlDb开发者_运维技巧Type.Int);
errorCodeParam.IsNullable = true;
errorCodeParam.Value = state.ErrorCode;
command.Parameters.Add( errorCodeParam );

My ErrorCode property on the state object is simply a nullable int .... "int?" with an assigned value of "null". As the table column for this field supposedly allows nulls I'm a bit stumpped on why it's complaining.

Thoughts or ideas? Thanks.


After some additional experimentation I found the answer (thought I'd pass it along in case someone else runs into the same situation). I have been using statements like the following:

command.Parameters.Add(new SqlParameter("ErrorCode", (state.ErrorCode != null ? state.ErrorCode : null)));

Whereby I am checking the value of a nullable int (int?) to see if has a value, and if not, assigning a C# null to a SqlParameter .... which wants a DBNull.Value instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消