Solved

combo not populating in vb.net

Posted on 2016-08-01
12
37 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
  • 7
  • 3
  • 2
12 Comments
 
LVL 69

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
 

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 69

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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 69

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 32

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 32

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now