Solved

updating a sql server table from dynamically created combobox

Posted on 2004-03-23
6
323 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
  • 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
java constructor error 8 88
Specific format 21 144
How Complex Is This Java Course ? 9 63
recursion example 16 70
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
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 …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

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

21 Experts available now in Live!

Get 1:1 Help Now