Solved

DBNull.Value for a string OdbcParameter

Posted on 2010-09-10
7
894 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project

726 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