cyclonebri Posted October 1, 2003 Posted October 1, 2003 Hi everybody, I am having an issue in C#.net/ADO.net with passing a null integer value to a stored procedure. I use the following: I have a data adapter and dataset that are pointing and filling from the SQLCommand object that is properly pointing to the database stored procedure with a connection that is working properly. The dataset is returning exactly what I need and working fine, except for the null int values. For instance, I need to send a value of null for the parameter "@UserId" (which is an integer value or <<Null>>). //I have tried this: this.cmdSql1.Parameters["@UserId"].Value = null; //which returns the error 'The stored procedure is expecting a //parameter for the value '@UserId' which was not supplied //so then I tried this: int x = Convert.ToInt32(null); this.cmdSql1.Parameters["@UserID"].Value = x; //this works, but it sets the value to 0, and since the database //values are all <<Null>> for the items I want to return, it does //not return what i need it to return. Is there a trick to passing the <<Null>> integer value to the sql 7 database that I am missing? Any suggestions would be greatly appreciated..Thanks! Thanks in advance, Brian Quote
*Gurus* Derek Stone Posted October 1, 2003 *Gurus* Posted October 1, 2003 .NET value types (integers, bytes, booleans, etc.) cannot be null. SQL Server parameters and the .NET object that wraps them, SqlParameter, can be however, since they are wrappers to the underlying types. You'll need to set the parameters IsNullable property to true prior to passing in a null value. Dim p As SqlParameter = New SqlParameter() p.SqlDbType = SqlDbType.Int p.IsNullable = True p.Value = Nothing command.Parameters.Add(p) Quote Posting Guidelines
cyclonebri Posted October 1, 2003 Author Posted October 1, 2003 Ok, your explanation makes perfect sense to me, however I cannot seem to get any methods to work. //I have now tried: System.Data.SqlClient.SqlParameter x = new SqlParameter(); x.SqlDbType = System.Data.SqlDbType.Int; x.ParameterName = "@UserId"; x.IsNullable = true; //not listed in intellisense but no error x.Value = null; //nothing is not a keyword in C# cmdSql1.Parameters.Add(x); //this returns the error : Procedure or Function //myStoredProcedure has too many arguments specified. //So then I tried : this.cmdSql1.Parameters["@UserId"].SqlDbType = System.Data.SqlDbType.Int; this.cmdSql1.Parameters["@UserId"].IsNullable = true; this.cmdSql1.Parameters["@UserId"].Value = null; //which again gives me the message: //Procedure myStoredProcedure expects parameter '@UserId', //which was not supplied. Is there something else I should try? Is there another way to arrange what you have given me that I am missing? I seem to be having no luck with this. Thanks again for your help! Brian Quote
cyclonebri Posted October 1, 2003 Author Posted October 1, 2003 I have found a solution, for anyone who might search and find this thread in the future. Although I'm not sure this is the best solution, I found it is possible to set the value of the parameter to DBNull.Value, without having to mess with any IsNullable or anything else. this.cmdSql1.Parameters["@UserId"].Value = DBNull.Value; This passes the correct value of Null to the storedprocedure. Thanks, Brian Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.