473,560 Members | 3,030 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

converting system.dbnull to system.string

HI all,

I have a column value returned to a string variable in my c# app. But the
return type is of system.dbnull. How can I convert that to a system.sting

Thanks
Robert
Dec 2 '06 #1
9 34670
Hi Robert,

object value = row["column"];

string str = (value is DBNull) ? null : (string) value;

--
Dave Sexton

"Robert Bravery" <me@u.comwrot e in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
HI all,

I have a column value returned to a string variable in my c# app. But the
return type is of system.dbnull. How can I convert that to a system.sting

Thanks
Robert


Dec 2 '06 #2
Hello Robert,

Just call ToString() on the value returned. DbNull.String() will return string.empty.

Alternatively, if you want null returned then you'll have to convert if yourself.
A little helper function... well helps:

public static object ReadNullIfDbNul l(IDataReader reader, int column)
{
object value = reader.GetValue (column);
return value == DbNull.Value ? null : value;
}

....

string column = (string)ReadNul lIfDbNull(reade r, 0);

Cheers,
Stefan Delmarco

http://www.fotia.co.uk
HI all,

I have a column value returned to a string variable in my c# app. But
the return type is of system.dbnull. How can I convert that to a
system.sting

Thanks
Robert


Dec 2 '06 #3
HI Stefan,

Thanks for your reply.
I tried that, as in:
string dbnString;
dbnString = cuser.FirstName .ToString();

But still get the error:
- cuser.FirstName .ToString() 'this.cuser.Fir stName' threw an exception of
type 'System.Data.St rongTypingExcep tion' string
{System.Data.St rongTypingExcep tion}

I'm new to this, so no guessing why I am confused now

Thanks
Robert
"Stefan Delmarco" <St************ @fotia.co.ukwro te in message
news:c0******** *************** ***@news.micros oft.com...
Hello Robert,

Just call ToString() on the value returned. DbNull.String() will return
string.empty.
>
Alternatively, if you want null returned then you'll have to convert if
yourself.
A little helper function... well helps:

public static object ReadNullIfDbNul l(IDataReader reader, int column)
{
object value = reader.GetValue (column);
return value == DbNull.Value ? null : value;
}

...

string column = (string)ReadNul lIfDbNull(reade r, 0);

Cheers,
Stefan Delmarco

http://www.fotia.co.uk
HI all,

I have a column value returned to a string variable in my c# app. But
the return type is of system.dbnull. How can I convert that to a
system.sting

Thanks
Robert



Dec 2 '06 #4
Hi Robert,

When using a Typed DataSet you must check first if the column contains a
DBNull value:

string dbnString;

if (cuser.IsFirstN ameNull())
dbnString = null;
else
dbnString = cuser.FirstName ;

--
Dave Sexton

"Robert Bravery" <me@u.comwrot e in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
HI Stefan,

Thanks for your reply.
I tried that, as in:
string dbnString;
dbnString = cuser.FirstName .ToString();

But still get the error:
- cuser.FirstName .ToString() 'this.cuser.Fir stName' threw an exception of
type 'System.Data.St rongTypingExcep tion' string
{System.Data.St rongTypingExcep tion}

I'm new to this, so no guessing why I am confused now

Thanks
Robert
"Stefan Delmarco" <St************ @fotia.co.ukwro te in message
news:c0******** *************** ***@news.micros oft.com...
>Hello Robert,

Just call ToString() on the value returned. DbNull.String() will return
string.empty.
>>
Alternativel y, if you want null returned then you'll have to convert if
yourself.
>A little helper function... well helps:

public static object ReadNullIfDbNul l(IDataReader reader, int column)
{
object value = reader.GetValue (column);
return value == DbNull.Value ? null : value;
}

...

string column = (string)ReadNul lIfDbNull(reade r, 0);

Cheers,
Stefan Delmarco

http://www.fotia.co.uk
HI all,

I have a column value returned to a string variable in my c# app. But
the return type is of system.dbnull. How can I convert that to a
system.sting

Thanks
Robert




Dec 2 '06 #5
Hello Robert,

From the code snippet it looks like you're using a strongly typed data set.
I was assuming you were iterating over an IDataReader. Have a look at the
XSD for your dataset (or have a look at it in the DataSet designer). I bet
that the "NullValue" property for the FirstName column is "(Throw)". Look
for msprop:nullValu e="_throw" in the XSD.

More information here: http://msdn2.microsoft.com/en-us/lib...az(vs.71).aspx

Cheers,
Stefan Delmarco

