Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 56
  • Last Modified:

combo not populating in vb.net

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
PeterBaileyUk
Asked:
PeterBaileyUk
  • 7
  • 3
  • 2
1 Solution
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
PeterBaileyUkAuthor Commented:
There is only ever one row. as its been filtered already by the parameter passing
0
 
PeterBaileyUkAuthor Commented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
PeterBaileyUkAuthor Commented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
PeterBaileyUkAuthor Commented:
ok i found the problem, i had assigned the wrong combo name

its working
0
 
PeterBaileyUkAuthor Commented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
>>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
 
PeterBaileyUkAuthor Commented:
it really is working fine.
0
 
it_saigeDeveloperCommented:
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
 
it_saigeDeveloperCommented:
Sorry, I was working on my response when you found your answer.

-saige-
0
 
PeterBaileyUkAuthor Commented:
apologies for that, I started reviewing the code and then spotted the mistake, sometimes you see what you want to see.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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.

  • 7
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now