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. Erro wen sql server table has a empty value

Erro wen sql server table has a empty value

Scheduled Pinned Locked Moved C#
helpdatabasesql-serversysadminquestion
8 Posts 7 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.
  • N Offline
    N Offline
    NewToAspDotNet
    wrote on last edited by
    #1

    Hi guys, I have a app that reads data from sqlserver table and set them into textboxes. The data type of my table's columns are 'text'. The problem is that wen there is a empty field in my table, i get the error: Unable to cast object of type 'System.DBNull' to type 'System.String' anyone a idea????

    P C 2 Replies Last reply
    0
    • N NewToAspDotNet

      Hi guys, I have a app that reads data from sqlserver table and set them into textboxes. The data type of my table's columns are 'text'. The problem is that wen there is a empty field in my table, i get the error: Unable to cast object of type 'System.DBNull' to type 'System.String' anyone a idea????

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      You need to check it to see if it's a DBNull before you attempt to convert it. Suppose that you are using a DataReader, you could do this

      if (!myDataReader.IsDBNull(0))
        myText = myDataReader.GetString(0);
      

      Deja View - the feeling that you've seen this post before.

      My blog | My articles

      N 1 Reply Last reply
      0
      • P Pete OHanlon

        You need to check it to see if it's a DBNull before you attempt to convert it. Suppose that you are using a DataReader, you could do this

        if (!myDataReader.IsDBNull(0))
          myText = myDataReader.GetString(0);
        

        Deja View - the feeling that you've seen this post before.

        My blog | My articles

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

        Pete O'Hanlon, Thanx for your response, bud what if i don't use a datareader? and have only a select. this how my code looks like: SqlCommand comm00 = new SqlCommand("select Cmp_nr from Companies where Cmp_name='" + companyName + "'", myconn); companyName = (string)comm00.ExecuteScalar();

        M E L V 4 Replies Last reply
        0
        • N NewToAspDotNet

          Hi guys, I have a app that reads data from sqlserver table and set them into textboxes. The data type of my table's columns are 'text'. The problem is that wen there is a empty field in my table, i get the error: Unable to cast object of type 'System.DBNull' to type 'System.String' anyone a idea????

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

          Depends where the code is. Obviously, whatever construct you're using ( data reader, data table, etc ), you're not checking if it has data in it before you try to read it.

          Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

          1 Reply Last reply
          0
          • N NewToAspDotNet

            Pete O'Hanlon, Thanx for your response, bud what if i don't use a datareader? and have only a select. this how my code looks like: SqlCommand comm00 = new SqlCommand("select Cmp_nr from Companies where Cmp_name='" + companyName + "'", myconn); companyName = (string)comm00.ExecuteScalar();

            M Offline
            M Offline
            Malcolm Smart
            wrote on last edited by
            #5

            Use the IsNull SQL Command and convert it on the server. Also, you should use parameters in your queries, not appending strings. Using parameters prevents your select statement being open to an SQL Injection attacks. (Said with a broad Scottish accent...)

            "More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF

            "This time yesterday, I still had 24 hours to meet the deadline I've just missed today."

            1 Reply Last reply
            0
            • N NewToAspDotNet

              Pete O'Hanlon, Thanx for your response, bud what if i don't use a datareader? and have only a select. this how my code looks like: SqlCommand comm00 = new SqlCommand("select Cmp_nr from Companies where Cmp_name='" + companyName + "'", myconn); companyName = (string)comm00.ExecuteScalar();

              E Offline
              E Offline
              echuck66
              wrote on last edited by
              #6

              For simple select statements such as this, I would use ISNULL in the sql command... For example, using the code you included: SqlCommand comm00 = new SqlCommand(); string commSQL = "SELECT ISNULL(Cmp_nr, '') AS Cmp_nr FROM Companies where Cmp_name=@cmpName"; SqlPararameter cmpParam = new SqlParameter("@cmpName", SqlDbType.VarChar, 50); //note: 50 should be changed to the field length of your db field Cmp_name cmpParam.Value = companyName; comm00.Parameters.Add(cmpParam); comm00.Connection = myconn; comm00.CommandText = commSQL;

              1 Reply Last reply
              0
              • N NewToAspDotNet

                Pete O'Hanlon, Thanx for your response, bud what if i don't use a datareader? and have only a select. this how my code looks like: SqlCommand comm00 = new SqlCommand("select Cmp_nr from Companies where Cmp_name='" + companyName + "'", myconn); companyName = (string)comm00.ExecuteScalar();

                L Offline
                L Offline
                Luis Alonso Ramos
                wrote on last edited by
                #7
                string companyName = "";
                object result = comm00.ExecuteScalar();
                if(result != DBNull.Value)
                    companyName = (string) result;
                

                That will work. Also, use parameterized queries. Your code has a security hole and is prone to SQL injection attacks. See this article[^].

                Luis Alonso Ramos Intelectix Chihuahua, Mexico

                My Blog!

                1 Reply Last reply
                0
                • N NewToAspDotNet

                  Pete O'Hanlon, Thanx for your response, bud what if i don't use a datareader? and have only a select. this how my code looks like: SqlCommand comm00 = new SqlCommand("select Cmp_nr from Companies where Cmp_name='" + companyName + "'", myconn); companyName = (string)comm00.ExecuteScalar();

                  V Offline
                  V Offline
                  Vasudevan Deepak Kumar
                  wrote on last edited by
                  #8

                  Djavid j wrote:

                  SqlCommand comm00 = new SqlCommand("select Cmp_nr from Companies where Cmp_name='" + companyName + "'", myconn);

                  This query, in short, is devil's palace. I mean it is subject to SQL Injection attacks. Did you consider having it as a Stored Procedure instead?

                  Vasudevan Deepak Kumar Personal Homepage
                  Tech Gossips
                  Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE 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