http://www.fotia.co.uk
HI Stefan,

Thanks for your reply.
I tried that, as in:
string dbnString;
dbnString = cuser.FirstName .ToString();
But still get the error:
- cuser.FirstName .ToString() 'this.cuser.Fir stName' threw an
exception of
type 'System.Data.St rongTypingExcep tion' string
{System.Data.St rongTypingExcep tion}
I'm new to this, so no guessing why I am confused now

Thanks
Robert
"Stefan Delmarco" <St************ @fotia.co.ukwro te in message
news:c0******** *************** ***@news.micros oft.com...
>Hello Robert,

Just call ToString() on the value returned. DbNull.String() will
return
string.empty.
>Alternativel y, if you want null returned then you'll have to convert
if
yourself.
>A little helper function... well helps:

public static object ReadNullIfDbNul l(IDataReader reader, int column)
{
object value = reader.GetValue (column);
return value == DbNull.Value ? null : value;
}
...

string column = (string)ReadNul lIfDbNull(reade r, 0);

Cheers,
Stefan Delmarco
http://www.fotia.co.uk
>>HI all,

I have a column value returned to a string variable in my c# app.
But the return type is of system.dbnull. How can I convert that to a
system.stin g

Thanks
Robert

Dec 2 '06 #6
I should have added, there are 3 ways to configure your typed dataset's handling
of null values (per column):

- Throw an exception if a null is encountered (the default)
- Return null
- Return string.Empty

Your typed data set is using the default. .NET Reflector shows that this
is the only use of the StrongTypingExc eption type.

Cheers,
Stefan Delmarco

http://www.fotia.co.uk
Hello Robert,

From the code snippet it looks like you're using a strongly typed data
set. I was assuming you were iterating over an IDataReader. Have a
look at the XSD for your dataset (or have a look at it in the DataSet
designer). I bet that the "NullValue" property for the FirstName
column is "(Throw)". Look for msprop:nullValu e="_throw" in the XSD.

More information here:
http://msdn2.microsoft.com/en-us/lib...az(vs.71).aspx

Cheers,
Stefan Delmarco
http://www.fotia.co.uk
>HI Stefan,

Thanks for your reply.
I tried that, as in:
string dbnString;
dbnString = cuser.FirstName .ToString();
But still get the error:
- cuser.FirstName .ToString() 'this.cuser.Fir stName' threw an
exception of
type 'System.Data.St rongTypingExcep tion' string
{System.Data.S trongTypingExce ption}
I'm new to this, so no guessing why I am confused now
Thanks
Robert
"Stefan Delmarco" <St************ @fotia.co.ukwro te in message
news:c0******* *************** ****@news.micro soft.com...
>>Hello Robert,

Just call ToString() on the value returned. DbNull.String() will
return
string.empty .
>>Alternatively , if you want null returned then you'll have to convert
if
yourself.
>>A little helper function... well helps:

public static object ReadNullIfDbNul l(IDataReader reader, int
column)
{
object value = reader.GetValue (column);
return value == DbNull.Value ? null : value;
}
...
string column = (string)ReadNul lIfDbNull(reade r, 0);

Cheers,
Stefan Delmarco
http://www.fotia.co.uk
HI all,

I have a column value returned to a string variable in my c# app.
But the return type is of system.dbnull. How can I convert that to
a system.sting

Thanks
Robert

Dec 2 '06 #7
Thanks Dave and Stefan,
Im off to try youre suggestions
And yes, I am using a string typed dataset.

Robert

"Stefan Delmarco" <St************ @fotia.co.ukwro te in message
news:c0******** *************** ***@news.micros oft.com...
I should have added, there are 3 ways to configure your typed dataset's
handling
of null values (per column):

- Throw an exception if a null is encountered (the default)
- Return null
- Return string.Empty

Your typed data set is using the default. .NET Reflector shows that this
is the only use of the StrongTypingExc eption type.

Cheers,
Stefan Delmarco

http://www.fotia.co.uk
Hello Robert,

From the code snippet it looks like you're using a strongly typed data
set. I was assuming you were iterating over an IDataReader. Have a
look at the XSD for your dataset (or have a look at it in the DataSet
designer). I bet that the "NullValue" property for the FirstName
column is "(Throw)". Look for msprop:nullValu e="_throw" in the XSD.

More information here:
http://msdn2.microsoft.com/en-us/lib...az(vs.71).aspx

Cheers,
Stefan Delmarco
http://www.fotia.co.uk
HI Stefan,

