?
Solved

combo not populating in vb.net

Posted on 2016-08-01
12
Medium Priority
?
50 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 2000 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
Technology Partners: 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 34

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 34

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

Independent Software Vendors: 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!

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

718 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