Link to home
Start Free TrialLog in
Avatar of BOOBIC
BOOBIC

asked on

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

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
Avatar of Clever_Bob
Clever_Bob
Flag of Australia image

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.
Avatar of BOOBIC
BOOBIC

ASKER

I need to that code in ASP.NET.
ASKER CERTIFIED SOLUTION
Avatar of yokkui
yokkui
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
 }
}
Avatar of BOOBIC

ASKER

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BOOBIC

ASKER

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