Thanks for your reply.
I tried that, as in:
string dbnString;
dbnString = cuser.FirstName .ToString();
But still get the error:
- cuser.FirstName .ToString() 'this.cuser.Fir stName' threw an
exception of
type 'System.Data.St rongTypingExcep tion' string
{System.Data.St rongTypingExcep tion}
I'm new to this, so no guessing why I am confused now
Thanks
Robert
"Stefan Delmarco" <St************ @fotia.co.ukwro te in message
news:c0******** *************** ***@news.micros oft.com...
Hello Robert,

Just call ToString() on the value returned. DbNull.String() will
return

string.empty.

Alternativel y, if you want null returned then you'll have to convert
if

yourself.

A little helper function... well helps:

public static object ReadNullIfDbNul l(IDataReader reader, int
column)
{
object value = reader.GetValue (column);
return value == DbNull.Value ? null : value;
}
...
string column = (string)ReadNul lIfDbNull(reade r, 0);

Cheers,
Stefan Delmarco
http://www.fotia.co.uk
HI all,

I have a column value returned to a string variable in my c# app.
But the return type is of system.dbnull. How can I convert that to
a system.sting

Thanks
Robert


Dec 2 '06 #8
Hi Stefan,

Note that the last two options only work when a DataColumn is typed as a
string. A Typed DataColumn will not be able to return a null reference or
string.Empty for anything other than System.String.

--
Dave Sexton

"Stefan Delmarco" <St************ @fotia.co.ukwro te in message
news:c0******** *************** ***@news.micros oft.com...
>I should have added, there are 3 ways to configure your typed dataset's
handling of null values (per column):

- Throw an exception if a null is encountered (the default)
- Return null
- Return string.Empty

Your typed data set is using the default. .NET Reflector shows that this
is the only use of the StrongTypingExc eption type.

Cheers,
Stefan Delmarco

http://www.fotia.co.uk
>Hello Robert,

From the code snippet it looks like you're using a strongly typed data
set. I was assuming you were iterating over an IDataReader. Have a
look at the XSD for your dataset (or have a look at it in the DataSet
designer). I bet that the "NullValue" property for the FirstName
column is "(Throw)". Look for msprop:nullValu e="_throw" in the XSD.

More information here:
http://msdn2.microsoft.com/en-us/lib...az(vs.71).aspx

Cheers,
Stefan Delmarco
http://www.fotia.co.uk
>>HI Stefan,

Thanks for your reply.
I tried that, as in:
string dbnString;
dbnString = cuser.FirstName .ToString();
But still get the error:
- cuser.FirstName .ToString() 'this.cuser.Fir stName' threw an
exception of
type 'System.Data.St rongTypingExcep tion' string
{System.Data. StrongTypingExc eption}
I'm new to this, so no guessing why I am confused now
Thanks
Robert
"Stefan Delmarco" <St************ @fotia.co.ukwro te in message
news:c0****** *************** *****@news.micr osoft.com...
Hello Robert,

Just call ToString() on the value returned. DbNull.String() will
return

string.empt y.

Alternativel y, if you want null returned then you'll have to convert
if

yourself.

A little helper function... well helps:

public static object ReadNullIfDbNul l(IDataReader reader, int
column)
{
object value = reader.GetValue (column);
return value == DbNull.Value ? null : value;
}
...
string column = (string)ReadNul lIfDbNull(reade r, 0);

Cheers,
Stefan Delmarco
http://www.fotia.co.uk
HI all,
>
I have a column value returned to a string variable in my c# app.
But the return type is of system.dbnull. How can I convert that to
a system.sting
>
Thanks
Robert


Dec 2 '06 #9
DAve,
Thanks for this. It is good to know.
A lot to learn, especially when comming from a very differnt programming
background

Robert

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:%2******** **********@TK2M SFTNGP05.phx.gb l...
Hi Stefan,

Note that the last two options only work when a DataColumn is typed as a
string. A Typed DataColumn will not be able to return a null reference or
string.Empty for anything other than System.String.

--
Dave Sexton

"Stefan Delmarco" <St************ @fotia.co.ukwro te in message
news:c0******** *************** ***@news.micros oft.com...
I should have added, there are 3 ways to configure your typed dataset's
handling of null values (per column):

- Throw an exception if a null is encountered (the default)
- Return null
- Return string.Empty

Your typed data set is using the default. .NET Reflector shows that this
is the only use of the StrongTypingExc eption type.

Cheers,
Stefan Delmarco

http://www.fotia.co.uk
Hello Robert,

