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
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
ASKER
I need to that code in ASP.NET.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
}
}
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.
}
}
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
It works fine for 1 distinct value and 1 ComboBox. But what about 10?
Thank you guys for your support.
Sam
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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=(loc al);databa se=AAA;Tru sted_Conne ction=yes" )
Connection.Open()
Dim command As SqlCommand = Connection.CreateCommand()
command.CommandText = "CollectNames"
command.CommandType = CommandType.StoredProcedur e
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
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=(loc
Connection.Open()
Dim command As SqlCommand = Connection.CreateCommand()
command.CommandText = "CollectNames"
command.CommandType = CommandType.StoredProcedur
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
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.