473,387 Members | 1,597 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.

How deal with nulls in SQLDataReader?

I have some code that looks like this:

Dim SSN, LName, FName, M As String
mySqlConnection = New SqlConnection(myConnectionString)
Dim sql_Command As New SqlCommand( _
"Select SSN, LName, FName, M from Students WHERE (SSN = " + " '" +
ProposedValue + "')", _
mySqlConnection)
Try
mySqlConnection.Open()
Dim myReader As SqlDataReader =
sql_Command.ExecuteReader(CommandBehavior.CloseCon nection)

While myReader.Read()
SSN = myReader.GetString(0) ' The first field in the answer
set
LName = myReader.GetString(1) 'The second field in the
answer set
FName = myReader.GetString(2) 'The third field in the answer
set
'M = myReader.GetString(3) 'The fourth field in the answer
set
End While
myReader.Close()
sql_Command.Connection.Close()

The problem is that the field caled M (middle initial) is sometimes null.
When it is the code above fails. How can I write the code so that if the
middle initial field is blank or null the code won't crash?

Nov 20 '05 #1
5 6564
"Woody Splawn" <wo***@splawns.com> schrieb
I have some code that looks like this:

Dim SSN, LName, FName, M As String
mySqlConnection = New SqlConnection(myConnectionString)
Dim sql_Command As New SqlCommand( _
"Select SSN, LName, FName, M from Students WHERE (SSN = " + "
'" +
ProposedValue + "')", _
mySqlConnection)
Try
mySqlConnection.Open()
Dim myReader As SqlDataReader =
sql_Command.ExecuteReader(CommandBehavior.CloseCon nection)

While myReader.Read()
SSN = myReader.GetString(0) ' The first field in the
answer
set
LName = myReader.GetString(1) 'The second field in
the
answer set
FName = myReader.GetString(2) 'The third field in the
answer
set
'M = myReader.GetString(3) 'The fourth field in the
answer
set
End While
myReader.Close()
sql_Command.Connection.Close()

The problem is that the field caled M (middle initial) is sometimes
null. When it is the code above fails. How can I write the code so
that if the middle initial field is blank or null the code won't
crash?


Which value do you want to have in M when the database value is Null?
Nothing? A zero-length String? A certain string like "(Null)"?

--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2
"Woody Splawn" <wo***@splawns.com> schrieb
I have some code that looks like this:

Dim SSN, LName, FName, M As String
mySqlConnection = New SqlConnection(myConnectionString)
Dim sql_Command As New SqlCommand( _
"Select SSN, LName, FName, M from Students WHERE (SSN = " + "
'" +
ProposedValue + "')", _
mySqlConnection)
Try
mySqlConnection.Open()
Dim myReader As SqlDataReader =
sql_Command.ExecuteReader(CommandBehavior.CloseCon nection)

While myReader.Read()
SSN = myReader.GetString(0) ' The first field in the
answer
set
LName = myReader.GetString(1) 'The second field in
the
answer set
FName = myReader.GetString(2) 'The third field in the
answer
set
'M = myReader.GetString(3) 'The fourth field in the
answer
set
End While
myReader.Close()
sql_Command.Connection.Close()

The problem is that the field caled M (middle initial) is sometimes
null. When it is the code above fails. How can I write the code so
that if the middle initial field is blank or null the code won't
crash?


Which value do you want to have in M when the database value is Null?
Nothing? A zero-length String? A certain string like "(Null)"?

--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #3
M = IIf(IsDBNull(dr.GetString(3)), _
String.Empty, dr(3))

"Woody Splawn" <wo***@splawns.com> wrote in message
news:Oj**************@TK2MSFTNGP10.phx.gbl...
I have some code that looks like this:

Dim SSN, LName, FName, M As String
mySqlConnection = New SqlConnection(myConnectionString)
Dim sql_Command As New SqlCommand( _
"Select SSN, LName, FName, M from Students WHERE (SSN = " + " '" +
ProposedValue + "')", _
mySqlConnection)
Try
mySqlConnection.Open()
Dim myReader As SqlDataReader =
sql_Command.ExecuteReader(CommandBehavior.CloseCon nection)

While myReader.Read()
SSN = myReader.GetString(0) ' The first field in the answer set
LName = myReader.GetString(1) 'The second field in the
answer set
FName = myReader.GetString(2) 'The third field in the answer set
'M = myReader.GetString(3) 'The fourth field in the answer
set
End While
myReader.Close()
sql_Command.Connection.Close()

The problem is that the field caled M (middle initial) is sometimes null.
When it is the code above fails. How can I write the code so that if the
middle initial field is blank or null the code won't crash?

