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. General Programming
  3. C#
  4. String conversion to DBNull.Value

String conversion to DBNull.Value

Scheduled Pinned Locked Moved C#
question
5 Posts 2 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.
  • B Offline
    B Offline
    Brendan Vogt
    wrote on last edited by
    #1

    Hi, I tried the following function, but it doesn't want to work: //********************************************************************* // // CheckStringForDBNulls Static Method // // The CheckStringForDBNulls method checks if a string contains a value, if not it // returns DBNull.Value, or the value of the string. // //********************************************************************* public static DBNull CheckStringForDBNulls(string strCheckString) { if(strCheckString.Trim() != string.Empty) return strCheckString.Trim(); else return DBNull.Value; } Could you please let me know what I am doing wrong? This is used to check the parameter being added, if it contains nothing, the make the value DBNull.Value, or use the value of thr string. Thanks BRENDAN -- modified at 5:46 Thursday 20th October, 2005

    D 1 Reply Last reply
    0
    • B Brendan Vogt

      Hi, I tried the following function, but it doesn't want to work: //********************************************************************* // // CheckStringForDBNulls Static Method // // The CheckStringForDBNulls method checks if a string contains a value, if not it // returns DBNull.Value, or the value of the string. // //********************************************************************* public static DBNull CheckStringForDBNulls(string strCheckString) { if(strCheckString.Trim() != string.Empty) return strCheckString.Trim(); else return DBNull.Value; } Could you please let me know what I am doing wrong? This is used to check the parameter being added, if it contains nothing, the make the value DBNull.Value, or use the value of thr string. Thanks BRENDAN -- modified at 5:46 Thursday 20th October, 2005

      D Offline
      D Offline
      Dead Skin Mask
      wrote on last edited by
      #2

      the return type of this method is DBNull, so you can only ever return a null value.. i suspect you don't really need a static method to perform this check, and could probably get away with something like this instead : if(string.IsNullOrEmpty(strCheckString)) object = DBNull.Value; else object = strCheckString;

      B 1 Reply Last reply
      0
      • D Dead Skin Mask

        the return type of this method is DBNull, so you can only ever return a null value.. i suspect you don't really need a static method to perform this check, and could probably get away with something like this instead : if(string.IsNullOrEmpty(strCheckString)) object = DBNull.Value; else object = strCheckString;

        B Offline
        B Offline
        Brendan Vogt
        wrote on last edited by
        #3

        I want to put this into a function because I have a 20 checks, and the code can get very long. Please put the above mentioned code in a function.

        D 1 Reply Last reply
        0
        • B Brendan Vogt

          I want to put this into a function because I have a 20 checks, and the code can get very long. Please put the above mentioned code in a function.

          D Offline
          D Offline
          Dead Skin Mask
          wrote on last edited by
          #4

          I don't know how well it would work but you could try making your method return Object

          B 1 Reply Last reply
          0
          • D Dead Skin Mask

            I don't know how well it would work but you could try making your method return Object

            B Offline
            B Offline
            Brendan Vogt
            wrote on last edited by
            #5

            This was how I initially had it, I just thought there might be another way. But thanks for your help, I appreciate it!!

            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