Results 1 to 6 of 6

Thread: Casting

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Casting

    How do you cast a 'null' value into a 'string'? i cant find ANY info on that. If it's not possible, what a way around this when a databse field has a null value in it?

  2. #2
    Addicted Member
    Join Date
    Aug 2003
    Posts
    153
    VB Code:
    1. Dim myString As String
    2.  
    3. If IsDBNull(myRow.Item("Example")) = True Then
    4.       myString = ""
    5. Else
    6.       myString = CStr(myRow.Item("Example")).TrimEnd
    7. End If

  3. #3
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    OR:
    Code:
    Dim myString As String
    
    If myRow.Item("Example") Is DBNull.Value Then
          myString = ""
    Else
          myString = CStr(myRow.Item("Example")).TrimEnd
    End If

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    neither one worked. It's coming from a database table and I get this message:

    cast from type 'DBNull' to type 'String' is not valid


    I thought for sure that code y'all had would work.

  5. #5
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    I used this for a quick project

    VB Code:
    1. Private Function DBReturn(ByVal objValue As Object) As Object
    2.             If TypeOf (objValue) Is DBNull Then
    3.                 Return Nothing
    4.             Else
    5.                 Return objValue
    6.             End If
    7.         End Function


    and then

    VB Code:
    1. 'cast The object type returned by DBReturn to the proper Data type
    2.  
    3. 'so...
    4.  
    5. Msgbox (CStr(DBReturn(myRow.Item("Example"))).TrimEnd)

    I am sure that someone will post a more elegant solution but this works..
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  6. #6
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Being that a textbox should have nothing for text when the form loads, a simple:
    VB Code:
    1. If Not(myrow.Item(0)) Is DbNull.Value Then myTextbox.Text = myrow.item(0)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width