Solved

DBNull.Value for a string OdbcParameter

Posted on 2010-09-10
7
889 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 75

Expert Comment

by:käµfm³d 👽
ID: 33648952
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
ID: 33649004
try sending null instead of DBNull.Value - odbc should be smart enough to convert it for you.
0
 

Author Comment

by:tantormedia
ID: 33649714
kaufmed,

I tried it, and it stored an empty string instead of null .
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:tantormedia
ID: 33649778
packratt_jk,

This also stores an empty string.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 33649895
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
ID: 33649910
Thanks.
0
 
LVL 75

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to Get total minutes between two hours 4 44
Calling stored proc in EDMX 11 23
bulid json format 3 46
How would you add MULTITHREADING to the attached C# code? 4 51
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

911 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

19 Experts available now in Live!

Get 1:1 Help Now