Problems Firing an Event Handler

S

shookim

I am trying to populate a combobox and then fire an indexchanged event
handler. My combobox populates fine, the problem is when it gets to
the datasource object (cmbClient.DataSource = ds), it immediately goes
to the event handler (SelectedIndexChanged), which later causes an
error. This is my code:

Private Sub PopulateClient()
Dim sqlConn As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))

Try
Dim ds As New DataSet("ds")
Dim objDataAdapter As SqlDataAdapter = New SqlDataAdapter
Dim cmd As SqlCommand = New SqlCommand("SELECT ClientName
FROM tblClients", sqlConn)

sqlConn.Open()

objDataAdapter.SelectCommand = cmd
objDataAdapter.Fill(ds, "Clients")

'Set up the Combobox bindings
cmbClient.DataSource = ds
cmbClient.SelectedIndex = 0
cmbClient.DisplayMember = "Clients.ClientName"

Catch ex As Exception
'Error Handler
lblError.Visible = True
lblError.Text = Err.Description()
Finally
'Close the connection.
sqlConn.Close()
End Try
End Sub

Private Sub cmbClient_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cmbClient.SelectedIndexChanged
Call PopulateEntity(cmbEntity.SelectedValue)
End Sub

Any suggestions??
 
R

RobinS

You're saying that as you populate your combobox, it fires the
SelectedIndexChanged event, right?

I would put a boolean in there outside the PopulateClient sub, called
something like "_Loading".

In PopulateClient, set it to True at the beginning, and False at the end.

In the SelectedIndexChanged routine, check the boolean, and if _Loading is
true, don't do anything.

You can also remove the event handler for the SelectedIndexChanged event
before you start to load the data, then put it back after loading, but I
think it's kind of a pain.

Robin S.
 
S

Stoitcho Goutsev \(100\)

My suggestion is to wire the event after you set the data source.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top