Solved

updating a sql server table from dynamically created combobox

Posted on 2004-03-23
6
333 Views
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.i;
cboValue[i];
cboValue(i);
cboValue & cstr(i);

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


Jay
0
Comment
Question by:j-d
[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
  • 3
  • 2
6 Comments
 
LVL 1

Expert Comment

by:FaithNoMore
ID: 10663415
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.
0
 
LVL 27

Expert Comment

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

Dabas
0
 

Author Comment

by:j-d
ID: 10669138
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.Show()
                .Controls.Add(.cboValue)
                .cboValue.DataSource = attributevalues.Copy
                .cboValue.DisplayMember = "AttributeValue"
                .cboValue.ValueMember = "AttributeValueID"
                .cboValue.SelectedValue = -1
            End With
Next

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.i;
cboValue[i];
cboValue(i);
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.
0
Industry Leaders: 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:j-d
ID: 10669550
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)
                cb.Show()
            End With
            Me.Controls.Add(cb)

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

            ' abc1
            ' abc2
            ' abc3
            ' abc4
            ' abc5

        Next
        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
0
 
LVL 27

Accepted Solution

by:
Dabas earned 125 total points
ID: 10670401
j-d:
I suggest using a HashTable to store the comboboxes

        Dim ht As New Hashtable

        Dim i As Integer
        For i = 1 To 5
            Dim cb As New ComboBox
            With cb
                ht.Add(i, cb)
                cb.Size = New Size(352, 20)
                cb.Name = "abc" & CStr(i)
                cb.Location = New System.Drawing.Point(50, 40 + i * 40)
                cb.Show()
            End With
            Me.Controls.Add(cb)
        Next
        Dim cb5 As ComboBox = CType(ht.Item(5), ComboBox)
        Debug.WriteLine(cb5.Name)

Dabas
0
 

Author Comment

by:j-d
ID: 10689722
Thanks dabas for your excellent answer...

jd
0

Featured Post

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!

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

763 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