We help IT Professionals succeed at work.

updating a sql server table from dynamically created combobox

j-d asked
Medium Priority
Last Modified: 2012-05-04
I have a form with dynamically created comboboxes in which a user enters values. What I want to do is (when user clicks SAVE button) take these combobox values and insert into a SQL Server 2000 table in the backend. The combobox is defined as a class and the different instances are created dynamically on the form. My problem is how to refer to different combobox values in code?

For example, cboValue is the combobox which has 5 instances and hence 5 values given to it. cboValue.selectedvalue will give me a value that I can insert into a table. The problem is I can get only the last combobox value this way. I want something like this:

for i = 1 to 5
insert into table1 (field1) values cboValue.i
next                                        ^^^^^^^

cboValue.i is where the mess happens - how do I refer to the 5 different combo values  - I have tried these so far with no success -

cboValue & cstr(i);

This is a pretty general problem that could apply to any class and its instances. Thanks for any help...

Watch Question

I dont understand how you can have 5 instances of a class with the same name in your code...

'combobox is defined as a class' .. Is this a customer class, if so is it a collection type class? ie, a collection of comboboxs. If so, then you would need to post the code of it so we could see how it is defined.

I would assume that each instance of the combox class would be given a different name in your code.  If this is the case then you would have to refere to these names to get access to the .Value property.

Hi j-d,
First of all, you have to specify what your development environment is!
.NET? VB or C#?



OK here are the details:

I am using VB.NET

Here are the relevant sections of code:

Public Class frmAllocationDetailEdit
    Inherits System.Windows.Forms.Form
    Friend WithEvents cboName As New ComboBox
    Friend WithEvents cboValue As New ComboBox ' define cboValue as a Combobox class
' Create cboValue comboboxes below; instances of class, cboValue, are named as ValueComboBox1, ValueComboBox2, etc.
' Number, SomeInteger, of instances are dynamically generated based on numbers created elsewhere in program.

for i=1 to SomeInteger
            With frmChild
                .cboValue = New ComboBox
                .cboValue.Size = New Size(150, 20)
                .cboValue.Name = "ValueComboBox" & CStr(i) ' Naming of instances here
                .cboValue.Location = New Point(175, 40 + i * 25)
                .cboValue.DropDownStyle = ComboBoxStyle.DropDownList
                .cboValue.DataSource = attributevalues.Copy
                .cboValue.DisplayMember = "AttributeValue"
                .cboValue.ValueMember = "AttributeValueID"
                .cboValue.SelectedValue = -1
            End With

User fills these instances with their own selected values from a drop-down list.

Now I have a form with SomeInteger, say 5, comboboxes. I ned to save the user-selected values into a table. I have no idea how to refer to the instances of comboboxes.

I have tried:

cboValue & cstr(i);
and ValueComboBox1;
"ValueComboBox" & i

etc. etc. I have a shaky understanding of classes as I have just started learning this stuff. The essential question is: from a dynamically-generated set of combos as created above, and user-filled data, how do I move the data from comboboxes to a table (I am fine with SQL part, but not the VB.NET part).

Much thanks for any help you can provide me.


Here is a more simplified code with the above problem (I created a new form and added the following in its LOAD event) -my problem is that of accessing combos after they have been dynamically produced (please see my comments in the code):

Public Class Form3
    Inherits System.Windows.Forms.Form
    Friend WithEvents cb As New ComboBox

#Region " Windows Form Designer generated code "

    Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim i As Integer
        For i = 1 To 5
            With cb
                cb.Size = New Size(352, 20)
                cb.Name = "abc" & CStr(i)
                cb.Location = New System.Drawing.Point(50, 40 + i * 40)
            End With

            Debug.WriteLine(cb.Name) ' Results are given below

            ' abc1
            ' abc2
            ' abc3
            ' abc4
            ' abc5

        Debug.WriteLine(cb.Name) ' gives abc5 as result (once out of FOR-NEXT loop, only last combo is available - I need all)

        ' My big problem:
        ' How do I refer to individual comboboxes such as produced above
        ' I would think cb instances should be avaialable somehow as properties of cb
        ' Using either cb mutations or abc* to get individual combos doesn't work

        Debug.WriteLine(abc1.name) ' Says abc1 is not declared
        Debug.WriteLine(cb.Items(0)) ' Compile errors
        Debug.WriteLine(cb(1).name) ' Compile errors

    End Sub
End Class
Unlock this solution and get a sample of our free trial.
(No credit card required)


Thanks dabas for your excellent answer...

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.