#1
  1. No Profile Picture
    Contributing User
    ASP Explorer (0 - 99 posts)

    Join Date
    Mar 2005
    Posts
    34
    Rep Power
    13

    Dealing with empty string


    VS 2012, SQL Server 2012, VB, n-layer

    When I splatter data onto a web form I've noticed that for variables where the value held in the database is null, the output will default to some non-null value. So for numeric values, it will be '0' and date values it will be '00:00'. I'm trying to prevent this behaviour as it's important that outputs are the same as in my database.

    I'll give an example of what I've been doing. I have a class called 'Patient' in a layer I call BusinessEntities. This class holds all the
    constructors for the object 'Patient' (surname, forename, dob, medication etc).

    I have changed the following from:
    Code:
    <VB>
    Private _t18DNAResult As Integer
    
    Public Property T18DNAResult As Integer
            Get
                Return _t18DNAResult
            End Get
            Set(ByVal value As Integer)
                _t18DNAResult = value
            End Set
    End Property
    </VB>
    to
    Code:
    <VB>
    Private _t18DNAResult As Integer?
    
    Public Property T18DNAResult As Nullable(Of Integer)
            Get
                Return _t18DNAResult
            End Get
            Set(ByVal value As Nullable(Of Integer))
                _t18DNAResult = value
            End Set
    End Property
    </VB>
    Now, on my web form, I have two routines. One loads the form controls with data from the patient object ('LoadFormControlsFromPatient'), and the other loads data from the form controls back into the patient object ('LoadPatientFromFormControls').

    Here is how it works for the example I've given above:

    <VB>
    'LoadFormControlsFromPatient,' routine

    If myPatient.T18DNAResult.HasValue Then
    txtT18DNAResult.Text = myPatient.T18DNAResult
    Else
    txtT18DNAResult.Text = String.Empty
    End If
    </VB>

    And 'LoadPatientFromFormControls'

    <VB>
    If myPatient.T18DNAResult.HasValue Then
    myPatient.T18DNAResult = txtT18DNAResult.Text
    Else
    ??????? (What to do here?)
    End If
    </VB>

    The first part works well and I no longer have the original problem of default values appearing on my web form. My problem now is the second part, i.e. how to load data back into the patient object for this particular variable, which is now an empty string.

    I've tried to convert contents of the textbox 'txtT18DNAResult', but get a 'string is in incorrect format error'. I've also tried using DBNull, but that just gives me a 'Value of type System.DBNull cannot be converted to type 'Integer?'.

    I'm not sure what I should be doing to load the contents of my textbox into the myPatient object when the contents of the textbox is an empty string.

    I hope I've explained myself OK and that someone can help.
    Last edited by maij; October 14th, 2015 at 04:08 AM.
  2. #2
  3. I do .NET for a living
    ASP Skiller (1500 - 1999 posts)

    Join Date
    Sep 2003
    Location
    Florida
    Posts
    1,558
    Rep Power
    214
    Either don't use the else (leave the value empty) or

    myPatient.T18DNAResult = txtT18DNAResult.Text
    Else
    myPatient.T18DNAResult = String.Empty
    Roger (.NET MCP)

Similar Threads

  1. Replies: 2
    Last Post: November 23rd, 2011, 10:37 AM
  2. LOGON_USER returns an empty string
    By Soporose in forum ASP Development
    Replies: 2
    Last Post: April 11th, 2009, 03:05 PM
  3. ASP 'or' statement dealing with string cases
    By Velnias in forum ASP Development
    Replies: 4
    Last Post: July 29th, 2007, 09:03 PM
  4. correct syntax for empty string
    By dev5 in forum ASP Development
    Replies: 20
    Last Post: March 26th, 2004, 04:52 PM
  5. Dealing with apostrophes in an SQL string
    By Steve Schofield in forum .NET Development
    Replies: 1
    Last Post: September 5th, 2000, 11:15 AM

IMN logo majestic logo threadwatch logo seochat tools logo