Solved

combo not populating in vb.net

Posted on 2016-08-01
12
46 Views
Last Modified: 2016-08-02
I have a small sub that gets the data, everything is fine up until I get to the add items to the combo, the string variables are populated correctly, I appear to have mishandled the actual .add but cannot see why

I did look at the microsoft website I havnt posted the link
but it says: ' Visual Basic
ComboBox1.Items.Add("Tokyo")



    Private Sub GetCBChords()
        Dim StrProcName As String
        Dim StrWord As String
        Dim dtChord As DataTable
        Dim Stri As String
        Dim Strii As String
        Dim Striii As String
        Dim Striv As String
        Dim Strv As String
        Dim Strvi As String
        Dim Strvii As String
        Dim Strdim7 As String


        Dim connectionString As String = "Data Source=MAIN-PC\SQLEXPRESS;Initial Catalog=PushCircle;Integrated Security=True;MultipleActiveResultSets=True"
        dtChord = New DataTable



        StrProcName = "usp_GetChords"
        Using connChord As New SqlConnection(connectionString)
            Using cmdChord As New SqlCommand(StrProcName, connChord)
                cmdChord.CommandType = CommandType.StoredProcedure
                connChord.Open()
                cmdChord.Parameters.AddWithValue("@Tonic", CBNote.SelectedItem.ToString())
                cmdChord.Parameters.AddWithValue("@ScaleType", CBScaleType.SelectedItem.ToString())
                Dim reader As SqlDataReader = cmdChord.ExecuteReader()

                dtChord.Load(reader)


                For Each drRecord As DataRow In dtChord.Rows
                    Stri = drRecord("i")
                    Strii = drRecord("ii")
                    Striii = drRecord("iii")
                    Striv = drRecord("iv")
                    Strv = drRecord("v")
                    Strvi = drRecord("vi")
                    Strvii = drRecord("vii")
                    Strdim7 = drRecord("dim7")

                Next
                CBScaleType.Items.Add(Stri)
                CBScaleType.Items.Add(Strii)
                CBScaleType.Items.Add(Striii)
                CBScaleType.Items.Add(Striv)
                CBScaleType.Items.Add(Strv)
                CBScaleType.Items.Add(Strvi)
                CBScaleType.Items.Add(Strvii)
                CBScaleType.Items.Add(Strdim7)



            End Using
        End Using


    End Sub

Open in new window

0
Comment
Question by:PeterBaileyUk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
12 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 41737612
not sure what your real issue is!

but you have one problem for sure: you are looping through the rows, overwriting your variables for each new row. once you have gone through all the rows, the variables contain only the values of the last row. You finally add these variables (containing only the values of the last row) to the combo!

if you want the values of all the rows, you need to add the content of each row:
                For Each drRecord As DataRow In dtChord.Rows
                    Stri = drRecord("i")
                    Strii = drRecord("ii")
                    Striii = drRecord("iii")
                    Striv = drRecord("iv")
                    Strv = drRecord("v")
                    Strvi = drRecord("vi")
                    Strvii = drRecord("vii")
                    Strdim7 = drRecord("dim7")

                CBScaleType.Items.Add(Stri)
                CBScaleType.Items.Add(Strii)
                CBScaleType.Items.Add(Striii)
                CBScaleType.Items.Add(Striv)
                CBScaleType.Items.Add(Strv)
                CBScaleType.Items.Add(Strvi)
                CBScaleType.Items.Add(Strvii)
                CBScaleType.Items.Add(Strdim7)

                Next

Open in new window

0
 

Author Comment

by:PeterBaileyUk
ID: 41737614
There is only ever one row. as its been filtered already by the parameter passing
0
 

Author Comment

by:PeterBaileyUk
ID: 41737625
the only other code is but i dont think that is affecting it, if either of the combo boxes get changed the 3rd combo gets repopulated with the data provided from the other two.

 Private Sub CBNote_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CBNote.SelectedIndexChanged
        If ((CBNote.SelectedIndex = -1) Or (CBScaleType.SelectedIndex = -1)) Then

        Else
            Call GetCBChords()
        End If
    End Sub

    Private Sub CBScaleType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CBScaleType.SelectedIndexChanged
        If ((CBNote.SelectedIndex = -1) Or (CBScaleType.SelectedIndex = -1)) Then

        Else
            Call GetCBChords()
        End If
    End Sub

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:PeterBaileyUk
ID: 41737626
the row the one row is always passing these fields just with different note and scaletype:

C maj      D min      E min      F maj      G maj      A min      B dim -
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41737628
Sorry. I may be dumb but I don't understand your issue. Can you provide the full picture of what you trying to achieve?

And in your last comment, you are testing CBScaleType and your GetCBChords methods fills it. Is it really what you want? Looks circular to me!
0
 

Author Comment

by:PeterBaileyUk
ID: 41737641
ok i found the problem, i had assigned the wrong combo name

its working
0
 

Author Comment

by:PeterBaileyUk
ID: 41737642
on the issue of the combos I have a note selector combo and a scaletype selector and if either one of those changes it requeries.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41737643
>>i had assigned the wrong combo name

This is exactly what I told in : And in your last comment, you are testing CBScaleType and your GetCBChords methods fills it. Is it really what you want? Looks circular to me!
0
 

Author Comment

by:PeterBaileyUk
ID: 41737647
it really is working fine.
0
 
LVL 33

Expert Comment

by:it_saige
ID: 41737670
Do you get an error or exception or is the combobox just blank?  I am guessing that the combobox is just blank.

Let's try a different approach:
Imports System.Data.SqlClient

Public Class Form1
	Private Sub OnLoad(sender As Object, e As EventArgs) Handles MyBase.Load
		GetCBChords(1)
	End Sub

	Private Sub GetCBChords(id As Integer)
		Dim [table] As DataTable = New DataTable
		Using [connection] As New SqlConnection("Data Source=.;Initial Catalog=EE_Q28960774;Integrated Security=True;Pooling=False")
			Using [command] As New SqlCommand(String.Format("SELECT * FROM Chord WHERE ID = {0}", id), [connection])
				[connection].Open()
				Dim reader As SqlDataReader = [command].ExecuteReader()
				[table].Load(reader)
				For Each [row] As DataRow In [table].Rows
					For Each [column] As DataColumn In [table].Columns
						If Not [column].ColumnName.Equals("ID", StringComparison.OrdinalIgnoreCase) Then
							ComboBox1.Items.Add([row]([column].ColumnName))
						End If
					Next
				Next
			End Using
		End Using
	End Sub
End Class

Open in new window

Produces the following results for me -Capture.JPG
-saige-
0
 
LVL 33

Expert Comment

by:it_saige
ID: 41737675
Sorry, I was working on my response when you found your answer.

-saige-
0
 

Author Comment

by:PeterBaileyUk
ID: 41738557
apologies for that, I started reviewing the code and then spotted the mistake, sometimes you see what you want to see.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question