Loading a Null value into an int variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aboon
    New Member
    • Aug 2008
    • 12

    Loading a Null value into an int variable

    I am loading values from a database record into a class. One of the class properties is an int, which corresponds to an int fiel in the database.
    However, sometimes the DB value in null, which causes an exception when assigning the value to the class property.

    What is the standard proceure for dealing with this problem?

    Thanks
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    Check to make sure that value is not null first...

    Comment

    • aboon
      New Member
      • Aug 2008
      • 12

      #3
      Hi balabaster,

      What if I can not change the value in the database to not null. This is how it works...

      Employee enters start dates and en dates. If the employee is currently employed, then he only needs to enter start date, and the end date will be null and the null value is stored in the DB.

      I'm trying to get the value of start dates and end dates to calculate the working period.When it comes to the record of currently working which the date end is null. I want to set the null value from the DB to int value. Is there the way to do that?

      thanks

      Comment

      • balabaster
        Recognized Expert Contributor
        • Mar 2007
        • 798

        #4
        Originally posted by aboon
        Hi balabaster,

        What if I can not change the value in the database to not null. This is how it works...

        Employee enters start dates and en dates. If the employee is currently employed, then he only needs to enter start date, and the end date will be null and the null value is stored in the DB.

        I'm trying to get the value of start dates and end dates to calculate the working period.When it comes to the record of currently working which the date end is null. I want to set the null value from the DB to int value. Is there the way to do that?

        thanks
        The point of null is there's nothing there... null = empty = nothing = void. You can't change nothing to an integer, there's nothing to change it's like trying to produce a rabbit out of thin air... If you wish to display values that do exist, and don't display values that don't exist then you have to check and see if the data in the database is null, and if it's not null, then display it as an integer...

        if (value != null) textbox1.text = value;

        and the other way:

        if (textbox1.text = "") dbvalue = null;

        Comment

        • artov
          New Member
          • Jul 2008
          • 40

          #5
          NULL in databases in one (I guess main) reason there is Nullable<T> structure
          in .NET. In C# it is marked with ?, so in your case:

          clas MyClass
          {
          int? theValue;
          ..
          }

          If (dbValue.IsNull )
          myObject.theVal ue = null;
          else
          myObject.theVal ue = dbValue.Value;

          So, Nullable<T> works like T, but there is an value null (quite like the value
          in SQL, where there is also NULL).

          Comment

          • MrMancunian
            Recognized Expert Contributor
            • Jul 2008
            • 569

            #6
            You didn't supply us with the language you're using, so here's the VB-equivalent:

            Code:
            Dim intValue as Integer
            If Not IsDBNull(<databasevalue>) Then
              intValue = <databasevalue>
            Else
              intValue = 0
            End If

            Comment

            • aboon
              New Member
              • Aug 2008
              • 12

              #7
              thank you all of you. I'll try your advice. I'm using C#. sorry I didn't provide the language I'm using at first place.

              Comment

              Working...