sidder
sidder

Reputation: 11

vb .net null condition

this is my code -

            Dim provider As CultureInfo = CultureInfo.InvariantCulture
            Dim a1 As DateTime = Nothing
            insexp = DateTime.ParseExact(date1.SelectedValue, "MMMM yyyy", provider)
            If a1.Month = Today.Month AndAlso a1.Year = Today.Year Then
                a1 = Today.Date
            End If

this will work only if date1.selectedvalue is not null, but will crash if it is null. how do i give if condition to run this only if not null? thanks

Upvotes: 1

Views: 990

Answers (2)

Duha
Duha

Reputation: 829

 Dim provider As CultureInfo = CultureInfo.InvariantCulture
        Dim a1 As DateTime = Nothing
        If not String.IsNullOrEmpty(date1.SelectedValue) Then         
    insexp = DateTime.ParseExact(date1.SelectedValue, "MMMM yyyy", provider)
         End If
    If a1.Month = Today.Month AndAlso a1.Year = Today.Year Then
        a1 = Today.Date
    End If

Upvotes: 1

tster
tster

Reputation: 18237

If date1.SelectedValue IsNot Nothing Then
    ...
End If

Upvotes: 5

Related Questions