hi friends i m doing project in vb and database is microsoft access

in my form i have 3 radiobuttons and 3 combobox when i select radio button then respective combobox enable and remainig disable i load all the data in combobox through data reader the problem is user can select different radio button so as query also changes to fetch the data from databse whenever i select 1 rasio buttons its work whenever select it not work pl help

Do While dr.Read
            ComboBox1.Items.Add(dr(0).ToString)
            ComboBox2.Items.Add(dr(7).ToString)
            ComboBox3.Items.Add(dr(1).ToString)


  Dim danew As OleDbDataAdapter
        Dim dsnew As DataSet
        Dim query As String = ""
        Dim name As String
        Dim id As String
        Dim room As Integer
        id = ComboBox1.SelectedItem
        room = ComboBox2.SelectedItem
        name = ComboBox3.SelectedItem
        Dim cmdbobj As OleDbCommand

        If ComboBox1.SelectedItem Then
            query = "select * from Member where Id=" & id

      

        ElseIf ComboBox2.SelectedItem Then
            query = "select * from Member where RoomNo='" & room & "'"
        Else
            query = "select * from Member where MemberName='" & name & "'"
        End If
        danew = New OleDbDataAdapter(query, con)
        dsnew = New DataSet

        con.Open()
      
        Try
            cmdbobj = New OleDbCommand(query, con)
            danew.Fill(dsnew, "Member")

            DataGridView1.DataSource = dsnew
            DataGridView1.DataMember = "Member"

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        con.Close()

Be aware that lines 13 to 15 can fail if no selected item in combobox.

Be aware that the selected item in combobox does not 'disapears' if you change the combo box from enabled to disabled.

So I would sugges you to change your code to some thing like:

.
  .
  .
  Dim query As String = ""
  Dim name As String
  Dim id As String
  Dim room As Integer
  room = ComboBox2.SelectedItem
  name = ComboBox3.SelectedItem

  If RadioButton1.Checked Then
       If ComboBox1.SelectedIndex >= 0 Then
            query = "select * from Member where Id=" & ComboBox1.SelectedItem
       End IF
  ElseIf RadioButton2.Checked then
       If ComboBox2.SelectedIndex >= 0 Then
            query = "select * from Member where RoomNo='" & ComboBox2.SelectedItem & "'"
       End if
  ElseIf RadioButton3.Checked then
       If ComboBox3.SelectecIndex >=0 Then
            query = "select * from Member where MemberName='" & ComboBox3.selectedItem & "'"
  End If

  If query.Length > 0 then
  '
  ' Update here
  '
  End if

  .
  .
  .

Additionally, on the RadioButton CheckedChanged event, reset the corresponding ComboBox SelectedIndex to -1 (no selection)

Hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.