首页 > 解决方案 > 来自不同形式的错误会赶上其他形式

问题描述

如果您能帮助我解决 vb.net 项目中的奇怪行为,我将不胜感激。

我有一个登录表单。用户提供用户名和密码,如果凭据正常,应用程序会转到另一个表单

Try
        con.Open()
        Dim sql As New SQLite.SQLiteCommand("Select * From users where username = '" & UsernameTextBox.Text & "' and userpass = '" & PasswordTextBox.Text & "'", con)
        Dim dr As SQLite.SQLiteDataReader = sql.ExecuteReader
        Dim dt As New DataTable
        dt.Load(dr)
        If dt.Rows.Count = 1 Then
            'there is only one user
            Dim f As New MainFrm
            f.lbluser.Tag = dt.Rows(0)(3)
            f.lbluser.Text = dt.Rows(0)(1)
            f.ShowDialog()
            Me.Close()
        ElseIf dt.Rows.Count = 0 Then
            'credentials are wrong
            MessageBox.Show("No user with those credentials. Try again!", "Wrong credentials", MessageBoxButtons.OK, MessageBoxIcon.Error)
            UsernameTextBox.Text = ""
            PasswordTextBox.Text = ""
            UsernameTextBox.Focus()
            Exit Sub
        Else
            'credentials are multiple in the database
            MessageBox.Show("Issues with the credentials. Code error: LFUE-1010", "Multiple users | LFUE-1010", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If
        con.Close()
    Catch ex As Exception
        con.Close()
        'there are problems with the connection / or sql
        MessageBox.Show("Connection issues - code error: LFDC-1020 " & ex.Message, "DB connection error | LFDC-1020", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

这完美地工作。在另一种形式中,假设我有一个按钮:

If cbkeywords.SelectedIndex = -1 Then
        MsgBox("select something")
        Exit Sub
    End If
    Dim d = DirectCast(cbkeywords.SelectedItem, DataRowView).Item("Keyword")
    If lstkeywords.Items.Count > 0 Then
        For i As Integer = 0 To lstkeywords.Items.Count - 1
            If lstkeywords.Items(i).contains(d) Then
                MessageBox.Show("there is already value " & d, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            Else
                dtkeys.Rows.Add(cbkeywords.SelectedValue, d)
                lstkeywords.Items.Add(d)
            End If
        Next
    Else
        dtkeys.Rows.Add(cbkeywords.SelectedValue, d)
        lstkeywords.Items.Add(d)
    End If

如果代码有错误,则应用程序转到登录表单 Catch ex As Exception 并获取消息。

每当我遇到不同形式的代码问题时,应用程序都会转到 Login Form Catch ex As Exception。

我清理了解决方案、项目、重新启动,但没有任何改变。有任何想法吗?提前致谢

标签: vb.netformstry-catch

解决方案


先做数据库代码,然后再做其他代码。将它们分开。否则,当你这样做时f.ShowDialog(),它仍然在 Try...Catch 中。

像这样的东西:

Dim dt As New DataTable

Dim con As New SqliteConnection(yourConnectionString)

Try
    Dim sql As New SQLite.SQLiteCommand("Select * From users where username = '" & UsernameTextBox.Text & "' and userpass = '" & PasswordTextBox.Text & "'", con)
    Dim dr As SQLite.SQLiteDataReader = sql.ExecuteReader()
    dt.Load(dr)

Catch ex As Exception
    con.Close()
    'there are problems with the connection / or sql
    MessageBox.Show("Connection issues - code error: LFDC-1020 " & ex.Message, "DB connection error | LFDC-1020", MessageBoxButtons.OK, MessageBoxIcon.Error)

Finally
    If Sql IsNot Nothing Then
        Sql.Dispose()
    End If

    If con IsNot Nothing Then
        con.Dispose()
    End If

End Try

If dt.Rows.Count = 1 Then
    'there is only one user
    Dim f As New MainFrm
    f.lbluser.Tag = dt.Rows(0)(3)
    f.lbluser.Text = dt.Rows(0)(1)
    f.ShowDialog()
    Me.Close()
ElseIf dt.Rows.Count = 0 Then
    'credentials are wrong
    MessageBox.Show("No user with those credentials. Try again!", "Wrong credentials", MessageBoxButtons.OK, MessageBoxIcon.Error)
    UsernameTextBox.Text = ""
    PasswordTextBox.Text = ""
    UsernameTextBox.Focus()
    Exit Sub
Else
    'credentials are multiple in the database
    MessageBox.Show("Issues with the credentials. Code error: LFUE-1010", "Multiple users | LFUE-1010", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Exit Sub
End If

您不需要打开连接,因为该.Load方法会执行此操作,然后将连接保持在原来的状态。


推荐阅读