S VG

S VG

  • NA
  • 1
  • 0

InvalidOperationException was unhandled

Jan 7 2008 1:01 PM

hi. I am trying to write a "simple"program to access and edit data from an access database. trying to get the data to display from a combobox selection to an unbound datagrid. when I run the program I get the error and the explanation "Not allowed to change the 'ConnectionString' property. The connection's current state is open."

below is the  code I am using and on my form I have the datagrid "dgMaleLeague" and combobox "cbMaleName"

hope somebody knows of a simple solution I am very much a beginner.

Thanks

Public Class MaleLeague

Dim conMDB As New OleDb.OleDbConnection

Dim cmdMDB As New OleDb.OleDbCommand

Dim rdrMDB As OleDb.OleDbDataReader

Private Sub MaleLeague_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

conMDB.ConnectionString = _

"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=J:\tennis.mdb"

cmdMDB.Connection = conMDB

cmdMDB.CommandText = "SELECT * FROM Player WHERE(Gender = 'Male')"

cmdMDB.CommandType = CommandType.Text

conMDB.Open()

rdrMDB = cmdMDB.ExecuteReader

While rdrMDB.Read

cbMaleName.Items.Add(rdrMDB("Surname"))

End While

cbMaleName.SelectedIndex = 0

rdrMDB.Close()

conMDB.Close()

 

End Sub

Private Sub loadMaleLeague()

conMDB.ConnectionString = _

"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=J:\tennis.mdb"

cmdMDB.Connection = conMDB

cmdMDB.CommandText = "MaleLeague"

cmdMDB.CommandType = CommandType.StoredProcedure

cmdMDB.Parameters.Add("@MaleLeague", OleDb.OleDbType.Char)

cmdMDB.Parameters.Item("@MaleLeague").Value = cbMaleName.Text

rdrMDB = cmdMDB.ExecuteReader

Dim adapter As New OleDb.OleDbDataAdapter

adapter.SelectCommand = cmdMDB

Dim table As New DataSet

adapter.Fill(table)

dgMaleLeague.DataSource = table

table.Tables(0).TableName = "MaleLeague"

dgMaleLeague.DataMember = "MaleLeague"

With dgMaleLeague

.Columns(0).Width = 100

.Columns(1).Width = 100

.Columns(2).Width = dgMaleLeague.Width - 204

End With

rdrMDB.Close()

conMDB.Close()

End Sub

Private Sub cbMaleName_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbMaleName.SelectedIndexChanged

loadMaleLeague()

End Sub

End Class