2013-01-15 51 views
0

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

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 

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

回答

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 

} 
+0

我把'null'。但它显示'用'DBNull.Value'替换'null' – Thanzeem

+0

其实我正在使用VB.NET。 Visual Studio 2008 – Thanzeem

+0

是的,因为这实际上是从C#(null),让我们尝试改变'CMBID.SelectedValue = Nothing' – spajce