Solved

combo not populating in vb.net

Posted on 2016-08-01
12
41 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…
A simple description of email encryption using a secure portal service. This is one of the choices offered by The Email Laundry for email encryption. The other choices are pdf encryption which creates an encrypted pdf of your email and any attachmen…

911 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

22 Experts available now in Live!

Get 1:1 Help Now