PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 5.00 average.

Passing NULL values to SqlCommand.Parameters.AddWithValue

 
 
Giammarco
Guest
Posts: n/a
 
      18th Sep 2005
Hi All,

I have the following code:

SqlCommand sqlCmd = new SqlCommand(sqlStatment, dbConn);
sqlCmd.Parameters.AddWithValue("@Name", name);
sqlCmd.Parameters.AddWithValue("@Surname", surname);

If surname is NULL I get the following error message:

Parameterized Query xyz expects parameter Surname which was not
supplied.

Do you know any workaround? I basically would like to pass NULL values
to the SQL statment, instead of writing different SQL statment
depending on what values the user provides.

Thanks,
Giammarco

 
Reply With Quote
 
 
 
 
em00guy
Guest
Posts: n/a
 
      18th Sep 2005
This may work
C# Solution

sqlCmd.Parameters[0].IsNullable = true;
sqlCmd.Parameters[1].IsNullable = true;

let me know

 
Reply With Quote
 
Miha Markic [MVP C#]
Guest
Posts: n/a
 
      18th Sep 2005
pass DBNull.Value instead

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

"Giammarco" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi All,
>
> I have the following code:
>
> SqlCommand sqlCmd = new SqlCommand(sqlStatment, dbConn);
> sqlCmd.Parameters.AddWithValue("@Name", name);
> sqlCmd.Parameters.AddWithValue("@Surname", surname);
>
> If surname is NULL I get the following error message:
>
> Parameterized Query xyz expects parameter Surname which was not
> supplied.
>
> Do you know any workaround? I basically would like to pass NULL values
> to the SQL statment, instead of writing different SQL statment
> depending on what values the user provides.
>
> Thanks,
> Giammarco
>



 
Reply With Quote
 
Giammarco
Guest
Posts: n/a
 
      18th Sep 2005
Thanks guys,
I've solved in the following way:

SqlCommand sqlCmd = new SqlCommand(sqlStatment, dbConn);
sqlCmd.Parameters.AddWithValue("@Name", name);
sqlCmd.Parameters.AddWithValue("@Surname", surname);
// I have more parameters here

foreach (SqlParameter Parameter in sqlCmd.Parameters)
{
if (Parameter.Value == null)
{
Parameter.Value = DBNull.Value;
}
}

Hope it's the best way to do this!

Regards,
Giammarco Schisani

 
Reply With Quote
 
Mark Ashton
Guest
Posts: n/a
 
      21st Sep 2005
the parameter IsNullable is an unused DesignTime only property

--
This posting is provided "AS IS", with no warranties, and confers no rights.
Please do not send email directly to this alias. This alias is for newsgroup
purposes only.

"em00guy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> This may work
> C# Solution
>
> sqlCmd.Parameters[0].IsNullable = true;
> sqlCmd.Parameters[1].IsNullable = true;
>
> let me know
>



 
Reply With Quote
 
Giammarco
Guest
Posts: n/a
 
      25th Sep 2005
Hey,
Just an update on the issue: I am using the following workaround:

private SqlMoney? money;
public SqlMoney? Money
{
get { return money; }
set { money = value; }
}

if (Money != null)
{
sqlCmd.Parameters.AddWithValue("@Money", Money);
}
else
{
sqlCmd.Parameters.AddWithValue("@Money", DbNull.Value);
}


Hope it helps!
Giammarco

 
Reply With Quote
 
New Member
Join Date: Jan 2007
Posts: 1
 
      5th Jan 2007
sqlCmd.Parameters.AddWithValue("@Surname", ((surname == null) ? (object)DBNull.Value ? (object)surname));
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parameters.AddWithValue refreshing values Roy Gourgi Microsoft C# .NET 6 5th Dec 2005 05:17 PM
Passing output parameters to SQL Server stored procedures through an SqlCommand object's Parameters collection Mark Rae Microsoft ADO .NET 8 26th Sep 2005 04:28 PM
sqlcommand parameters with null gv Microsoft ADO .NET 2 1st Apr 2005 05:42 PM
Output Parameters null when using SqlCommand.ExecuteReader()...? mo Microsoft ADO .NET 6 7th Feb 2004 09:35 PM
Passing Null Values via a SQLCommand.Parameter Thomas Ficker Microsoft ADO .NET 1 18th Aug 2003 06:11 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:09 AM.