DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2007
    Posts
    1

    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

  2. #2
    Join Date
    Jul 2004
    Posts
    364
    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

  1. App Object
    By Rob Teixeira in forum .NET
    Replies: 15
    Last Post: 05-31-2002, 03:30 PM
  2. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 2
    Last Post: 08-11-2000, 12:11 PM
  3. Deadlock error.. how to remove
    By Chandra in forum VB Classic
    Replies: 0
    Last Post: 06-22-2000, 12:52 PM
  4. Please help me -- urgent -- deadlock error
    By chandra in forum VB Classic
    Replies: 0
    Last Post: 06-22-2000, 07:36 AM
  5. Replies: 0
    Last Post: 06-22-2000, 07:30 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


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


Sponsored Links