Results 1 to 6 of 6

Thread: replace Null values - Cast from type 'DBNull' to type 'String' is not valid.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    120

    replace Null values - Cast from type 'DBNull' to type 'String' is not valid.

    I am starting to understand why the error "Cast from type 'DBNull' to type 'String' is not valid." is happening, but I am not sure how to determine WHICH line is failing ( I am in the middle of a DotNetNuke app and very new to .NET in general )..
    But I can see the replace_Null function is failing.

    the function:
    Private Function replace_Null(ByVal myobj As Object) As String
    If IsDBNull(myobj) Then
    Return ""
    Else
    Return CStr(myobj)
    End If
    End Function

    and the section of code that calls the function.
    Is there a way like in classic asp to Response.Write = "HERE" ??
    So I know where the error is occuring?

    Public Function Load_Booth_Research(ByVal Trade_Show_ID As Integer, ByVal Company_ID As Integer) As Boolean
    btn_update.Visible = True
    Dim dr As SqlDataReader = leadsDB.get_market_company(Trade_Show_ID, Company_ID)
    Load_Booth_Research = False
    If dr.Read Then
    'Top section
    '**************************************************************
    txt_research.Text = replace_Null(dr("researched_by"))
    If CType(replace_Null(dr("front_end")), Boolean) = True Then
    txt_front.Text = CStr(dr("front_end_booth")) 'crash here
    Else
    txt_front.Text = ""
    End If

    txtSalesperson.Text = replace_Null(dr("SMC_Salesperson"))
    txtDistributorSalesperson.Text = replace_Null(dr("Dist_Salesperson"))

    'Company Info
    '**************************************************************
    Booth_Research_ID = CInt(dr("Booth_Research_ID"))
    txt_company.Text = replace_Null(dr("company_name"))
    txt_Address.Text = replace_Null(dr("address1"))
    txt_City.Text = replace_Null(dr("city"))
    txt_Zip.Text = replace_Null(dr("zip"))
    txt_Phone.Text = replace_Null(dr("phone"))
    txt_WebAddress.Text = replace_Null(dr("website"))
    txt_products.Text = replace_Null(dr("type_of_products"))
    ck_on_display.Checked = CType(dr("tool_on_display"), Boolean)

    'State
    Try
    drpState.Items.FindByText(replace_Null(dr("StateShort"))).Selected = True
    Catch e As Exception

    End Try

    'Market Share
    Try
    drpMarketShare.Items.FindByValue(replace_Null(dr("Share_ID"))).Selected = True
    Catch e As Exception

    End Try

    'Potential
    Try
    drp_potential.Items.FindByValue(replace_Null(dr("Potential_ID"))).Selected = True
    Catch e As Exception

    End Try

    'Channel
    Try
    drpSalesChannel.Items.FindByValue(replace_Null(dr("channel_id"))).Selected = True
    Catch e As Exception

    End Try

    'AS400
    If Not IsDBNull(dr("isAS400")) Then
    If CType(replace_Null(dr("isAS400")), Boolean) = True And CType(replace_Null(dr("isActive")), Boolean) = True Then
    drpAS400.Items(1).Selected = True
    ElseIf CType(replace_Null(dr("isAS400")), Boolean) = True Then
    drpAS400.Items(2).Selected = True
    Else
    drpAS400.Items(3).Selected = True
    End If
    Else
    drpAS400.Items(0).Selected = True
    End If

    'Company Type
    Dim mydr As SqlDataReader = leadsDB.get_Type_by_Company(Company_ID)
    While mydr.Read
    Try
    chkCompanyType.Items.FindByValue(CStr(mydr("Type_ID"))).Selected = True
    Catch ex As Exception

    End Try
    End While

    'Company Classification
    mydr = leadsDB.get_Classification_by_Company(Company_ID)
    While mydr.Read
    Try
    chkCompanyClass.Items.FindByValue(CStr(mydr("Classification_ID"))).Selected = True
    Catch ex As Exception

    End Try
    End While


    'Other
    '*************************************************************
    ck_pnuematic.Checked = CType(dr("pneumatic_equipment"), Boolean)

    Try
    drp_reason.Items.FindByText(replace_Null(dr("why_current_supplier"))).Selected = True
    Catch ex As Exception
    Dim sReason As String = replace_Null(dr("why_current_supplier"))
    If sReason <> "" Then
    drp_reason.Items.FindByText("Other").Selected = True
    Else
    drp_reason.Items.FindByValue("99").Selected = True
    End If
    txt_reason_other.Text = sReason
    End Try

    ck_catalog.Checked = CType(dr("want_catalog"), Boolean)
    txt_catalog.Text = CType(replace_Null(dr("catalog_desc")), String)
    ck_sales_person.Checked = CType(replace_bool_Null(dr("want_contact")), Boolean)

    txt_comments.Text = replace_Null(dr("notes"))
    Load_Booth_Research = True

    Dim myparent As MarketResearch = CType(Me.Parent, MarketResearch)
    Load_Product_Competitor_Tables(CInt(myparent.Trade_Show_ID), Company_ID)
    Load_Contacts(Company_ID)
    End If

    dr.Close()
    End Function

  2. #2
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: replace Null values - Cast from type 'DBNull' to type 'String' is not valid.

    Instead of using IsDBNull, try this:
    Code:
    If String.IsNullOrEmpty(myStr) Then

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    120

    Re: replace Null values - Cast from type 'DBNull' to type 'String' is not valid.

    HI there,

    The specific crash is occuring on the db column front_end_booth (it has nulls):

    If CType(replace_Null(dr("front_end")), Boolean) = True Then
    txt_front.Text = CStr(dr("front_end_booth")) 'crash here
    Else
    txt_front.Text = ""
    End If

    So the application is saying "Hey wait!!, I cant convert a NULL to a String!!" ??
    Last edited by Acuffdl; Jul 2nd, 2007 at 01:26 PM.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    120

    Re: replace Null values - Cast from type 'DBNull' to type 'String' is not valid.

    Hi there.
    I tried changing the replace_Null function to:

    If String.IsNullOrEmpty(myobj) Then..

    But I am getting an error in the source:
    'IsNullOrEmpty is not a member of String'

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: replace Null values - Cast from type 'DBNull' to type 'String' is not valid.

    Acuffdl - what version of VB are you using?

    =tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    120

    Re: replace Null values - Cast from type 'DBNull' to type 'String' is not valid.

    This is an old DotNetNuke version 2.1 circa 2001?

    And Visual Basic 2003.net is kicking in as the IDE instead of my newly prefered VS 2005...

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