From the code snippet it looks like you're using a strongly typed data
set. I was assuming you were iterating over an IDataReader. Have a
look at the XSD for your dataset (or have a look at it in the DataSet
designer). I bet that the "NullValue" property for the FirstName
column is "(Throw)". Look for msprop:nullValu e="_throw" in the XSD.

More information here:
http://msdn2.microsoft.com/en-us/lib...az(vs.71).aspx

Cheers,
Stefan Delmarco
http://www.fotia.co.uk

HI Stefan,

Thanks for your reply.
I tried that, as in:
string dbnString;
dbnString = cuser.FirstName .ToString();
But still get the error:
- cuser.FirstName .ToString() 'this.cuser.Fir stName' threw an
exception of
type 'System.Data.St rongTypingExcep tion' string
{System.Data.S trongTypingExce ption}
I'm new to this, so no guessing why I am confused now
Thanks
Robert
"Stefan Delmarco" <St************ @fotia.co.ukwro te in message
news:c0******* *************** ****@news.micro soft.com...
Hello Robert,

Just call ToString() on the value returned. DbNull.String() will
return

string.empty .

Alternatively , if you want null returned then you'll have to convert
if

yourself.

A little helper function... well helps:

public static object ReadNullIfDbNul l(IDataReader reader, int
column)
{
object value = reader.GetValue (column);
return value == DbNull.Value ? null : value;
}
...
string column = (string)ReadNul lIfDbNull(reade r, 0);

Cheers,
Stefan Delmarco
http://www.fotia.co.uk
HI all,

I have a column value returned to a string variable in my c# app.
But the return type is of system.dbnull. How can I convert that to
a system.sting

Thanks
Robert


Dec 3 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5369
by: Prabhu | last post by:
Hi, We are having problem in converting a byte array to string, The byte array has char(174), char(175), char(240), char(242) and char(247) as delimiters for the message. when we use "System.Text.Encoding.ASCII.GetString(bytearray)" of .Net library, we found that the char (delimiters) specified above are replaced with different char.
2
11109
by: sbox | last post by:
I've got an error "System.FormatException: Input string was not in a correct format." while I'm implementing a datagrid and a textbox What's wrong with it? Sub Button1_Click(sender As Object, e As EventArgs) DataGrid1.DataSource = MyQueryMethod(CInt(TextBox1.Text)) DataGrid1.DataBind() End Sub Function MyQueryMethod(ByVal others As...
4
1723
by: Tina | last post by:
I have instantiated an insertRow for a dataset. I now want to make the GLCode DBNull. I have tried: insertRow.GLCode = Convert.dbnull insertRow.GLCode = Convert.dbnull(insertRow.GLCode) and several other things. How is it done?
0
868
by: Keith Chadwick | last post by:
I have been building a class around a xml document that is the representation of several tables from our sql2k database. The class has your standard methods such as load and save but I have also been adding in properties to allow for easy access to the the underlying xml document. My problem is this. I would like to strongly type the...
7
30696
by: Scott | last post by:
Hello all ! I'm working on a project which needs to connect quite frequently to a database. When I'm tryin to retrieve data and some columns contain a null value, my program crashes and I get the error : 'conversion from DBNull to String is invalid'... How can I solve this? Thanks in advance !
3
1720
by: Tom Luetz II | last post by:
I'm having some trouble with the DataAdapter in my code. When the parameter in an OleDbCommand, which is created as having a type of OleDbType.Decimal, is set to System.DbNull, the actual value posted to the database upon execution of the Insert command is a floating point value of zero (0.0, actually). The DataAdapter is associated with a...
4
13243
by: moondaddy | last post by:
How can I test to see if an object is System.DBNull? I have an event and need to get the value from a parameter like this: Private Sub ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Try Dim ID As Int32 'Next line is suto code. it doesnt compile If sender.Value Is System.DBNull Then
6
27300
by: cj | last post by:
in the command window I get: ? result {System.DBNull} : {System.DBNull} I need to check for this in code. I then tested in the command window if result = system.DBNull 'DBNull' is a type and cannot be used as an expression.
0
1475
by: paulnamroud | last post by:
Hello, I'm trying to send a Null value in a DateTime field while calling my stored procedure. If I use this first method (short with one line), i got the following error message: cmd.Parameters.Value = (this.OrderDate == DateTime.MaxValue) ? DBNull.Value : this.OrderDate;
0
7636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7849
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8070
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7603
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6194
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5461
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3590
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1171
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
882
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.