2014-12-04 23 views
0

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

但问题是我上面解释的一切都没有发生..我必须重新启动我的系统才能输入新的正确输入。我希望有人能帮助我..最重要的是我想知道如何检测来自用户的错误输入。我已经发现了如何进入新的输入,无需重新启动我的系统...帮我请..

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 

回答

0

有你想要怎样的值是一个特定的语法。你已经提到它为“D1233455”,如果它总是以这种格式,那么你可以比较输入的第一个字符并计算应该是8的输入的长度。如果这些条件是错误的,那么它应该显示一个表示该值的消息框无效。

+0

我有你的想法..但你可以告诉我的例子编码..新的VB .. .. – 2014-12-07 14:58:26

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 

希望那些帮助你的连接。