Question about checking for Nulls

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a text box on a form “txtExamDate†that I know contains nothing. I am
trying to test code that will determine the text box is nothing. I am
getting the error because there is no value in the field and I am assigning
it to a date field. My question is: How do I get the If statement to work?

Exception Details: System.FormatException: String was not recognized as a
valid DateTime

Line 723: If TypeOf txtExamDate.Text Is DBNull Then
Line 724: Else
Line 725: patientRow.ExamDate =
DateTime.Parse(txtExamDate.Text)
Line 726: End If

Source File: c:\inetpub\wwwroot\STD\Patient_Detail.aspx.vb Line: 725
 
Change the If condition to:

If (txtExamDate.Text.Trim() = "") Then
'Text box empty
Else
' DateTime parsing...
End If

I have a text box on a form "txtExamDate" that I know contains nothing. I
am
trying to test code that will determine the text box is nothing. I am
getting the error because there is no value in the field and I am assigning
it to a date field. My question is: How do I get the If statement to work?

Exception Details: System.FormatException: String was not recognized as a
valid DateTime

Line 723: If TypeOf txtExamDate.Text Is DBNull Then
Line 724: Else
Line 725: patientRow.ExamDate =
DateTime.Parse(txtExamDate.Text)
Line 726: End If

Source File: c:\inetpub\wwwroot\STD\Patient_Detail.aspx.vb Line: 725
 
a text box (web or form) will never contain DBNull.Value which is used when
checking for Database nulls.

Compair to string.Empty or for a length of 0.

-Andrew
 
Hi Jacko,

I don't know your requirement but why don't you use RequiredFieldValidator
control when we have it for such a problem.

I think it is the best solution for your problem.

Thanks,
Bhavesh Patel,
Software Engineer,
Ahmedabad-India.
 
Back
Top