Combo box value change

  • Thread starter Thread starter Tonya
  • Start date Start date
T

Tonya

Hi,

I have a combobox that contains values from a database.
What i want to do is write code that will execute an sql
statement each time the value of the combobox is changed.
I want to do this so i can enter the values returned from
these queries into another 2 combo boxes i have.
Can anyone give me any guidence on this. I am a beginner
and have no idea of where to start.

Any help would be fab :o)
 
Hi Tonya,

On the event "index_changed" from the combobox (that is automaticly made as
you push on it in the designer) you take the value from the combobox.text

Something like this

dim SqlString as string ="SELECT * FROM mydatabase WHERE myselectitem = '"
& combobox.text & "'"

And with that you can read your data (by datareader or dataadaper or
whatever way you want)

There are a lot of other ways to make it more sophisticated but I would not
do that till you know how to do this.

Cor
 
thx for your help.

i just wanted to ask if it is poosible to use variables,
(as u did in your query ---MySelectedItem) within a
query? do i just declare it further up in the code?
Will the query recognise that it needs a value from the
user.

thx for your time
 
Hi
Tonya the selected _index_changed is fired when you fill the combobox.

(Put a switch, boolean, flag to prevent that that happens while loading it)
You can set that to false when the combobox is filled with the items. That
is the same with all the other comboboxes.

(There are more ways to do this, but this is the simplest)

And when the user pushes on the dropdownbutton and selects.
So when you set the query in that event, it only can be fired when the user
select something.

If you don't understand it, I will make a little example.

Cor
 
Hi Cor

Im sorry i am still confused on what u mean. I think that
example may be required :o)
Sorry to keep bugging u.

This is the downfall of trying to explain anything to a
complete beginner!!!
 
Angelina,

I made this example, it is partly copied and partly typed, so check the
errors yourself.

I hope this helps a little bit.

Cor

\\\
imports System.data.sqlclient.
private swcombo as boolean
Private Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load
dim i as integer
For i = 10 To 1000 Step 10
combobox1.Items.Add(i.ToString)
Next
combobox1.SelectedIndex = 1
swcombo = true
End Sub
Private Sub combobox1_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles combobox1.SelectedIndexChanged
if swcombo then
Dim connString as String = "a nice connection string"
Dim sqlString As String
sqlString = "SELECT * FROM mydatabase WHERE myselectfield = '" &
cmbSize.Text & "'"
Dim Conn As New SqlConnection(connString)
Dim cmd As New SqlCommand(sqlString, Conn)
Dim dataset1 As New DataSet
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
da.Fill(dataset1)
end if
End Sub
///
 
Hi Cor and Tonya
I recently stumbled across the
SelectionChangeCommitted event of a combo.
As far as I have tested it this far (which I admit is not much) this event
is not fired when you fill the combo but otherwise it works in the same way
as the SelectedIndexChanged.
This might free you from the switches.
Cor, if this is incorrect I would like to hear your experiences with
SelectionChangeCommitted

Best regards

Jan
 
Hi Jan,

I saw your answer in adonet, I thought I tryed it also once before, but I
do it today again.
That what I did write to Tonya was rough written not tested and copied from
2 applications

I am busy with a 2 comboboxen now, that are filled in that way, so I can try
it today.
And give you an answer.

Thanks,

Cor
 
Hi Jan,

I tried it and it worked fine, I thought I knew it somehow but forgot it.

Because I was quiet sure, I told somebody who had a problem with it already
about it.

I can now remember it, because I can think on you when I do this the next
time or give somebody advice.

Thanks

Cor
 
Back
Top