Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Items are multiplying - Cascade comboboxes VB.net + SQL

vbnet combobox sql items multiplying

Best Answer lespauled, 04 July 2013 - 12:54 PM

This wont help your issue, but do an inner join instead of the in clause.  It looks like your double quotes are out of sync

Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1 RiderOfTheli

RiderOfTheli

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 04 July 2013 - 12:10 PM

Hi all, 
 
I'm trying to figure out, why after clicking once again on my combobox, next combobox remember items from previous selection.
 
I have 7 comboboxes (items for cb's are storen in SQL table). For each I have Sub to fill it with the items, like this:
 

Sub fillAdditional()
Dim var As String = Convert.ToString(cbAccount.SelectedItem)
Dim var2 As String = Convert.ToString(cbTower.SelectedItem)
Dim var3 As String = Convert.ToString(cbProcess.SelectedItem)
strsql = "Select Distinct Additional from tblAdministratorData where AccIc in ( Select AID from tblAccount)"
'((TimerAccount like '%" + var + "%') AND (TimerTower like '%" + var2 + "%') and (TimerProcess like '%" + var3 + "%'))"
Dim acscmd As New SqlCommand
acscmd.CommandText = strsql
acscmd.Connection = cn
acsdr = acscmd.ExecuteReader
While (acsdr.Read())
Me.cbAdditional.Items.Add(acsdr("Additional"))
End While
acscmd.Dispose()
acsdr.Close()
End Sub

 
I'm calling each after Combobox_SelectedIndexChange like:
 

Me.fillAdditional()

 
I was trying to do it, by adding 

cbTower.SelectedIndex = cbTower.FindString(cbAccount.SelectedText)


to Combobox_SelectedIndexChange, but no value added...
 
Could someone tell me, what I'm doing wrong?
Thank you in advance!
Best Regards

Matt



#2 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 04 July 2013 - 12:54 PM   Best Answer

This wont help your issue, but do an inner join instead of the in clause.  It looks like your double quotes are out of sync


My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#3 zeroGiven

zeroGiven

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 61 posts

Posted 04 July 2013 - 12:55 PM

Putting the call to the sub in the selectedindexchanged event would be bad practice for this. What you are essentially doing is adding the result of your query to the combobox and then when the user selects an item, you're adding the results of your query to the combobox. However, there is already data there so it's duplicating. No you could easily clear your combobox right before your While loop starts but that would probably have issues with the selectedindex that the user selects when you clear to repopulate.

 

I would suggest populating the comboboxes based on the query either during the loading of the form or right after you know how you want to populate them and leave it alone until you have deemed it necessary to re-fill (clear and fill) based on newly added options for the user.



#4 RiderOfTheli

RiderOfTheli

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 05 July 2013 - 12:24 AM

Hi, thanks for your answer.

 

I'm trying to do that, but I don't know where to put 'clear', because I'm calling me.fill'Combo' in SelectedIndexChanged event on previous Combobox...I cannot upload all data on_load, because my filling subs are using previous selection as a variant so....

 

Ehh, I have no idea how to solve it...



#5 RiderOfTheli

RiderOfTheli

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 05 July 2013 - 05:54 AM

Okay, I've done this. I had an error in the table :)

 

Thank you for your help!






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download