472,109 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,109 software developers and data experts.

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 34406
Hi Robert,

object value = row["column"];

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

--
Dave Sexton

"Robert Bravery" <me@u.comwrote in message
news:%2****************@TK2MSFTNGP04.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 ReadNullIfDbNull(IDataReader reader, int column)
{
object value = reader.GetValue(column);
return value == DbNull.Value ? null : value;
}

....

string column = (string)ReadNullIfDbNull(reader, 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.FirstName' threw an exception of
type 'System.Data.StrongTypingException' string
{System.Data.StrongTypingException}

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

Thanks
Robert
"Stefan Delmarco" <St************@fotia.co.ukwrote in message
news:c0**************************@news.microsoft.c om...
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 ReadNullIfDbNull(IDataReader reader, int column)
{
object value = reader.GetValue(column);
return value == DbNull.Value ? null : value;
}

...

string column = (string)ReadNullIfDbNull(reader, 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.IsFirstNameNull())
dbnString = null;
else
dbnString = cuser.FirstName;

--
Dave Sexton

"Robert Bravery" <me@u.comwrote in message
news:%2****************@TK2MSFTNGP05.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.FirstName' threw an exception of
type 'System.Data.StrongTypingException' string
{System.Data.StrongTypingException}

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

Thanks
Robert
"Stefan Delmarco" <St************@fotia.co.ukwrote in message
news:c0**************************@news.microsoft.c om...
>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 ReadNullIfDbNull(IDataReader reader, int column)
{
object value = reader.GetValue(column);
return value == DbNull.Value ? null : value;
}

...

string column = (string)ReadNullIfDbNull(reader, 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:nullValue="_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.FirstName' threw an
exception of
type 'System.Data.StrongTypingException' string
{System.Data.StrongTypingException}
I'm new to this, so no guessing why I am confused now

Thanks
Robert
"Stefan Delmarco" <St************@fotia.co.ukwrote in message
news:c0**************************@news.microsoft.c om...
>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 ReadNullIfDbNull(IDataReader reader, int column)
{
object value = reader.GetValue(column);
return value == DbNull.Value ? null : value;
}
...

string column = (string)ReadNullIfDbNull(reader, 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 #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 StrongTypingException 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:nullValue="_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.FirstName' threw an
exception of
type 'System.Data.StrongTypingException' string
{System.Data.StrongTypingException}
I'm new to this, so no guessing why I am confused now
Thanks
Robert
"Stefan Delmarco" <St************@fotia.co.ukwrote in message
news:c0**************************@news.microsoft. 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 ReadNullIfDbNull(IDataReader reader, int
column)
{
object value = reader.GetValue(column);
return value == DbNull.Value ? null : value;
}
...
string column = (string)ReadNullIfDbNull(reader, 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.ukwrote in message
news:c0**************************@news.microsoft.c om...
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 StrongTypingException 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:nullValue="_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.FirstName' threw an
exception of
type 'System.Data.StrongTypingException' string
{System.Data.StrongTypingException}
I'm new to this, so no guessing why I am confused now
Thanks
Robert
"Stefan Delmarco" <St************@fotia.co.ukwrote in message
news:c0**************************@news.microsoft.c om...
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 ReadNullIfDbNull(IDataReader reader, int
column)
{
object value = reader.GetValue(column);
return value == DbNull.Value ? null : value;
}
...
string column = (string)ReadNullIfDbNull(reader, 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.ukwrote in message
news:c0**************************@news.microsoft.c om...
>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 StrongTypingException 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:nullValue="_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.FirstName' threw an
exception of
type 'System.Data.StrongTypingException' string
{System.Data.StrongTypingException}
I'm new to this, so no guessing why I am confused now
Thanks
Robert
"Stefan Delmarco" <St************@fotia.co.ukwrote in message
news:c0**************************@news.microsoft .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 ReadNullIfDbNull(IDataReader reader, int
column)
{
object value = reader.GetValue(column);
return value == DbNull.Value ? null : value;
}
...
string column = (string)ReadNullIfDbNull(reader, 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******************@TK2MSFTNGP05.phx.gbl...
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.ukwrote in message
news:c0**************************@news.microsoft.c om...
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 StrongTypingException 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:nullValue="_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.FirstName' threw an
exception of
type 'System.Data.StrongTypingException' string
{System.Data.StrongTypingException}
I'm new to this, so no guessing why I am confused now
Thanks
Robert
"Stefan Delmarco" <St************@fotia.co.ukwrote in message
news:c0**************************@news.microsoft. 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 ReadNullIfDbNull(IDataReader reader, int
column)
{
object value = reader.GetValue(column);
return value == DbNull.Value ? null : value;
}
...
string column = (string)ReadNullIfDbNull(reader, 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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Prabhu | last post: by
4 posts views Thread by Tina | last post: by
reply views Thread by Keith Chadwick | last post: by
7 posts views Thread by Scott | last post: by
4 posts views Thread by moondaddy | last post: by
6 posts views Thread by cj | last post: by
reply views Thread by leo001 | last post: by

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.