CompleteNewb
CompleteNewb

Reputation: 125

How do I make an IF ELSE while using WITH END WITH statement?

How do I insert an If Else Statement here when I want to display the studentID and loginID if there is no student name/address.

Sub ShowStudentInfo()
    Dim dt As DataTable = GetInfoForStudent("test", frmLogin.txtusername.Text, frmLogin.txtPassword.Text)

    With dt.Rows(0)
        frmLibrary.txtStudentID.Text = .Item("StudentID")
        frmLibrary.txtLoginID.Text = .Item("LoginID")
        frmLibrary.txtStudentName.Text = .Item("Student Name")
        frmLibrary.txtStudentAddress.Text = .Item("Student address")


    End With
End Sub

Upvotes: 1

Views: 566

Answers (2)

APrough
APrough

Reputation: 2701

Building off of Gage's answer. You are getting no results because your GetInfoForStudent function is not returning a row. When this happens, your If statement on the Row Count has no Else clause. Put some code there to add a row or whatever, and it should work out.

Sub ShowStudentInfo()     
    Dim dt As DataTable = GetInfoForStudent("test", frmLogin.txtusername.Text, frmLogin.txtPassword.Text)
    If dt.Rows.Count > 0  then    
        With dt.Rows(0)         
            if String.IsNullOrEmpty(.Item("Student Name")) AndAlso String.IsNullOrEmpty(.Item("Student Name")) then                     
                frmLibrary.txtStudentID.Text = .Item("StudentID")             
                frmLibrary.txtLoginID.Text = .Item("LoginID")         
            else             
                frmLibrary.txtStudentName.Text = .Item("Student Name")                             
                frmLibrary.txtStudentAddress.Text = .Item("Student address")         
            end if      
        End With    
    Else
        'Do something here to add new row, etc.
    End If

End Sub

Upvotes: 1

Gage
Gage

Reputation: 7493

Sub ShowStudentInfo()
    Dim dt As DataTable = GetInfoForStudent("test", frmLogin.txtusername.Text, frmLogin.txtPassword.Text)

    With dt.Rows(0)
        if .Item("Student Name")="" and .Item("Student address")="" then
            frmLibrary.txtStudentID.Text = .Item("StudentID")
            frmLibrary.txtLoginID.Text = .Item("LoginID")
        else
            frmLibrary.txtStudentName.Text = .Item("Student Name")
            frmLibrary.txtStudentAddress.Text = .Item("Student address")
        end if

    End With
End Sub

Is that what you were looking for?

Upvotes: 2

Related Questions