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. DBNull.Value problem

DBNull.Value problem

Scheduled Pinned Locked Moved C#
databasehelpperformancequestion
10 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.
  • T Offline
    T Offline
    t4ure4n
    wrote on last edited by
    #1

    Hello i am facing a slight problem with Store a record in the database. I have a column of Type Decimal (It can have null values in the DB). i take the input in a textbox and saves it to the database.

    dataRow["SPEED"] = (this.tbSpeed.Text == "" ? DBNull.Value.ToString() : this.tbSpeed.Text);
    

    If I leave it blank it gives me following error An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll Additional information: Input string was not in a correct format.Couldn't store <> in SPEED Column. Expected type is Decimal What could be the solution to this problem

    o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°

    C B 3 Replies Last reply
    0
    • T t4ure4n

      Hello i am facing a slight problem with Store a record in the database. I have a column of Type Decimal (It can have null values in the DB). i take the input in a textbox and saves it to the database.

      dataRow["SPEED"] = (this.tbSpeed.Text == "" ? DBNull.Value.ToString() : this.tbSpeed.Text);
      

      If I leave it blank it gives me following error An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll Additional information: Input string was not in a correct format.Couldn't store <> in SPEED Column. Expected type is Decimal What could be the solution to this problem

      o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      t4urean wrote:

      DBNull.Value.ToString()

      Why are you suppling a ToString() on DBNull.Value? Don't do that just pass DBNull.Value


      Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Ready to Give up - Your help will be much appreciated. My website

      T 1 Reply Last reply
      0
      • C Colin Angus Mackay

        t4urean wrote:

        DBNull.Value.ToString()

        Why are you suppling a ToString() on DBNull.Value? Don't do that just pass DBNull.Value


        Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Ready to Give up - Your help will be much appreciated. My website

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

        If I don't use DbNull.value.tostring() then i get the following error Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'string'

        o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°

        C M 3 Replies Last reply
        0
        • T t4ure4n

          If I don't use DbNull.value.tostring() then i get the following error Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'string'

          o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          I suspect that is because of the trinary operator that you are using. These are generally considered to be bad practice. Try this:

          if (this.tbSpeed.Text == "")
          dataRow["SPEED"] = DBNull.Value;
          else
          dataRow["SPEED"] = this.tbSpeed.Text;


          Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Ready to Give up - Your help will be much appreciated. My website

          1 Reply Last reply
          0
          • T t4ure4n

            If I don't use DbNull.value.tostring() then i get the following error Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'string'

            o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            And I'd guess that that the contents of the text box will need to be converted to a decimal also.


            Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Ready to Give up - Your help will be much appreciated. My website

            1 Reply Last reply
            0
            • T t4ure4n

              Hello i am facing a slight problem with Store a record in the database. I have a column of Type Decimal (It can have null values in the DB). i take the input in a textbox and saves it to the database.

              dataRow["SPEED"] = (this.tbSpeed.Text == "" ? DBNull.Value.ToString() : this.tbSpeed.Text);
              

              If I leave it blank it gives me following error An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll Additional information: Input string was not in a correct format.Couldn't store <> in SPEED Column. Expected type is Decimal What could be the solution to this problem

              o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°

              B Offline
              B Offline
              blackjack2150
              wrote on last edited by
              #6

              IF you would use a simple "IF", it would be very simple.

              T 1 Reply Last reply
              0
              • B blackjack2150

                IF you would use a simple "IF", it would be very simple.

                T Offline
                T Offline
                t4ure4n
                wrote on last edited by
                #7

                I have used this even then i am getting the same error if (this.tbSpeed.Text == "") dataRow["SPEED"] = DBNull.Value;//"NULL"; else dataRow["SPEED"] = this.tbSpeed.Text;

                o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°

                1 Reply Last reply
                0
                • T t4ure4n

                  Hello i am facing a slight problem with Store a record in the database. I have a column of Type Decimal (It can have null values in the DB). i take the input in a textbox and saves it to the database.

                  dataRow["SPEED"] = (this.tbSpeed.Text == "" ? DBNull.Value.ToString() : this.tbSpeed.Text);
                  

                  If I leave it blank it gives me following error An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll Additional information: Input string was not in a correct format.Couldn't store <> in SPEED Column. Expected type is Decimal What could be the solution to this problem

                  o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°

                  B Offline
                  B Offline
                  blackjack2150
                  wrote on last edited by
                  #8

                  Your manner of storing values in the database seems pretty unusual. I would create a command with an "INSERT" statement as the command text and call it's ExecuteNonQuery() method. For the decimal column I would use an SQL Parameter of decimal type to avoid format errors.

                  T 1 Reply Last reply
                  0
                  • B blackjack2150

                    Your manner of storing values in the database seems pretty unusual. I would create a command with an "INSERT" statement as the command text and call it's ExecuteNonQuery() method. For the decimal column I would use an SQL Parameter of decimal type to avoid format errors.

                    T Offline
                    T Offline
                    t4ure4n
                    wrote on last edited by
                    #9

                    I have found the solution... Just applied the validation when creating the query string and it works fine to specify NUll for an empty string. I am doing in the same way you are saying but i have a seperate class that deals with it when provided with a data row.

                    o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°

                    1 Reply Last reply
                    0
                    • T t4ure4n

                      If I don't use DbNull.value.tostring() then i get the following error Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'string'

                      o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°

                      M Offline
                      M Offline
                      martin_hughes
                      wrote on last edited by
                      #10

                      Try this:

                      dataRow\["Speed"\] = this.tbSpeed.Text ?? (object) DBNull.Value;
                      
                      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