0

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
3
Contributors
6
Replies
8
Views
5 Years
Discussion Span
Last Post by mikev2
0

The exception is being thrown here;
If (DecisionDate.Text = "") Then
DecDateMade = Nothing <-------
Else
DecDateMade = Convert.ToDateTime(DecisionDate.Text)
End If

and I have tried using DBNull as well as System.DbNull.Value, as well as just empty double quotes "", converted to a string, and tried. been trying everything I can really think of. Also, tried putting condition into the stored procedure like
.parameters.add(@DecDate, sqldbtype.date)
If DecDateMade.text = "" then
.paramater(@DecDate).value = DBNull
else
.parameter(@DecDate).value = Convert.toDateTime(DecDateMade.Text)
end if

I know that might not be perfectly correct syntax, I was just giving an example.
Thanks for taking a look

0

well, i know ive had the same problem a few months ago in a school project, dont have access to my code at this time but ill look tonight and get back to you.

in the mean time try setting VBNull or just Null instead of CBNull or Nothing, otherwise i think you could have a second stored procedure without the date parameter, and simply call that procedure when the textbox is empty, and the original procedure when its not.

0

Try this:

imports System.Data.SqlTypes

If (DecisionDate.Text = "") Then
    DecDateMade = SqlDateTime.Null
Else
    DecDateMade = Convert.ToDateTime(DecisionDate.Text)
End If

Edited by mikev2: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.