?
Solved

DBNull.Value for a string OdbcParameter

Posted on 2010-09-10
7
Medium Priority
?
906 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tantormedia
ID: 33649778
packratt_jk,

This also stores an empty string.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

764 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