-
Importing DBNull values into a String
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
-
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
Similar Threads
-
By Rob Teixeira in forum .NET
Replies: 15
Last Post: 05-31-2002, 03:30 PM
-
By Julian Milano in forum VB Classic
Replies: 2
Last Post: 08-11-2000, 12:11 PM
-
By Chandra in forum VB Classic
Replies: 0
Last Post: 06-22-2000, 12:52 PM
-
By chandra in forum VB Classic
Replies: 0
Last Post: 06-22-2000, 07:36 AM
-
By Chandra in forum VB Classic
Replies: 0
Last Post: 06-22-2000, 07:30 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks