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
Solved

updating a sql server table from dynamically created combobox

Posted on 2004-03-23
6
332 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

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

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
A short article about problems I had with the new location API and permissions in Marshmallow
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 …

860 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