I am trying to pull data from an SQL Database and put it into some variables to use. The problem I am having is I have a date field called DateSubtmitted that is originally set to Null in the database. When I try to pull it into a vb.net variable called SubmittedDate As Date, it is giving me the following error:
Conversion from type 'DBNull' to type 'Date' is not valid.
I thought that VB.Net data types could all hold the null value, but I guess not. I know this must be a beginner mistake, but how can I make the null value work. My code is below, the line that gives me the error is:
SubmittedDate = myDR("DateSubmitted")
Any help is apprecaited.
Dim ServUser As String = Mid(Request.ServerVariables("LOGON_USER"), 7, 15) 'Connect to the database and retrieve the users ID and other variables. Dim myConnection As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ID.mdf;Integrated Security=True;User Instance=True" Dim myConn As New SqlConnection() Dim myCmd As New SqlCommand() Dim myDR As SqlDataReader Dim table As String Dim sqlQuery As String Dim ID As String Dim SubmittedDate As Date Dim ApprovedDate As Date Dim DateChanged As Date table = "Users" sqlQuery = "SELECT * FROM " & table & " WHERE UserLogon='" & ServUser & "'" myConn.ConnectionString = myConnection myConn.Open() myCmd.Connection = myConn myCmd.CommandText = sqlQuery myDR = myCmd.ExecuteReader While myDR.Read() ID= myDR("ID") SubmittedDate = myDR("DateSubmitted") ApprovedDate = myDR("DateApproved") DateChanged = myDR("DateChangesMade") End While Dim submitted As Boolean = False If SubmittedDate <> System.DBNull.Value.ToString Then submitted = True End If myDR.Close() myConn.Close()
This post has been edited by crazybear: 20 July 2009 - 09:14 AM