PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 1.00 average.

Conversion from type DBNull to type String is invalid

 
 
Michel Vanderbeke
Guest
Posts: n/a
 
      15th May 2007
Hello,

When reading data from a SqlClient.SqlDataReader in VB.NET 2005, I want to
display fiels that sometimes are empty.

sUser = CStr(rdUser("Name") & Space(1) & CStr(rdUser("First_Name")

When the First_Name of a user is empty, I get an error telling me type
DBNull cannot be converted to String.
Is there a way of solving this problem?

Many thanks,

Michel


 
Reply With Quote
 
 
 
 
rowe_newsgroups
Guest
Posts: n/a
 
      15th May 2007
On May 15, 10:33 am, "Michel Vanderbeke" <michel.vanderb...@skynet.be>
wrote:
> Hello,
>
> When reading data from a SqlClient.SqlDataReader in VB.NET 2005, I want to
> display fiels that sometimes are empty.
>
> sUser = CStr(rdUser("Name") & Space(1) & CStr(rdUser("First_Name")
>
> When the First_Name of a user is empty, I get an error telling me type
> DBNull cannot be converted to String.
> Is there a way of solving this problem?
>
> Many thanks,
>
> Michel


Instead of:

sUser = CStr(rdUser("Name") & Space(1) & CStr(rdUser("First_Name")

Use:

sUser = rdUser("Name").ToString() & Space(1) & rdUser("First
Name")

or even better (imo):

sUser = String.Format("{0} {1}", rdUser("Name").ToString(),
rdUser("First_Name").ToString())


Thanks,

Seth Rowe

 
Reply With Quote
 
rowe_newsgroups
Guest
Posts: n/a
 
      15th May 2007
On May 15, 10:56 am, rowe_newsgroups <rowe_em...@yahoo.com> wrote:
> On May 15, 10:33 am, "Michel Vanderbeke" <michel.vanderb...@skynet.be>
> wrote:
>
> > Hello,

>
> > When reading data from a SqlClient.SqlDataReader in VB.NET 2005, I want to
> > display fiels that sometimes are empty.

>
> > sUser = CStr(rdUser("Name") & Space(1) & CStr(rdUser("First_Name")

>
> > When the First_Name of a user is empty, I get an error telling me type
> > DBNull cannot be converted to String.
> > Is there a way of solving this problem?

>
> > Many thanks,

>
> > Michel

>
> Instead of:
>
> sUser = CStr(rdUser("Name") & Space(1) & CStr(rdUser("First_Name")
>
> Use:
>
> sUser = rdUser("Name").ToString() & Space(1) & rdUser("First
> Name")
>
> or even better (imo):
>
> sUser = String.Format("{0} {1}", rdUser("Name").ToString(),
> rdUser("First_Name").ToString())
>
> Thanks,
>
> Seth Rowe



> sUser = rdUser("Name").ToString() & Space(1) & rdUser("First
> Name")


That should be:

sUser = rdUser("Name").ToString() & Space(1) &
rdUser("First_Name").ToString()


Thanks,

Seth Rowe

 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      15th May 2007
"rowe_newsgroups" <(E-Mail Removed)> schrieb:
> Instead of:
>
> sUser = CStr(rdUser("Name") & Space(1) & CStr(rdUser("First_Name")
>
> Use:
>
> sUser = rdUser("Name").ToString() & Space(1) & rdUser("First
> Name")
>
> or even better (imo):
>
> sUser = String.Format("{0} {1}", rdUser("Name").ToString(),
> rdUser("First_Name").ToString())



I am curious why this should be better. In addition, I'd write '... & " " &
....' instead of '... & Space(1) & ...'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

 
Reply With Quote
 
rowe_newsgroups
Guest
Posts: n/a
 
      16th May 2007
On May 15, 4:32 pm, "Herfried K. Wagner [MVP]" <hirf-spam-me-
h...@gmx.at> wrote:
> "rowe_newsgroups" <rowe_em...@yahoo.com> schrieb:
>
> > Instead of:

>
> > sUser = CStr(rdUser("Name") & Space(1) & CStr(rdUser("First_Name")

>
> > Use:

>
> > sUser = rdUser("Name").ToString() & Space(1) & rdUser("First
> > Name")

>
> > or even better (imo):

>
> > sUser = String.Format("{0} {1}", rdUser("Name").ToString(),
> > rdUser("First_Name").ToString())

>
> I am curious why this should be better. In addition, I'd write '... & " " &
> ...' instead of '... & Space(1) & ...'.
>
> --
> M S Herfried K. Wagner
> M V P <URL:http://dotnet.mvps.org/>
> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


I never said it was better - I was referring to String.Format - I too
would use & " ". I just left the code as the OP had it for
consistency.

Thanks,

Seth Rowe

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Conversion from type 'DBNull' to type 'String' is not valid Cirene Microsoft ASP .NET 2 19th Jun 2008 06:18 AM
Conversion from type 'DBNull' to type 'String' is not valid Chris Microsoft ASP .NET 2 11th May 2006 09:20 AM
conversion from type 'DBNull' to type 'float' is not valid ibiza Microsoft ASP .NET 2 27th Jan 2006 09:57 PM
Cast from type 'DBNull' to type 'String' is not valid. tshad Microsoft ASP .NET 6 15th Dec 2004 05:10 PM
Cast from type 'DBNull' to type 'String' is not valid. when creating dataset troutbum Microsoft ADO .NET 5 13th Mar 2004 01:30 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:32 AM.