如何检测用户vb的错误输入

时间:2014-12-04 09:55:32

标签: mysql vb.net

我希望我的系统能够检测到用户的错误输入。例如,我使用“matric_no”作为我的数据库中的主键,主键的数据是“D1233455”。当用户想要删除数据库中的记录时,他们必须键入主键,然后按删除按钮。但如果主键输入不正确,系统将在按下删除按钮时继续检测错误,我不必重新启动程序来更正输入。我使用编码链接数据库。

但问题是我上面解释的一切都没有发生..我必须重新启动我的系统以输入新的正确输入。我希望有人可以帮助我..最重要的是我想知道如何检测用户的错误输入。我已经找到了如何在不重新启动系统的情况下输入新输入...请帮帮我..

Dim query As String = "delete from Student_Database where Matric_No = '" & TextBox1.Text & "'"
Dim query1 As String = "delete from Fee_Database where Matric_No = '" & TextBox1.Text & "'"         
        If Me.TextBox1.Text = String.Empty Then
            Notify the user of the invalid value.
            MessageBox.Show("Please enter a value.", _
                            "Required Field", _
                            MessageBoxButtons.OK, _
                            MessageBoxIcon.Warning)

        ElseIf ("I need the solution for this part") Then
            MessageBox.Show("Record cannot be trace. Please enter the correct ID", "Required Field", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        Else
            objResult.queryCommand(query)
            objResult.queryCommand(query1)
            MessageBox.Show("Record has been deleted.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

2 个答案:

答案 0 :(得分:0)

是否有特定的语法表达您的价值。您已将其描述为" D1233455"如果它始终采用该格式,那么您可以只比较输入的第一个字符并计算输入的长度,该值应为8.如果这些条件为假,则应显示一个消息框,指出该值无效。

答案 1 :(得分:0)

您需要一个选择所有matric_no行的选择查询,然后将其与文本框中的值进行比较。

如果我正在使用MS-Access数据库,那么我使用OleDb连接,如果您正在使用其他数据库,只需将连接更改为您使用的连接。

这里是我使用的连接

Imports System.Data.OleDb

Module Connection
    Public Conn As New OleDbConnection
    Public CMD As OleDbCommand
    Public DA As OleDbDataAdapter
    Public DR As OleDbDataReader
    Public DS As DataSet
    Public DT As DataTable
    Public STR As String

    Public Sub Connect()
        Try
             STR = "provider=microsoft.ACE.OLEDB.12.0;data source=" & Application.StartupPath & "\your_database_name_here.mdb"
             Conn = New OleDb.OleDbConnection(STR)
             If Conn.State = ConnectionState.Closed Then
                 Conn.Open()
             End If
        Catch ex As Exception
             MsgBox("Failed to connect database")
        End Try
    End Sub
End Module

然后是您的问题代码

'Add this to the top of the code
'Actually before the `Public Class <your_form_name>`
'So it's like
Imports System.Data.OleDb

Public Class blablabla
.....

Private Sub DeleteData()

Dim query As String = "delete from Student_Database where Matric_No = '" & TextBox1.Text & "'"
Dim query1 As String = "delete from Fee_Database where Matric_No = '" & TextBox1.Text & "'"

'Changes
Dim query2 As String = "SELECT * FROM Student_Database where Matric_No = '" & TextBox1.Text & "'" 'This query to detect that user input is same to any Matric_No rows.

Connect()
CMD = New OleDbCommand(query2, Conn)
DR = CMD.ExecuteReader
DR.Read
If DR.HasRows Then
    If Me.TextBox1.Text = String.Empty Then
        Notify the user of the invalid value.
        MessageBox.Show("Please enter a value.", _
                        "Required Field", _
                        MessageBoxButtons.OK, _
                        MessageBoxIcon.Warning)
        Else
            objResult.queryCommand(query)
            objResult.queryCommand(query1)
            MessageBox.Show("Record has been deleted.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
Else 'Here you detect the wrong input
    MessageBox.Show("Record cannot be trace. Please enter the correct ID", "Required Field", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
Conn.Close()

End Sub

希望有人帮助你。