使用组合框VB​​.NET进行数据库记录搜索

时间:2013-01-15 13:38:32

标签: vb.net sql-server-2005 combobox

我尝试使用cmbo框搜索sql数据库中的数据记录。它显示错误“多部分标识符”System.Data.DataRowView“无法找到”在“''''附近的”不正确的语法“之后。我的代码如下。

 Private Sub BTNEDIT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNEDIT.Click
    refreshComboBox()
    CMBID.SelectedText = "Select"
 End Sub

 Private Sub refreshComboBox()
    getConnect()
    Try
        Conn.Open()
        Dim strSQL As String = "SELECT * FROM EMPLOYEE ORDER BY EMP_ID ASC"
        Dim da As New SqlDataAdapter(strSQL, Conn)
        Dim ds As New DataSet
        da.Fill(ds, "EMPLOYEE")
        CMBID.DataSource = ds.Tables(0)
        CMBID.DisplayMember = "EMP_ID"
        CMBID.ValueMember = "ID"
        CMBID.SelectedValue = 0
        CMBID.Invalidate()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        Conn.Close()
    End Try
End Sub

Private Sub CMBID_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CMBID.SelectedIndexChanged
    Dim gend As String
    getConnect()
    Dim strSQL As String = "SELECT * FROM EMPLOYEE WHERE EMP_ID=" & CMBID.Text & ""
    Try
        Dim da As SqlDataAdapter = New SqlDataAdapter(strSQL, Conn)
        Dim ds As DataSet = New DataSet
        da.Fill(ds, "EMPLOYEE")
        Dim dt As DataTable = ds.Tables("EMPLOYEE")
        Dim row As DataRow
        For Each row In dt.Rows
            ID.Text = row("ID")
            TXTNAME.Text = row("EMP_NAME")
            TXTFNAME.Text = row("EMP_FNAME")
            gend = row("EMP_GENDER")
            If gend = "MALE" Then
                RBMALE.Checked = True
                RBFEMALE.Checked = False
            ElseIf gend = "FEMALE" Then
                RBFEMALE.Checked = True
                RBMALE.Checked = False
            End If
            DTPEMPDOB.Value = row("EMP_DOB")
            TXTCASTE.Text = row("EMP_CAST")
            CMBDEPT.Text = row("EMP_DEPART")
            CMBDESIG.Text = row("EMP_DESIG")
            DTPEMPDOJ.Value = row("EMP_DOJ")
            MTXTSAL.Text = row("EMP_SALARY")
            MTXTPFESI.Text = row("EMP_PF_ESI")
            TXTBRANCH.Text = row("EMP_BRANCH")
            MTXTCONTACT.Text = row("EMP_CONTACT")
            RTXTADDRESS.Text = row("EMP_ADDRESS")
        Next row
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        Conn.Close()
    End Try
End Sub

请尝试检查我的代码并给我解决方案。

1 个答案:

答案 0 :(得分:0)

    Dim strSQL As String = "SELECT * FROM EMPLOYEE ORDER BY EMP_ID ASC"
    Dim da As New SqlDataAdapter(strSQL, Conn)
    Dim ds As New DataSet
    da.Fill(ds, "EMPLOYEE")
    CMBID.DataSource = ds.Tables(0)
    CMBID.DisplayMember = "EMP_ID"
    CMBID.ValueMember = "ID"
    CMBID.SelectedValue = Nothing
    //CMBID.Invalidate()



Private Sub CMBID_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CMBID.SelectedIndexChanged
{
  If clientComboBox.SelectedValue Is Nothing Then
Return
  End If

  If CMBID.SelectedValue.toString() = "System.Data.DataRowView" Then
Return
  End If
   //Do stuff

}