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
  1. Home
  2. Web Development
  3. ASP.NET
  4. "cast from type 'dbnull' to type 'string' is not valid"

"cast from type 'dbnull' to type 'string' is not valid"

Scheduled Pinned Locked Moved ASP.NET
helpdatabasetutorialquestion
5 Posts 4 Posters 1 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.
  • M Offline
    M Offline
    macca24
    wrote on last edited by
    #1

    I keep getting the following error in my code: "cast from type 'dbnull' to type 'string' is not valid" My code is as follows: Do While DR.Read tbxRefNo.Text = Trim(DR("Ref")) tbxTitle.Text = Trim(DR("Title")) If Not IsDBNull(DR("Signed")) Then tbxDateSigned.Text = DR("Signed") Else tbxDateSigned.Text = "" End If tbxStatus.Text = DR("StatusName") tbxSignedBy.Text = DR("SignedBy") Loop I am reading the values from a database using the loop but I was getting a problem when I read a record that had a NULL value in the "Signed" field. I changed the code to put in the I Not IsDBNull... but I am getting the above error message. Anyone any ideas how to do this better? Thanks, macca

    C T M 3 Replies Last reply
    0
    • M macca24

      I keep getting the following error in my code: "cast from type 'dbnull' to type 'string' is not valid" My code is as follows: Do While DR.Read tbxRefNo.Text = Trim(DR("Ref")) tbxTitle.Text = Trim(DR("Title")) If Not IsDBNull(DR("Signed")) Then tbxDateSigned.Text = DR("Signed") Else tbxDateSigned.Text = "" End If tbxStatus.Text = DR("StatusName") tbxSignedBy.Text = DR("SignedBy") Loop I am reading the values from a database using the loop but I was getting a problem when I read a record that had a NULL value in the "Signed" field. I changed the code to put in the I Not IsDBNull... but I am getting the above error message. Anyone any ideas how to do this better? Thanks, macca

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

      Are you certain this is the only field that has a null in it ? Because it looks like it should work to me. Assuming you're using SQL Server, the best approach would be to use the COALESCE keyword to avoid returning DBNUll at all. Even better would be to give these strings a default value of '' instead of null, assuming that they have the same meaning ( as they do in the UI ) Christian Graus - Microsoft MVP - C++ -- modified at 17:40 Thursday 15th September, 2005

      1 Reply Last reply
      0
      • M macca24

        I keep getting the following error in my code: "cast from type 'dbnull' to type 'string' is not valid" My code is as follows: Do While DR.Read tbxRefNo.Text = Trim(DR("Ref")) tbxTitle.Text = Trim(DR("Title")) If Not IsDBNull(DR("Signed")) Then tbxDateSigned.Text = DR("Signed") Else tbxDateSigned.Text = "" End If tbxStatus.Text = DR("StatusName") tbxSignedBy.Text = DR("SignedBy") Loop I am reading the values from a database using the loop but I was getting a problem when I read a record that had a NULL value in the "Signed" field. I changed the code to put in the I Not IsDBNull... but I am getting the above error message. Anyone any ideas how to do this better? Thanks, macca

        T Offline
        T Offline
        tojamismis
        wrote on last edited by
        #3

        The easiest thing to do is do DR("StatusName").ToString(). This will automatically convert any nulls to String.Empty Torin Blair - MCSD.NET
        'In the immortal words of Socrates - "I drank what?".'

        1 Reply Last reply
        0
        • M macca24

          I keep getting the following error in my code: "cast from type 'dbnull' to type 'string' is not valid" My code is as follows: Do While DR.Read tbxRefNo.Text = Trim(DR("Ref")) tbxTitle.Text = Trim(DR("Title")) If Not IsDBNull(DR("Signed")) Then tbxDateSigned.Text = DR("Signed") Else tbxDateSigned.Text = "" End If tbxStatus.Text = DR("StatusName") tbxSignedBy.Text = DR("SignedBy") Loop I am reading the values from a database using the loop but I was getting a problem when I read a record that had a NULL value in the "Signed" field. I changed the code to put in the I Not IsDBNull... but I am getting the above error message. Anyone any ideas how to do this better? Thanks, macca

          M Offline
          M Offline
          MihirV
          wrote on last edited by
          #4

          hi use this to check null values If Not DR.IsDBNull(DR.GetOrdinal("Signed")) Then tbxDateSigned.Text = DR("Signed") Else tbxDateSigned.Text = "" End If Mihir..

          M 1 Reply Last reply
          0
          • M MihirV

            hi use this to check null values If Not DR.IsDBNull(DR.GetOrdinal("Signed")) Then tbxDateSigned.Text = DR("Signed") Else tbxDateSigned.Text = "" End If Mihir..

            M Offline
            M Offline
            macca24
            wrote on last edited by
            #5

            Guys, thanks for all your help. This is working fine for me now. macca

            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