如何在使用WITH END WITH语句时创建IF ELSE?

时间:2012-02-06 16:51:47

标签: vb.net if-statement with-statement

如果我想在没有学生姓名/地址的情况下显示studentID和loginID,如何在此处插入If Else语句。

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

2 个答案:

答案 0 :(得分:2)

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

那是你在找什么?

答案 1 :(得分:1)

建立盖奇的答案。您没有得到任何结果,因为您的GetInfoForStudent函数没有返回一行。发生这种情况时,行计数上的If语句没有Else子句。在那里放一些代码来添加一行或其他什么,它应该可以解决。

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