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

  • SEARCH
  • Home
  • 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 8035533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:20:31+00:00 2026-06-05T02:20:31+00:00

I have the following bit of code to set a parameter that will be

  • 0

I have the following bit of code to set a parameter that will be used in an INSERT statement to set a VARCHAR column in a SQL Server database. My value object (named ilo) has a property called Description that gets initialized to String.Empty, and then either gets set to some value read from XML, or if that XML element is empty it just stays as String.Empty.

So when inserting into the database, if the property is still set to String.Empty, I’d like to have it insert a null value.

database.AddInParameter(cmd, "@description", DbType.String, 
                           (ilo.Description.Equals(string.Empty)) ?
                            DBNull.Value :
                            ilo.Description);

So basically I’m saying, if ilo.Description equals string.empty, set the parameter to DBNull.Value, otherwise set it to ilo.Description.

This gives the following error in Visual Studio…

Error 141 Type of conditional expression cannot be determined because there is no implicit conversion between ‘System.DBNull’ and ‘string’

Why?

The curious part is that I can do the following with no error, which should be exactly the same thing as using inline conditional syntax like above!?!

if(ilo.Description.Equals(string.Empty))
{
    database.AddInParameter(cmd, "@description", DbType.String, DBNull.Value);
}
else
{
    database.AddInParameter(cmd, "@description", DbType.String, ilo.Description);
}

I searched other posts, and found the one below, but it doesn’t really answer my question.

EntLib Way to Bind "Null" Value to Parameter

I’m more interested in WHY, because the obvious workaround is to just use an if/else statement instead of the inline (ternary) syntax?

There’s sort of an answer at this link, but I’d like a better explanation because it seems to be BS to me that this doesn’t work; I’d call it a bug!

http://msdn.microsoft.com/en-us/library/ty67wk28.aspx

EDIT: 3/30/2021, it’s been 9 years since I wrote this question. Finally as of C# 9, this issue is fixed. Ternary expressions now support mixed types so long as they can be implicitly converted.

  • 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-06-05T02:20:32+00:00Added an answer on June 5, 2026 at 2:20 am

    This is a common error people receive when using the conditional operator. To fix it, simply cast one or both of the results to a common base type.

    ilo.Description.Equals(string.Empty) 
         ? (object)DBNull.Value 
         : ilo.Description
    

    The issue is revealed in the error message you saw.

    Type of conditional expression cannot be determined because there is no implicit conversion between ‘System.DBNull’ and ‘string’

    A string is not a DBNull, and a DBNull is not a string. Therefore, the compiler cannot determine the type of the expression. By using a cast to a common base type (in this case, object), you create a scenario where the compiler can then determine that string is also convertible to object, so the type of the expression can be determined as object, which also nicely fits with what your line of code also expects as the DbParameter argument.

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

Sidebar

Related Questions

I have the following bit of code which runs a SQL statement: int rowsEffected
I have the following code that takes an SQL statement (string), loads the results
I have the following bit of code that worked as expected before we upgraded
I have the following bit of code, I expect that given cstdio is included
I have the following bit of code that is timing out when I pass
How do I change the following bit of code so that I only have
I have the following bit of code that modifies an observable collection of 'screens'
I have the following bit of code in a method called by clicking the
I have the following bit of code: protected function onEnterFrame(e:Event):void { var diff:Number; //
I have the following bit of code, simply: $(function() { $('a.add-photos-link').live('click', function(e) { $(this).colorbox({

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.