Nov 20 '05 #4
One neat trick is to create your own DataReader class that implements
IDataReader. Yeah, it's some typing to do it once, but when you're done you
can use the resulting class in all of you database access code and never
worry about nulls again.

Public Class SafeDataReader
Implements IDataReader

Private mDataReader As IDataReader

Public Sub New(ByVal DataReader As IDataReader)
mDataReader = DataReader
End Sub

....And a whole LOT of GetXXX methods go here...

End Class

In it, you can write Get methods like this:

Public Function GetString(ByVal i As Integer) As String Implements
IDataReader.GetString
If mDataReader.IsDBNull(i) Then
Return ""
Else
Return mDataReader.GetString(i)
End If
End Function

Even if you don't want to do that, the if statement in the above function
could easily be modified for your example:
If MyReader.IsDBNull(3) Then
M = ""
Else
M = MyReader.GetString(3)
End If

However, if you do a one-time fix like this then you won't be protected when
a null crops up in other fields like FName or LName at some later date.

BTW: I don't want to steal credit for this idea; I found it in "Visual Basic
..NET Business Objects" by Rockford Lhotka.
"Woody Splawn" <wo***@splawns.com> wrote in message
news:Oj**************@TK2MSFTNGP10.phx.gbl...
I have some code that looks like this:

Dim SSN, LName, FName, M As String
mySqlConnection = New SqlConnection(myConnectionString)
Dim sql_Command As New SqlCommand( _
"Select SSN, LName, FName, M from Students WHERE (SSN = " + " '" +
ProposedValue + "')", _
mySqlConnection)
Try
mySqlConnection.Open()
Dim myReader As SqlDataReader =
sql_Command.ExecuteReader(CommandBehavior.CloseCon nection)

While myReader.Read()
SSN = myReader.GetString(0) ' The first field in the answer set
LName = myReader.GetString(1) 'The second field in the
answer set
FName = myReader.GetString(2) 'The third field in the answer set
'M = myReader.GetString(3) 'The fourth field in the answer
set
End While
myReader.Close()
sql_Command.Connection.Close()

The problem is that the field caled M (middle initial) is sometimes null.
When it is the code above fails. How can I write the code so that if the
middle initial field is blank or null the code won't crash?

Nov 20 '05 #5
"Adam J. Schaff" <ju**@maine.rr.com> schrieb
One neat trick is to create your own DataReader class that
implements IDataReader. Yeah, it's some typing to do it once, but
when you're done you can use the resulting class in all of you
database access code and never worry about nulls again.


Only a remark:
Disallowing Null values - if possible - in a database is the better
approach. When designing the database, one should wonder whether there is a
reason to distinguish between a Null value and e.g. a zero-length string.
Usually there isn't. If Null values are converted to zero-length strings
anyway, storing Null values does not make sense.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #6

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

Similar topics

12
by: Mike | last post by:
Hi. I have a database from which I am reading some integers into my C# app. The problem is some of these DB values are NULL, so obviously an error is thrown when I try to read them. Is there a...
4
by: Mark | last post by:
We have a SQL Server table with a decimal column that is not required. We're building a .NET application for data entry. If the value is optional, but a decimal is strongly typed in C# and cannot...
5
by: TomislaW | last post by:
I have integers in my database that can be null. I read them with SqlDataReader and show them to Repeater. Since integer can not be null in c#, I set null integer to -1. My project is 3-tier and it...
7
by: Franck Diastein | last post by:
Hi, when I call ExportData I have this error: Invalid attempt to Read when reader is closed. Telling me that there's a problem with this line: while(_dataR.Read()){ Code:...
3
by: Neil Guyette | last post by:
Hello, Everyone, I'm trying to find information on how to populate a combo box using a SqlDataReader. I want to be able to set the value of the combo's value property different then the...
1
by: Arvind P Rangan | last post by:
Hi All, How do you get all the values of a sqldatareader if it contains multiple resultset. Using sqldatareader.nextresult and sqldatareader.read e.g. While sqldatareader.read ' If not...
4
by: mimi | last post by:
Hi Please help me out, I can't find a way to close a sqldatareader when error occur at statement cmd.ExecuteReader(). I can't close it in catch because it is local in try scope and I can't...
0
by: Woody Splawn | last post by:
I have some code that looks like this: Dim SSN, LName, FName, M As String mySqlConnection = New SqlConnection(myConnectionString) Dim sql_Command As New SqlCommand( _ "Select SSN, LName, FName,...
4
by: Randy Galliano | last post by:
Hello, I am reading some records into a recordset when I got to assign a text box the value of one of the fields I get an exception because the recordset has a null value in it. How do you test...
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:
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.