Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27184

NULL vs Date

$
0
0
Hi,

At my database I have a table with date field.. Which has NULL /empty values when nothing is added to the backend table.

DateDisabled, datatype - datetime.

Now at the front end I have an application wchic pulls these records in a select statement as shown....

Code:

Dim OrgDetails As New System.Data.SqlClient.SqlCommand(("Select..................

        Using reader As System.Data.SqlClient.SqlDataReader = OrgDetails.ExecuteReader()
                While reader.Read()

                            Dim DateDisabled As Date = FixNullDate(reader.GetValue(13))
                            txtdatedisabled.Text = DateDisabled

                    End While
End Using


Code:


Public Shared Function FixNullDate(ByVal dbvalue) As Date
                If dbvalue Is DBNull.Value Then
                        Return "00/00/00"
                Else
                        'NOTE: This will cast value to string if
                        'it isn't a string.

                        Return dbvalue
                End If
        End Function

It gives me at runtime an error message;


Conversion from string "00/00/00" to type Date is not valid.

This is when the datedisabled is NULL in the backend database. To be honest I rather pull out an emoty string when empty/NULL, because there is a calendar to enter a date.
Also, datedisabled can be NULL at one time so we need to accomodate a situation where the value is NULL or empty.

A way round is to make the datedisabled field with the datatype varchar(50)... and save the date as string and not date. I have done that and it works fine but I still want to have datetime datatype for the field on the SQL table..

Any suggestions or help please

Thank you

Viewing all articles
Browse latest Browse all 27184

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>