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. Cannot get value because it is DBNull

Cannot get value because it is DBNull

Scheduled Pinned Locked Moved C#
questiondatabase
9 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.
  • B Offline
    B Offline
    beqs
    wrote on last edited by
    #1

    Hi, I am using a database. And return the data to a DataSet dsPlayers. Some players have a birthday, which is a DateTime field, and some do not. These have no value so it says . I want to put their birthday to a DateTimePicker dtpBirthday, but when it is I want to give the DateTimePicker the value DateTime.Now. I have the following code, which is noit working for me. if (dsPlayer.Players[0].BirthDay.ToString() == null) { dtpBirthday.Value = DateTime.Now; } else { dtpBirthday.Value = dsPlayer.Players[0].BirthDay; } What is the best way to make this if statement?

    G A 2 Replies Last reply
    0
    • B beqs

      Hi, I am using a database. And return the data to a DataSet dsPlayers. Some players have a birthday, which is a DateTime field, and some do not. These have no value so it says . I want to put their birthday to a DateTimePicker dtpBirthday, but when it is I want to give the DateTimePicker the value DateTime.Now. I have the following code, which is noit working for me. if (dsPlayer.Players[0].BirthDay.ToString() == null) { dtpBirthday.Value = DateTime.Now; } else { dtpBirthday.Value = dsPlayer.Players[0].BirthDay; } What is the best way to make this if statement?

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      You can use the method IsNull of the DataRow class to check if the value in a cell is DBNull. --- b { font-weight: normal; }

      A 1 Reply Last reply
      0
      • G Guffa

        You can use the method IsNull of the DataRow class to check if the value in a cell is DBNull. --- b { font-weight: normal; }

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        Okay. I am not know with this. Can you give me an example how to implement this?

        G 1 Reply Last reply
        0
        • B beqs

          Hi, I am using a database. And return the data to a DataSet dsPlayers. Some players have a birthday, which is a DateTime field, and some do not. These have no value so it says . I want to put their birthday to a DateTimePicker dtpBirthday, but when it is I want to give the DateTimePicker the value DateTime.Now. I have the following code, which is noit working for me. if (dsPlayer.Players[0].BirthDay.ToString() == null) { dtpBirthday.Value = DateTime.Now; } else { dtpBirthday.Value = dsPlayer.Players[0].BirthDay; } What is the best way to make this if statement?

          A Offline
          A Offline
          Azerax
          wrote on last edited by
          #4

          How about trying this? dtpBirthday.Value = dsPlayer.Players[0].BirthDay.ToString()== DBNull.Value ? DateTime.Now : dsPlayer.Players[0].BirthDay ; This is an example of a conditional if statement and will best suit your purpose. The Conditional Statement variable = op1 == op2 ? trueValue : FalseValue ; where: op1 == op2 is your condition Life is Music listen to it before it fades away

          A 1 Reply Last reply
          0
          • A Anonymous

            Okay. I am not know with this. Can you give me an example how to implement this?

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            When you read the data from the datatable, I call it playerTable here: if (playerTable.Rows[index].IsNull["BirthDay"]) { ... store a value that represents 'no data' } else { ... get the data } --- b { font-weight: normal; }

            A 1 Reply Last reply
            0
            • A Azerax

              How about trying this? dtpBirthday.Value = dsPlayer.Players[0].BirthDay.ToString()== DBNull.Value ? DateTime.Now : dsPlayer.Players[0].BirthDay ; This is an example of a conditional if statement and will best suit your purpose. The Conditional Statement variable = op1 == op2 ? trueValue : FalseValue ; where: op1 == op2 is your condition Life is Music listen to it before it fades away

              A Offline
              A Offline
              Anonymous
              wrote on last edited by
              #6

              Hi Azerax, I tried something like that but it gives me the following error: Operator '==' cannot be applied to operands of type 'string' and 'System.DBNull' That's why I came to codeproject.

              A 2 Replies Last reply
              0
              • G Guffa

                When you read the data from the datatable, I call it playerTable here: if (playerTable.Rows[index].IsNull["BirthDay"]) { ... store a value that represents 'no data' } else { ... get the data } --- b { font-weight: normal; }

                A Offline
                A Offline
                Anonymous
                wrote on last edited by
                #7

                Thanx Guffa that will do the trick! (y) :-D

                1 Reply Last reply
                0
                • A Anonymous

                  Hi Azerax, I tried something like that but it gives me the following error: Operator '==' cannot be applied to operands of type 'string' and 'System.DBNull' That's why I came to codeproject.

                  A Offline
                  A Offline
                  Azerax
                  wrote on last edited by
                  #8

                  Ohh I am sorry :(. Please note the corrected code as follows: dtpBirthday.Value = dsPlayer.Players[0]["BirthDay"]== DBNull.Value ? DateTime.Now : dsPlayer.Players[0]["Birthday"].ToString() ; Where birthday is a field in your dataTable. Elvis (a.k.a Azerax) :cool: Life is Music listen to it before it fades

                  1 Reply Last reply
                  0
                  • A Anonymous

                    Hi Azerax, I tried something like that but it gives me the following error: Operator '==' cannot be applied to operands of type 'string' and 'System.DBNull' That's why I came to codeproject.

                    A Offline
                    A Offline
                    Azerax
                    wrote on last edited by
                    #9

                    Ohh I am sorry I got it wrong again . Please note the corrected code as follows: dtpBirthday.Value = dsPlayer.Tables["Players"].Rows[0]["Birthday"]== DBNull.Value ? DateTime.Now : dsPlayer.Tables["Players"].Rows[0]["Birthday"].ToString() ; Sorry for the trouble... Elvis (a.k.a Azerax) Life is Music listen to it before it fades

                    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