Visual Studio错误和表达式预期?

时间:2015-09-26 08:30:07

标签: vb.net visual-studio-2010

我写了一些代码并得到了两个错误:

  1. 'Else' must be preceded by a matching 'If' or 'ElseIf'.
  2. Expression expected.
  3. 以下是代码:

    Imports System.Data.Odbc
    Public Class FormLogin
        Dim CMD As New OdbcCommand
        Dim RD As OdbcDataReader
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If TextBox1.Text = "" Or TextBox2.Text = "" Then MsgBox("Data Login Belum Lengkap!")
            Exit Sub
            Else
            Call Koneksi()
            CMD = New OdbcCommand("Select * From tbl_admin where kode_admin=" & TextBox1.Text & "and password_admin=" & TextBox2.Text &, Conn)
            RD = CMD.ExecuteReader
            RD.Read()
            If RD.HasRows Then
                Me.Close()
                FormMenuUtama.Show()
                FormMenuUtama.LoginToolStripMenuItem.Enabled = False
                FormMenuUtama.LogoutToolStripMenuItem.Enabled = True
                FormMenuUtama.MasterToolStripMenuItem.Enabled = True
                FormMenuUtama.TransaksiToolStripMenuItem.Enabled = True
                FormMenuUtama.LaporanToolStripMenuItem.Enabled = True
            Else
                MsgBox("Kode Admin atau Password Salah")
            End If
        End Sub
    
        Private Sub FormLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            TextBox1.MaxLength = 6
            TextBox2.PasswordChar = "*"
            TextBox1.Clear()
            TextBox2.Clear()
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Me.Close()
        End Sub
    End Class
    

2 个答案:

答案 0 :(得分:1)

1)不要将Then之后的代码放在If语句中。它变成了一行

所以:

If TextBox1.Text = "" Or TextBox2.Text = "" Then
MsgBox("Data Login Belum Lengkap!")
...

而不是

If TextBox1.Text = "" Or TextBox2.Text = "" Then MsgBox("Data Login Belum Lengkap!")
...

2)还有一个额外的&

所以:

CMD = New OdbcCommand("Select * From tbl_admin where kode_admin=" & TextBox1.Text & "and password_admin=" & TextBox2.Text , Conn)

而不是

CMD = New OdbcCommand("Select * From tbl_admin where kode_admin=" & TextBox1.Text & "and password_admin=" & TextBox2.Text &, Conn)

答案 1 :(得分:-1)

在End Sub。

之前添加另一个End If
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If TextBox1.Text = "" Or TextBox2.Text = "" Then MsgBox("Data Login Belum Lengkap!")
    Exit Sub
    Else
    Call Koneksi()
    CMD = New OdbcCommand("Select * From tbl_admin where kode_admin=" & TextBox1.Text & "and password_admin=" & TextBox2.Text &, Conn)
    RD = CMD.ExecuteReader
    RD.Read()
    If RD.HasRows Then
        Me.Close()
        FormMenuUtama.Show()
        FormMenuUtama.LoginToolStripMenuItem.Enabled = False
        FormMenuUtama.LogoutToolStripMenuItem.Enabled = True
        FormMenuUtama.MasterToolStripMenuItem.Enabled = True
        FormMenuUtama.TransaksiToolStripMenuItem.Enabled = True
        FormMenuUtama.LaporanToolStripMenuItem.Enabled = True
    Else
        MsgBox("Kode Admin atau Password Salah")
    End If
    End If
End Sub