473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Conversion from type DBNull to type String is invalid

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
May 15 '07 #1
4 6268
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

May 15 '07 #2
On May 15, 10:56 am, rowe_newsgroups <rowe_em...@yahoo.comwrote:
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

May 15 '07 #3
"rowe_newsgroups" <ro********@yahoo.comschrieb:
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/>

May 15 '07 #4
On May 15, 4:32 pm, "Herfried K. Wagner [MVP]" <hirf-spam-me-
h...@gmx.atwrote:
"rowe_newsgroups" <rowe_em...@yahoo.comschrieb:
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

May 15 '07 #5

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

Similar topics

30
by: Tim Johansson | last post by:
I'm new to C++, and tried to start making a script that will shuffle an array. Can someone please tell me what's wrong? #include <iostream.h> #include <string.h> int main () {...
5
by: Vijai Kalyan | last post by:
Hello, I have come back to C++ after a couple of years with Java so I am quite rusty and this question may seem poor: My platform is Windows XP with MSVC 7.1. I have a class with a...
0
by: Martin | last post by:
When I convert a program from VC6 to VC7 I get the following linker error: CVTRES : fatal error CVT1100: duplicate resource. type:STRING, name:1686, language:0x0409 LINK : fatal error LNK1123:...
1
by: dave | last post by:
.. Line 38: Function FormatURL5(ByVal strArgument) As String Line 39: If strArgument.length > 0 Then Line 40: Return ("<INPUT id='mmmm' type='checkbox'...
7
by: Alphonse Giambrone | last post by:
How can I convert a string to a different type based on another string or other variable? For instance, instead of Dim i as Integer i = ctype("1000", Integer) I would like to do
3
by: Bob Day | last post by:
VS 2003, sql How do you determine the data type of a column if its value is DBNull? 1)Table: Column1 STRING non-nullable 2) Fill to a DataSet via DataAdapter 3) dim Data_Type_Is...
2
by: ibiza | last post by:
Hi all, I'm using a business logic layer as described in this source code : ...
2
by: Chris | last post by:
Hi again, I want to read all the records of a table with 2 fields. The problem is that some records have null value in the second field. This code below works when all records have both fields...
3
by: Sep410 | last post by:
Hi all, Please help me in this code: txtLastNameFM.Text = IIf(dgFamilyMember.CurrentRow.Cells(3).Equals(System.DBNull.Value), "", dgFamilyMember.CurrentRow.Cells(3).Value) this is an error I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.