PC Review


Reply
Thread Tools Rate Thread

How deal with nulls in SQLDataReader?

 
 
Woody Splawn
Guest
Posts: n/a
 
      15th Dec 2003
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.CloseConnection)

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?



 
Reply With Quote
 
 
 
 
Armin Zingler
Guest
Posts: n/a
 
      15th Dec 2003
"Woody Splawn" <(E-Mail Removed)> 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.CloseConnection)
>
> 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

 
Reply With Quote
 
William Ryan
Guest
Posts: n/a
 
      15th Dec 2003
M = IIf(IsDBNull(dr.GetString(3)), _
String.Empty, dr(3))

"Woody Splawn" <(E-Mail Removed)> wrote in message
news:Oj$(E-Mail Removed)...
> 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.CloseConnection)
>
> 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?
>
>
>



 
Reply With Quote
 
Adam J. Schaff
Guest
Posts: n/a
 
      16th Dec 2003
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" <(E-Mail Removed)> wrote in message
news:Oj$(E-Mail Removed)...
> 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.CloseConnection)
>
> 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?
>
>
>



 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      16th Dec 2003
"Adam J. Schaff" <(E-Mail Removed)> 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

 
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
Count Nulls & Not Nulls in Reports RoadKill Microsoft Access Reports 2 28th Jul 2008 06:16 PM
Has anyone made a Deal or No Deal Gameboard? kajunlion@yahoo.com Microsoft Excel Programming 2 17th Apr 2008 12:12 AM
Question: SqlDataReader.GetSqlXXXX vs. SqlDataReader.GetXXXX Marlon Microsoft ADO .NET 1 29th Jan 2005 01:53 AM
Question: SqlDataReader.GetSqlXXXX vs. SqlDataReader.GetXXXX Marlon Microsoft Dot NET Framework 1 29th Jan 2005 01:47 AM
Re: Is this 181 GB drive on EBay a good deal or a bad deal? GMAN Storage Devices 0 24th Jun 2003 08:45 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:34 PM.