Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6819229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:16:51+00:00 2026-05-26T21:16:51+00:00

I have a SQL command that I’ve been asked to modify, and I’m having

  • 0

I have a SQL command that I’ve been asked to modify, and I’m having some troubles with the fact that what I’m passing to the SQL can now be null. If I’m passing a value, I can rely on the columnName = @parameterName in the SQL, but with NULL, I can’t pass null or DBNull and have it correctly resolve.

Here’s the SQL pseudocode:

SELECT
  Columns
FROM
  ClientSetup
WHERE
  Client_Code = @ClientCode AND
  Package_Code = @PackageCode AND
  Report_Code = @ReportCode

The problem is that now @ReportCode can validly be NULL. In my C# code where I set up the SqlCommand, I can put in:

cmd.Parameters.Add("@ReportCode", SqlDBType.VarChar, 5).Value = reportType;
  //reportType is a string, which can be null

But, if reportType is null, I need to use Report_Code IS NULL in the SQL, rather than Report_Code = @reportCode.

The solution I’ve found is to change the last where clause to the following:

((@ReportCode IS NULL AND Report_Code IS NULL) OR Report_Code = @ReportCode)

and the parameter phrase to

cmd.Parameters.Add("@ReportCode", SqlDBType.VarChar, 5).Value = string.IsNullOrEmpty(reportType) ? System.DBNull : reportType;

What this does works, but I was wondering if anyone knew of a cleaner or better way to handle nullable parameters when passing things to SQL from .NET code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:16:52+00:00Added an answer on May 26, 2026 at 9:16 pm

    The short answer is that no, the SqlClient API requires you to pass in a DbNull.Value for NULL parameter values.

    But I have some doubts about how you treat NULLs. For one you use string.IsNullOrEmpty which means that you treat the emtpy string as a NULL. This is questionable, there may be legitimate empty string values in the database.

    My second concern is the logic of matching NULLs in the database. More often than not passing in a NULL parameter means that the request is interested in any value, not specifically in NULL values. I’m not saying your logic of matching NULL parameters to NULL values is flawed, I just want to make sure you know what you’re doing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an UPDATE sql command that modifies a Date/Time field in a particular
I have created a function that takes a SQL command and produces output that
We have an application that installs SQL Server Express from the command line and
How can I execute an SQL command through a shell script so that I
I want to find a sql command or something that can do this where
I am having an asynchronous call to beingexecutereader method of sql command, that a
I have an entity framework data context and a SQL command that both need
Have a SQL command with FOR XML that returns a single long XML as
I have to make an SQL command that look for a String in the
I have a SQL Insert Into command that works in normal conditions. That means

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.