Change DataGrid information on ComboBox Selection

MartinaL

Active member
Joined
Jun 13, 2006
Messages
30
Programming Experience
1-3
I have a visual Basic .net 1.1 windows application connected to an access database.

One one of the pages there is a drop down box, and depending on the selection of a project ID (the combo box is populated from a table in the access database) I want a datagrid to populated with all of the rows in a table called Task that have this Project ID.

This is the code I am using but it keeps erroring when I click on the combo box.

Code:

VB.NET:
Private Sub cbxProjectID_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxProjectID.SelectedIndexChanged
Dim ProjectID As Integer
ProjectID = cbxProjectID.SelectedValue
Dim OleDbDataAdapterTasks = New OleDb.OleDbDataAdapter
Dim ProjectsDataSet1 = New DataSet
Dim cmdTasks As OleDb.OleDbCommand
cmdTasks = New OleDb.OleDbCommand("Select * From Tasks WHERE [Proj_ID] = " & ProjectID, OleDbConnection1)
cmdTasks.CommandType = CommandType.TableDirect
OleDbDataAdapterTasks.SelectCommand = cmdTasks
OleDbConnection1.Open()
OleDbDataAdapterTasks.Fill(ProjectsDataSet1)
OleDbConnection1.Close()
grdTasks.DataSource = ProjectsDataSet1.Task
grdTasks.DataMember = "Task"
grdTasks.CaptionText = "Tasks"
End Sub

It errors on the line "OleDbDataAdapterTasks.Fill(ProjectsDataSet1)" with this error - "An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in microsoft.visualbasic.dll"

Does anyone know why?
 
Changed the code

I have changed the code to this;

VB.NET:
Private Sub cbxProjectID_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxProjectID.SelectedIndexChanged
ProjectsDataSet1.Task.Clear()
OleDbDataAdapterTasks.SelectCommand.Parameters.Add("ProjectID", cbxProjectID.SelectedValue)
OleDbDataAdapterTasks.Fill(Me.ProjectsDataSet1)
grdTasks.DataSource = ProjectsDataSet1
grdTasks.DataMember = "Task"
grdTasks.CaptionText = "Tasks"
lblID.Text = cbxProjectID.SelectedValue
End Sub

Now i don't get an error, and when you select a value in the combo box it populates the datagrid, but when I change teh combo box value the datagrid doesn't change?
 
This could be that the selectedindexchanged event happens earlier than the selecteditemchanged event. Try putting your code into the seleceditemchanged event instead.
 
Back
Top