Setting a parameter to null

Results 1 to 2 of 2

Thread: Setting a parameter to null

  1. #1
    Join Date
    Dec 1969

    Default Setting a parameter to null

    &nbsp;<BR> This may be an ADO.NET question, but it is the C# syntax that seems to be the problem. I am using a stored proc to insert into MS SQL. I want to be able to allow the user to check a gender radio button - or choose none if they prefer. <BR><BR>None = a null in the database. The database column is set to allow nulls. If I execute the SP from query analyzer with a null as the parm it works OK. How do I set the parameter to null in code? If I set it to null (no quotes) I get an error saying "Procedure &#039;p_insertRider&#039; expects parameter &#039;@gender&#039;, which was not supplied." <BR><BR>Here is the code I am using...<BR>if(rbMale.Checked)<BR>objParam = objCmd.Parameters.Add("@gender", "M");<BR>else if(rbFemale.Checked)<BR>objParam = objCmd.Parameters.Add("@gender", "F");<BR>else<BR>objParam = objCmd.Parameters.Add("@gender", null);<BR><BR>If I check either the M or F radio button, the insert suceeds. <BR> <BR>

  2. #2

    Default RE: Setting a parameter to null

    Use DBNull.Value

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts