Validate Date Value using VB.Net in ASP.Net

Tevin
 
on Aug 11, 2021 04:59 AM
672 Views

I have a form with 4 different dates that is not mandatory. I want to validate a date if one is selected and if a date is not selected i want to insert "1900-01-01" into the database. Below is my if statement but it is not working correctly:

Dim dtpDateReceived As Date = New Date
Dim dtpDateAssessed As Date = New Date
Dim dtpDateManufactured As Date = New Date
Dim dtpCustomerBuildDate As Date = New Date

txtDateRecieved.Text = "1900-01-01"
txtDateAssessed.Text = "1900-01-01"
txtDateManufactured.Text = "1900-01-01"
txtCustomerBuildDate.Text = "1900-01-01"

If Not Date.TryParse(txtDateRecieved.Text, dtpDateReceived) Then
lblMessageError.Visible = True
lblMessageError.Text = "Incorrect Date Recieved"

ElseIf Not Date.TryParse(txtDateAssessed.Text, dtpDateAssessed) Then
lblMessageError.Visible = True
lblMessageError.Text = "Incorrect Date Assessed"

ElseIf Not Date.TryParse(txtDateManufactured.Text, dtpDateManufactured) Then
lblMessageError.Visible = True
lblMessageError.Text = "Incorrect Date Manufactured"

ElseIf Not Date.TryParse(txtCustomerBuildDate.Text, dtpCustomerBuildDate) Then
lblMessageError.Visible = True
lblMessageError.Text = "Incorrect Customer Build Date"

Else

mCR.InsertClaimReturn(....)

End If

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Results 1 - 5 of 7
dharmendr
 
on Aug 11, 2021 05:11 AM

Use below code.

Dim dtpDateReceived As Date
Dim dtpDateAssessed As Date
Dim dtpDateManufactured As Date
Dim dtpCustomerBuildDate As Date

If Not Date.TryParse(txtDateRecieved.Text, dtpDateReceived) Then
    dtpDateReceived = New Date(1900, 1, 1)
    lblMessageError.Visible = True
    lblMessageError.Text = "Incorrect Date Recieved"
ElseIf Not Date.TryParse(txtDateAssessed.Text, dtpDateAssessed) Then
    dtpDateAssessed = New Date(1900, 1, 1)
    lblMessageError.Visible = True
    lblMessageError.Text = "Incorrect Date Assessed"
ElseIf Not Date.TryParse(txtDateManufactured.Text, dtpDateManufactured) Then
    dtpDateManufactured = New Date(1900, 1, 1)
    lblMessageError.Visible = True
    lblMessageError.Text = "Incorrect Date Manufactured"
ElseIf Not Date.TryParse(txtCustomerBuildDate.Text, dtpCustomerBuildDate) Then
    dtpCustomerBuildDate = New Date(1900, 1, 1)
    lblMessageError.Visible = True
    lblMessageError.Text = "Incorrect Customer Build Date"
Else
    mCR.InsertClaimReturn(....)
End If

 

Tevin
 
on Aug 11, 2021 05:19 AM

When i try using this then my insert does not work, after validating the date it goes straight to my "End If".

dharmendr
 
on Aug 11, 2021 06:57 AM
on Aug 11, 2021 06:57 AM
Tevin says:
Dim dtpDateReceived As Date = New Date
Dim dtpDateAssessed As Date = New Date
Dim dtpDateManufactured As Date = New Date
Dim dtpCustomerBuildDate As Date = New Date

Do you want to validate each variable and set 1900-01-01 before insert?

Tevin
 
on Aug 11, 2021 08:04 AM

No I want to validate the date only if the user selected a date but if the user does not select a date i want to insert 1900-01-01.

dharmendr
 
on Aug 11, 2021 11:18 PM

Do you want to validate for dtpDateReceived, dtpDateAssessed, tpDateManufactured, dtpCustomerBuildDate Date variable?

For all?

Results 1 - 5 of 7