?
Solved

How to populate 10 ComboBoxes from one DataSet that gets data from the same table?

Posted on 2006-05-10
7
Medium Priority
?
289 Views
Last Modified: 2010-08-05
Hi everyone:

I have 10 ComboBoxes that I have to populate from one DataSet. DataSet gets data from one table. My environment ASP.NET and SQL2K.

RGDS
Sam
0
Comment
Question by:BOOBIC
7 Comments
 
LVL 7

Expert Comment

by:Clever_Bob
ID: 16655236
I have a few ideas, just so we're on the same page. I presume you mean that you have a recordset and you want to disperse data from that recordset accross 10 different comboboxes. I'm also presuming that you'd know how to do this with just one dropdown box?

Should all of the combo boxes have the same data?

If so...

1) Open the recordset
2) create a loop through your recordset that drops each row into your <options> for each dropdown
3) go back to the start of the recordset e.g.  recordset.movefirst
4) repeat for each of your 10 combo boxes

Or you could just put the code for your combo boxes in the loop too if you wanted to be more efficient - and assign a variable for their names.
0
 

Author Comment

by:BOOBIC
ID: 16655302
I need to that code in ASP.NET.
0
 
LVL 3

Accepted Solution

by:
yokkui earned 400 total points
ID: 16655790
This is one way of doing it. If you want it in C#. Use http://carlosag.net/Tools/CodeTranslator/Default.aspx to translate it.

Imports System.Data.SqlClient
.
.
.
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
        Dim listItems As New ArrayList
        Dim conn As New SqlConnection("Your Connection String")
        Dim comm As New SqlCommand("Select sid, name from student", conn)
        Try
            conn.Open()
            Dim reader As SqlDataReader = comm.ExecuteReader(CommandBehavior.CloseConnection)
            While reader.Read
                Dim item As New ListItem(reader.GetInt32(0).ToString, reader.GetString(1))
                listItems.Add(item)
            End While
            For Each control As Control In Controls
                If control.GetType Is GetType(HtmlForm) Then
                    For Each childControl As Control In control.Controls
                        If childControl.GetType Is GetType(DropDownList) Then
                            Dim combobox As DropDownList = DirectCast(childControl, DropDownList)
                            For Each item As ListItem In listItems
                                combobox.Items.Add(item)
                            Next
                        End If
                    Next
                End If
            Next
        Finally
            conn.Close()
        End Try
End Sub
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Expert Comment

by:Prambo
ID: 16656588
It would be a lot easier to just use simple data binding. I assume you know how to populate the DataSet from the database.
Put this in your OnLoad function:

if(!Page.IsPostBack)
{
 foreach(DropDownList ddl in My10Comboboxes)
 {
  ddl.DataMember = "tableName" // Name of the DataTable object you want to use in the DataSet.
  ddl.DataValueField = "fldId";      // Id from table.
  ddl.DataTextField = "fldName";  // Name from table
  ddl.DataSource = dataSet;        // Point to data.
  ddl.DataBind();                         // Populate control.
 }
}
0
 

Author Comment

by:BOOBIC
ID: 16672729
I have to mention that Stored procedure have to have 10 distinct values from 10 colomns from the same table and than I have to asgin those 10 distinct values to 10 ComboBoxes. The whole idea is to make one connection to DB and populate 10 ComboBoxes.

It works fine for 1 distinct value and 1 ComboBox. But what about 10?

Thank you guys for your support.
Sam
0
 
LVL 1

Assisted Solution

by:Prambo
Prambo earned 400 total points
ID: 16674163
Assuming you have the name of the columns in an array called fldColumns you can just modify my example a bit:

if(!Page.IsPostBack)
{
 for(int i = 0; i < 10; i++)
 {
  DropDownList ddl = My10Comboboxes[i];
  ddl.DataMember = "tableName" // Name of the DataTable object you want to use in the DataSet.
  ddl.DataValueField = fldColumns[i]; // I assume you want the value and text to be the same.
  ddl.DataTextField = fldColumns[i];  // Name from table
  ddl.DataSource = dataSet;        // Point to data.
  ddl.DataBind();                         // Populate control.
 }
}
0
 

Author Comment

by:BOOBIC
ID: 16731003
This is how I have done that.

CREATE PROC NAME_A
AS
SELECT DISTINCT Period1 from Name_Period
order by Period1

CREATE PROC NAME_B
AS
SELECT DISTINCT Period2 from Name_Period
order by Period2

CREATE PROC CollectNames
AS
EXEC NAME_A
EXEC NAME_B
...................
..................


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

       Dim Connection As New SqlConnection("server=(local);database=AAA;Trusted_Connection=yes")
        Connection.Open()

        Dim command As SqlCommand = Connection.CreateCommand()
        command.CommandText = "CollectNames"
        command.CommandType = CommandType.StoredProcedure
   
        Dim Rdr As SqlDataReader = command.ExecuteReader()

        With DropDownList1
            .DataSource = Rdr
            .DataValueField = "Period1"
            .DataTextField = "Period1"
            .DataBind()
        End With

        Rdr.NextResult()

        With DropDownList2
            .DataSource = Rdr
            .DataValueField = "Period2"
            .DataTextField = "Period2"
            .DataBind()
        End With

................
................

    End Sub

I do appreciate your help, guys. Points was split and refunded.

RGDS
Sam
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Starting up a Project

839 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