Solved

DBNull.Value for a string OdbcParameter

Posted on 2010-09-10
7
888 Views
Last Modified: 2012-05-10
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
Comment
Question by:tantormedia
  • 3
  • 3
7 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Have you tried:
parameters[3] = new OdbcParameter("pOrigValue", (oldFieldsValues[ctrl.ID] == string.Empty) ? DBNull.Value.ToString() : oldFieldsValues[ctrl.ID]);

Open in new window

0
 
LVL 3

Expert Comment

by:packratt_jk
Comment Utility
try sending null instead of DBNull.Value - odbc should be smart enough to convert it for you.
0
 

Author Comment

by:tantormedia
Comment Utility
kaufmed,

I tried it, and it stored an empty string instead of null .
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:tantormedia
Comment Utility
packratt_jk,

This also stores an empty string.
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:tantormedia
Comment Utility
Thanks.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
NP. Glad to help  :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now