Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I am using VB.Net 2008 and Access 2007.
I have Access Data Base Table Name "TableSrce". This table having two column "Home/Office" and "BillMeetrNo". I have two combo box on my vb form "cmbx1" for 1st column and cmbx2 for second column.
I want that when i select an item from 1st combo box, the 2nd combo box populated with the corresponding data.
For example when i select Home from the 1st combo box , the 2nd combo box show only the bill meeter no related to home.

I have the code but it did not work
Please point out my mistake and guide me .

VB
Imports System.Data.OleDb
Public Class Form1

   

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim cn As New OleDbConnection

        Dim cmd As New OleDbCommand
        Dim adapter As New OleDbDataAdapter
        Dim dt As New DataTable
        cn.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\UtilityBills.accdb;")
        cn.Open()
        cmd.Connection = cn
        cmd.CommandText = "SELECT Home/Office FROM SrceElectricity WHERE Home/Office ='" & ComboBox1.Text & "'"
        cmd.CommandType = CommandType.Text
        adapter.SelectCommand = cmd
        adapter.Fill(dt)
        ComboBox2.DataSource = dt
        ComboBox2.ValueMember = "EleMeeterNo"
        ComboBox2.DisplayMember = "EleMeeterNo"
    End Sub
End Class
Posted

1 solution

The
EleMeeterNo
column does not exist in your Select statement.

Also you mentioned that the name of the column was
BillMeetrNo
and not
EleMeeterNo


Hence the final code shold look something like this:

cmd.CommandText = "SELECT BillMeetrNo FROM SrceElectricity WHERE Home/Office ='" & ComboBox1.Text & "'"
 
Share this answer
 
Comments
ionline4u 29-Jun-14 12:29pm    
I have update my code but it still give me error "No value given for one or more required parameters."
The Code is given below

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

Dim cmd1 As New OleDbCommand
Dim adapter1 As New OleDbDataAdapter
Dim dt As New DataTable

con.Open()
cmd1.Connection = con
cmd1.CommandText = "SELECT EleMeeterNo FROM SrceElectricity WHERE Home/Office ='" & ComboBox1.Text & "'"
cmd1.CommandType = CommandType.Text
adapter1.SelectCommand = cmd1
adapter1.Fill(dt)
ComboBox2.DataSource = dt
ComboBox2.ValueMember = "Home/Office"
ComboBox2.DisplayMember = "Home/Office"

End Sub

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900