Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Conversion from type 'DBNull' to type 'String' is not valid

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

Scheduled Pinned Locked Moved ASP.NET
helpquestioncsharpasp-netdocker
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gerryR com
    wrote on last edited by
    #1

    Hi All Slowly getting through my 1st asp.net page but need to pick your brains (again!) Basically I'm pulling data from an excel sheet, one of the thing's I'm doing with the data is making a url link, if there's text in column 2 then that's to be the text for the link, if there isn't then the text in column 1 is the text for the link. Problem is, as there doesn't necessarily have to be text in column 2 I'm getting a dbnull error. Below is my function and the asp code refering to that function. I know I need to put some error handeling but my big question is where? Is it within the function or within the asp link?

    Function MakeLinkText(ByVal name As String, ByVal description As String) As String
    If description = "" Then
    name = name
    else
    name = description
    end if
    return name
    End Function

    <asp:TemplateField HeaderText="LinkText"><ItemTemplate><%# MakeLinkText(Container.Dataitem("title"), Container.Dataitem("refer to")) %></ItemTemplate></asp:TemplateField>

    I did try on error resume nextwithin the function but still got the error (I realise thats very bad practice, I was just trying to narrow down the cause of the error. Appreciate a point in the right direction. Rgds gerryR

    C N 2 Replies Last reply
    0
    • G gerryR com

      Hi All Slowly getting through my 1st asp.net page but need to pick your brains (again!) Basically I'm pulling data from an excel sheet, one of the thing's I'm doing with the data is making a url link, if there's text in column 2 then that's to be the text for the link, if there isn't then the text in column 1 is the text for the link. Problem is, as there doesn't necessarily have to be text in column 2 I'm getting a dbnull error. Below is my function and the asp code refering to that function. I know I need to put some error handeling but my big question is where? Is it within the function or within the asp link?

      Function MakeLinkText(ByVal name As String, ByVal description As String) As String
      If description = "" Then
      name = name
      else
      name = description
      end if
      return name
      End Function

      <asp:TemplateField HeaderText="LinkText"><ItemTemplate><%# MakeLinkText(Container.Dataitem("title"), Container.Dataitem("refer to")) %></ItemTemplate></asp:TemplateField>

      I did try on error resume nextwithin the function but still got the error (I realise thats very bad practice, I was just trying to narrow down the cause of the error. Appreciate a point in the right direction. Rgds gerryR

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Try if string.IsNullOrEmpty(description), assuming this is the point of error. In C#, you can do ?? string.Empty to turn null into an empty string.

      Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.

      1 Reply Last reply
      0
      • G gerryR com

        Hi All Slowly getting through my 1st asp.net page but need to pick your brains (again!) Basically I'm pulling data from an excel sheet, one of the thing's I'm doing with the data is making a url link, if there's text in column 2 then that's to be the text for the link, if there isn't then the text in column 1 is the text for the link. Problem is, as there doesn't necessarily have to be text in column 2 I'm getting a dbnull error. Below is my function and the asp code refering to that function. I know I need to put some error handeling but my big question is where? Is it within the function or within the asp link?

        Function MakeLinkText(ByVal name As String, ByVal description As String) As String
        If description = "" Then
        name = name
        else
        name = description
        end if
        return name
        End Function

        <asp:TemplateField HeaderText="LinkText"><ItemTemplate><%# MakeLinkText(Container.Dataitem("title"), Container.Dataitem("refer to")) %></ItemTemplate></asp:TemplateField>

        I did try on error resume nextwithin the function but still got the error (I realise thats very bad practice, I was just trying to narrow down the cause of the error. Appreciate a point in the right direction. Rgds gerryR

        N Offline
        N Offline
        NeverHeardOfMe
        wrote on last edited by
        #3

        In Visual Basic you can test for DbNull using If IsDbNull(whatever) Then... Howver, nb that you will need to re-write your function without explicitly defining the type - eg Assuming "name" is never null, but description might be:

        Function MakeLinkText(ByVal name, ByVal description) As String
        If IsDbNull(description) Then
        Return CStr(name)
        Else
        Return CStr(description)
        End If
        End Function

        G 1 Reply Last reply
        0
        • N NeverHeardOfMe

          In Visual Basic you can test for DbNull using If IsDbNull(whatever) Then... Howver, nb that you will need to re-write your function without explicitly defining the type - eg Assuming "name" is never null, but description might be:

          Function MakeLinkText(ByVal name, ByVal description) As String
          If IsDbNull(description) Then
          Return CStr(name)
          Else
          Return CStr(description)
          End If
          End Function

          G Offline
          G Offline
          gerryR com
          wrote on last edited by
          #4

          Thanks once again Phil, workes perfectly! One question, if I leave the CStr out it still seems to work, is there some other reason for having it in other than just good code practice? (not that that's not reason enough!) Thanks again gerryR

          N A 2 Replies Last reply
          0
          • G gerryR com

            Thanks once again Phil, workes perfectly! One question, if I leave the CStr out it still seems to work, is there some other reason for having it in other than just good code practice? (not that that's not reason enough!) Thanks again gerryR

            N Offline
            N Offline
            NeverHeardOfMe
            wrote on last edited by
            #5

            Well, not really... only that as the type is not explicitly defined it just takes care of any situation when you might call the same function from somewhere else with different data types...

            1 Reply Last reply
            0
            • G gerryR com

              Thanks once again Phil, workes perfectly! One question, if I leave the CStr out it still seems to work, is there some other reason for having it in other than just good code practice? (not that that's not reason enough!) Thanks again gerryR

              A Offline
              A Offline
              Abhishek Sur
              wrote on last edited by
              #6

              yes ... if you make Option Strict on then CStr is necessary.... :rose:

              Abhishek Sur

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups