Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 923
  • Last Modified:

DBNull.Value for a string OdbcParameter

Dear experts,

I want to execute a stored procedure, and some of the string parameters may be empty. Instead of passing to the stored procedure an empty string for saving in my database, I want to pass a DBNull.Value. I tried the following:
                    parameters[3] = new OdbcParameter("pOrigValue", (oldFieldsValues[ctrl.ID] == string.Empty) ? DBNull.Value : oldFieldsValues[ctrl.ID]);
But I am getting an error:
error CS0173: Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'string'
Could you please tell me how I could do it correctly?
Thanks.
0
tantormedia
Asked:
tantormedia
  • 3
  • 3
1 Solution
 
käµfm³d 👽Commented:
Have you tried:
parameters[3] = new OdbcParameter("pOrigValue", (oldFieldsValues[ctrl.ID] == string.Empty) ? DBNull.Value.ToString() : oldFieldsValues[ctrl.ID]);

Open in new window

0
 
packratt_jkCommented:
try sending null instead of DBNull.Value - odbc should be smart enough to convert it for you.
0
 
tantormediaAuthor Commented:
kaufmed,

I tried it, and it stored an empty string instead of null .
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
tantormediaAuthor Commented:
packratt_jk,

This also stores an empty string.
0
 
käµfm³d 👽Commented:
The problem is with your use of the ternary operator. "?:" needs to have compatible types on either side of ":" and as indicated by your error, there is no implicit conversion between string and DBNull.Value. Try breaking the logic into an if/else statement instead.
if (oldFieldsValues[ctrl.ID] == string.Empty)
{
    parameters[3] = new OdbcParameter("pOrigValue", DBNull.Value);
}
else
{
    parameters[3] = new OdbcParameter("pOrigValue", oldFieldsValues[ctrl.ID]);
}

Open in new window

0
 
tantormediaAuthor Commented:
Thanks.
0
 
käµfm³d 👽Commented:
NP. Glad to help  :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now