请填写必填字段消息框

时间:2014-12-22 09:09:17

标签: vb.net messagebox

我正在尝试使用“Please complete required fields”消息框。 Tt会显示,但只要我尝试在文本框中输入一个字符或单击两个单选按钮之一,就会在“"Account Created"”出现后立即弹出Please complete required fields。 此外,只要字段为空,“Please complete required fields”就会显示“User already exists!”而不是“Public Class CreateAccount Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try Using conn = New System.Data.OleDb.OleDbConnection() conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _ "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Thesis\Thesis\Database2.accdb" Dim sql As String = "INSERT INTO tbl_user (username, [password],facultymember,student) " & _ "VALUES (@uname, @pwd,@fmem,@stud)" Dim sqlCom As New System.Data.OleDb.OleDbCommand(sql, conn) sqlCom.Parameters.AddWithValue("@uname", TextBox1.Text) sqlCom.Parameters.AddWithValue("@pwd", TextBox2.Text) sqlCom.Parameters.AddWithValue("@fmem", RadioButton1.Checked) sqlCom.Parameters.AddWithValue("@stud", RadioButton2.Checked) conn.Open() Dim strUsername As String = TextBox1.Text Dim boolUsernameExists As Boolean = False Using dbConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Thesis\Thesis\Database2.accdb") dbConnection.Open() Using dbCommand As New System.Data.OleDb.OleDbCommand("select count(username) from tbl_user where username like ?", dbConnection) dbCommand.Parameters.AddWithValue("@uname", strUsername) Dim result As Integer = DirectCast(dbCommand.ExecuteScalar(), Integer) If result > 0 Then boolUsernameExists = True End If End Using dbConnection.Close() End Using If boolUsernameExists Then MessageBox.Show("Username already exists!") Return End If Dim sqlRead As System.Data.OleDb.OleDbDataReader = sqlCom.ExecuteReader() conn.Close() If TextBox1.Text = "" Or TextBox2.Text = "" Or RadioButton1.Checked = False Or RadioButton2.Checked = False Then MessageBox.Show("Please complete the required fields.", "Authentication Error!", MessageBoxButtons.OK, MessageBoxIcon.Error) End If RadioButton1.Checked = False RadioButton2.Checked = False TextBox1.Text = "" TextBox2.Text = "" MessageBox.Show("Account created successfully!") Me.Hide() LoginUser.Show() End Using Catch ex As Exception MessageBox.Show("Error:" & ex.Message) End Try End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.Hide() LoginUser.Show() End Sub Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged End Sub Private Sub Textbox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress Dim KeyAscii As Short = Asc(e.KeyChar) Select Case KeyAscii Case System.Windows.Forms.Keys.Back '<--- this is for backspace Case 13 e.Handled = True SendKeys.Send("{TAB}") '<---- use to tab to next textbox or control KeyAscii = 0 Case Is <= 32 ' KeyAscii = 0 Case 48 To 57 '<--- this is for numbers Exit Sub Case 65 To 90 '<--- this is for Uppercase Alpha Exit Sub Case 97 To 122 '<--- this is for Lowercase Alpha Exit Sub Case Else e.Handled = True MessageBox.Show("You can only input letters and numbers!", "Create Account") End Select End Sub Private Sub RadioButton1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton1.CheckedChanged End Sub End Class ”。

有人可以告诉我我的代码有什么问题吗?

谢谢....

{{1}}

2 个答案:

答案 0 :(得分:0)

你应该看看Button1_click sub的逻辑,因为它真的很难理解。

  • 您打开数据库两次
  • 您已经插入了一个用户名=“”和密码=“”的用户,这就是为什么当您没有输入任何内容时获得用户存在的消息
  • 帐户已成功创建!始终显示在之后请填写必填字段,因为没有检查/返回/当字段缺失时

不知道为什么每次击键都会调用DB Insert,因为你发布的内容中没有代码

答案 1 :(得分:0)

我同意DavidSdot,您的代码无序。

这是一个可行的例子。我说可能因为我不是很擅长vb.net。因此,您可能需要更改一些内容才能使其正常工作。然而,话虽如此,以下可能会对你应该采取的正确顺序做得很好。

Try
        Using conn = New System.Data.OleDb.OleDbConnection()
            conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                                    "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Thesis\Thesis\Database2.accdb"

            If ((TextBox1.Text <> "" And TextBox2.Text <> "") And (RadioButton1.Checked <> False Or RadioButton2.Checked <> False)) Then
                conn.Open()
                Using dbCommand As New System.Data.OleDb.OleDbCommand("select count(username) from tbl_user where username like ?", conn)
                    dbCommand.Parameters.AddWithValue("@uname", TextBox1.Text)
                    Dim result As Integer = DirectCast(dbCommand.ExecuteScalar(), Integer)
                    If result = 0 Then
                        Dim sql As String = "INSERT INTO tbl_user (username, [password],facultymember,student) " & _
                                "VALUES (@uname,@pwd,@fmem,@stud)"
                        Dim sqlCom As New System.Data.OleDb.OleDbCommand(sql, conn)
                        sqlCom.Parameters.AddWithValue("@uname", TextBox1.Text)
                        sqlCom.Parameters.AddWithValue("@pwd", TextBox2.Text)
                        sqlCom.Parameters.AddWithValue("@fmem", RadioButton1.Checked)
                        sqlCom.Parameters.AddWithValue("@stud", RadioButton2.Checked)

                        Dim sqlRead As System.Data.OleDb.OleDbDataReader = sqlCom.ExecuteReader()

                        RadioButton1.Checked = False
                        RadioButton2.Checked = False

                        TextBox1.Text = ""
                        TextBox2.Text = ""

                        MessageBox.Show("Account created successfully!")

                        Me.Hide()
                        LoginUser.Show()
                    Else
                        MessageBox.Show("Username already exists!")
                        Return
                    End If
                End Using
            Else
                MessageBox.Show("Please complete the required fields.", "Authentication Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

            conn.Close()
        End Using
    Catch ex As Exception
        MessageBox.Show("Error:" & ex.Message)
    End Try