Click to See Complete Forum and Search --> : Importing DBNull values into a String


Nick2175
03-14-2007, 01:18 PM
I'm trying to important data from Sql Server into my VB.Net program. However when I do this I receive an error.

If for example you take the variable "Main_Doctor" it contains Nulls in SQL Server, but String here won't accept the Nulls and I receive this error:


"Conversion from type 'DBNull' to type 'String' is not valid"


Does anyone know of the code I can use to accept these Nulls into a String value?

Many thanks in advance


Using reader As System.Data.SqlClient.SqlDataReader = Public Shared Function PracSearch() As List(Of Search)

Dim search As New List(Of Search)
Using conn As New System.Data.SqlClient.SqlConnection()
conn.ConnectionString = ConnectionString
conn.Open()
Dim PracSrch As New System.Data.SqlClient.SqlCommand(("Select Prac_No, Prac_Eid, Prac_Status, Main_Doctor, Post_Code, Telephone, System_Type, Address1, Address2, Town From TblPracDetails"), conn)

Using reader As System.Data.SqlClient.SqlDataReader = PracSrch.ExecuteReader()
While reader.Read()
'Dim Comm_Id As Integer = reader.GetValue(0)
Dim Prac_No As Integer = reader.GetValue(0)
Dim Prac_Eid As Integer = reader.GetValue(1)
Dim Prac_Status As String = reader.GetString(2)
Dim Main_Doctor As String = reader.GetValue(3)
Dim Post_Code As String = reader.GetString(4)
Dim Telephone As Integer = reader.GetValue(5)
Dim System_Type As String = reader.GetString(6)
Dim Address1 As String = reader.GetString(7)
Dim Address2 As String = reader.GetString(8)
Dim Town As String = reader.GetString(9)

Dim srch As New Search(Prac_No, Prac_Eid, Prac_Status, Main_Doctor, Post_Code, Telephone, System_Type, Address1, Address2, Town)

search.Add(srch)
End While
End Using

End Using
Return search


End Function

jcb1269
03-19-2007, 01:27 PM
Without actually messing with it... You might have to put the fields that can be null into an if statement checking if it's null. Look into isDBNULL and/or isNothing.

dim Prac_No as integer
if isDbNull(reader.GetValue(0)) then
......
Else
Prac_No = reader.GetValue(0))
end if

Hope this helps... :)

jb