使用ComboBox和TextBox进行搜索

时间:2015-08-13 06:19:40

标签: mysql vb.net combobox

我在vb中有一个小型作业,可以搜索学生记录

搜索文本框用于记录和选择组合框中的列,

enter image description here

Private Sub search()
    'search method for Search Record'
    Try
        If cmbKeywords.Text = "ID" Then
            Dim dbQuery As String = "Select * from tblstudents where id =" & txtSearch.Text
        ElseIf cmbKeywords.Text = "Name" Then
            Dim dbQuery As String = "Select * from tblstudents where id =" & txtSearch.Text
        ElseIf cmbKeywords.Text = "Address" Then
            Dim dbQuery As String = "Select * from tblstudents where id =" & txtSearch.Text
        ElseIf cmbKeywords.Text = "Contact" Then
            Dim dbQuery As String = "Select * from tblstudents where id =" & txtSearch.Text
        ElseIf cmbKeywords.Text = "Civil Status" Then
            Dim dbQuery As String = "Select * from tblstudents where id =" & txtSearch.Text
        End If
        Dim dbConnection As New MySqlConnection(dbConString)
        Dim dbCmd As New MySqlCommand(dbQuery, dbConnection)
        Dim dbReader As MySqlDataReader
        dbConnection.Open()
        dbReader = dbCmd.ExecuteReader()

        dgvRecords.ReadOnly = True

        dgvRecords.ColumnCount = 6
        dgvRecords.Columns(0).Name = "Stud ID"
        dgvRecords.Columns(1).Name = "Name"
        dgvRecords.Columns(2).Name = "Address"
        dgvRecords.Columns(3).Name = "Gender"
        dgvRecords.Columns(4).Name = "Status"
        dgvRecords.Columns(5).Name = "Contact No"

        While dbReader.Read()
            dgvRecords.Rows.Add(dbReader("id"), dbReader("lastname") & ", " & dbReader("firstname") & " " & dbReader("middlename"), dbReader("address"), dbReader("gender"), dbReader("civil_status"), dbReader("contact"))
        End While
        dbReader.Close()
        dbConnection.Close()
    Catch ex As Exception
        MsgBox("ERROR: " & ErrorToString(), MsgBoxStyle.Critical)
    End Try
End Sub

我在这一行有错误。

Dim dbCmd As New MySqlCommand(dbQuery, dbConnection)

它说dbQuery没有定义。 有人可以帮助我吗? 对不起新手在vb这里。

2 个答案:

答案 0 :(得分:0)

这是范围问题。如果您在dbQueryIf块中声明ElseIf,则它仅存在于该块内。所有这些声明实际上都创建了单独的变量,这些变量在End If语句中不再存在。

解决方案是仅在dbQuery语句之前声明If变量一次,然后在这些多个位置分配它。因为变量是在更宽的范围内声明的,所以可以在If Else行之后访问它。

答案 1 :(得分:0)

这取决于dbQuery变量的范围。当你在If- ElseIf-End If内声明它时,它是这个条件的局部变量,在它不存在的条件之外。你必须使它成为整个函数的本地,所以你可以通过函数引用它。以下是更好的答案。 试试这个

Private Sub search()
'search method for Search Record'
Dim dbQuery As String
Try
    If cmbKeywords.Text = "ID" Then
        dbQuery = "Select * from tblstudents where id =" & txtSearch.Text
    ElseIf cmbKeywords.Text = "Name" Then
        dbQuery = "Select * from tblstudents where id =" & txtSearch.Text
    ElseIf cmbKeywords.Text = "Address" Then
        dbQuery = "Select * from tblstudents where id =" & txtSearch.Text
    ElseIf cmbKeywords.Text = "Contact" Then
        dbQuery = "Select * from tblstudents where id =" & txtSearch.Text
    ElseIf cmbKeywords.Text = "Civil Status" Then
        dbQuery = "Select * from tblstudents where id =" & txtSearch.Text
    End If
    Dim dbConnection As New MySqlConnection(dbConString)
    Dim dbCmd As New MySqlCommand(dbQuery, dbConnection)
    Dim dbReader As MySqlDataReader
    dbConnection.Open()
    dbReader = dbCmd.ExecuteReader()

    dgvRecords.ReadOnly = True

    dgvRecords.ColumnCount = 6
    dgvRecords.Columns(0).Name = "Stud ID"
    dgvRecords.Columns(1).Name = "Name"
    dgvRecords.Columns(2).Name = "Address"
    dgvRecords.Columns(3).Name = "Gender"
    dgvRecords.Columns(4).Name = "Status"
    dgvRecords.Columns(5).Name = "Contact No"

    While dbReader.Read()
        dgvRecords.Rows.Add(dbReader("id"), dbReader("lastname") & ", " & dbReader("firstname") & " " & dbReader("middlename"), dbReader("address"), dbReader("gender"), dbReader("civil_status"), dbReader("contact"))
    End While
    dbReader.Close()
    dbConnection.Close()
Catch ex As Exception
    MsgBox("ERROR: " & ErrorToString(), MsgBoxStyle.Critical)
End Try
End Sub