Hey everyone,
I know that there are hundreds of articles online about this same error, but I have been going at this for almost 2 days and it's time I just started a thread of my own.
In my application, I have a textbox that has a CalendarExtender hooked up to it as shown here;
<asp:TextBox ID="ActualAdmitDate" runat="server"></asp:TextBox>
<asp:CalendarExtender ID="Calendarextender2" runat="server" CssClass="calStyle"
Enabled="True" Format="MMMM d, yyyy" PopupButtonID="calImg3"
TargetControlID="ActualAdmitDate">
</asp:CalendarExtender><img id="calImg3" alt="" class="style10" src="Styles/calendar.png" />
I also have more textboxes and controls that take information, but this issue is only happening with the date selections.
Basically, it's possible that a user doesn't select a date out of this CalendarExtender textbox, leaving the value as ""
I am trying to figure out how I can either store a Null value or "" in my database if the user doesn't select a date in the CalendarExtender. Here is the code I am currently using, along with the stored procedure that the values are passed into. Here is one condition of the code being used
The DecDateMade variable is the variable that is throwing the error.
Dim DecMadeBy As String
Dim DecDateMade As Date
Dim DecTimeMade As String
Dim ContID As String
Dim ContText As String
Dim ReferElsewhere As String
Dim ReferLocation As String
If B4A.Checked = True Then
ReferElsewhere = "Yes"
If B4McLean.SelectedIndex <> 0 Then
ReferLocation = B4McLean.SelectedValue
Else
ReferLocation = B4Else.Text
End If
ElseIf B4B.Checked = True Then
ReferElsewhere = "No"
ReferLocation = "N/A"
End If
If CheckBoxList1.SelectedValue = "" And CheckBoxList2.SelectedValue = "" And PatNoCriteria.Checked = False And ClosedOther.Checked = False Then
DecMadeBy = MadeBy.Text
[B] If (DecisionDate.Text = "") Then
DecDateMade = Nothing
Else
DecDateMade = Convert.ToDateTime(DecisionDate.Text)
End If[/B]
If (DecisionTime.Text.ToString = "") Then
DecTimeMade = "N/A"
Else
DecTimeMade = DecisionTime.Text
End If
ContID = ""
ContText = ""
AdmDecNoAdmit(DecDateMade, DecTimeMade, DecMadeBy, ContID, ContText, ReferElsewhere, ReferLocation)
End If
Stored Procedure:
Protected Sub AdmDecNoAdmit(ByVal DecDateMade As Date, ByVal DecTimeMade As String, ByVal NoAdmDecMadeBy As String, ByVal Cont As String, ByVal ContText As String, ByVal ReferElsewhere As String, ByVal ReferLocation As String)
Dim strSQL As String = "INSERT into NoAdmit(InquiryID_fk,ContID, ContText, ReferElsewhere, ReferLocation, MadeBy, DecisionDate, DecisionTime)VALUES(@InquiryID, @ContID, @ContText, @ReferElsewhere, @ReferLocation, @MadeBy, @DecDateMade, @DecTimeMade)SELECT ID = SCOPE_IDENTITY()"
Dim myConn As New SqlConnection(StringConnection)
Dim queryCommand As New SqlCommand
queryCommand.Connection = myConn
With queryCommand
.CommandType = CommandType.Text
.CommandText = strSQL
.Parameters.AddWithValue("@InquiryID", Session("NewInquiryID"))
.Parameters.AddWithValue("@ContID", Cont)
.Parameters.AddWithValue("@ContText", ContText)
.Parameters.AddWithValue("@ReferElsewhere", ReferElsewhere)
.Parameters.AddWithValue("@ReferLocation", ReferLocation)
.Parameters.AddWithValue("@MadeBy", NoAdmDecMadeBy)
.Parameters.AddWithValue("@DecDateMade", DecDateMade)
.Parameters.AddWithValue("@DecTimeMade", DecTimeMade)
myConn.Open()
Session("DecID") = .ExecuteScalar()
Session("DecID") = Convert.ToInt32(Session("DecID"))
'MsgBox(Session("DecID"), MsgBoxStyle.Critical)
myConn.Close()
End With
queryCommand.Dispose()
Dim UpdSQL As String = "Update Inquiry SET DecisionMade = 'Do Not Admit', DecisionID='" & Session("DecID") & "' WHERE Inquiry.ID = '" & Session("NewInquiryID") & "'"
Dim UpdateCommand As New SqlCommand
UpdateCommand.Connection = myConn
With UpdateCommand
.CommandType = CommandType.Text
.CommandText = UpdSQL
myConn.Open()
.ExecuteNonQuery()
myConn.Close()
End With
myConn.Dispose()
UpdateCommand.Dispose()
End Sub