Reputation: 73
I'm trying to implement a drop down that will have numbers 1 - value in database.
I can get as far as displaying the value from the database in the dropdown, but can't get the numbers from 1 onwards to be created as options in the dropdown.
Below is my asp code:
<asp:SqlDataSource
ID="selectSprintLength"
runat="server"
ConnectionString="<%$ ConnectionStrings:scConnection %>"
SelectCommand="SELECT * FROM sc_sprints WHERE scSprintID = @sprint">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="sprint" Name="sprint" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DropDownList ID="sprintLengthDropDown"
runat="server"
OnSelectedIndexChanged="sprintDropDown_SelectedIndexChanged"
DataSourceID="selectSprintLength"
DataTextField="scSprintTotal"
DataValueField="scSprintTotal"
AutoPostBack="true"
EnableViewState="true" />
And this is my c# code:
protected void sprintDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
int counter = 0;
int x = Int32.Parse(sprintLengthDropDown.SelectedValue);
do
{
counter++;
}
while (counter < x);
}
Can anyone please help so that values 1 - scSprintTotal are all options in the dropdown. For instance scSprintTotal is 7, the drop down values would be 1, 2, 3, 4, 5, 6, 7.
Thanks in advance!
Upvotes: 0
Views: 55
Reputation: 1
int items;
protected void Page_Load(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("SELECT * from scSprint",con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
Label1.Text = "The total is " + dr["scsprinttotal"].ToString();
items = Convert.ToInt32(dr["scsprinttotal"].ToString());
}
con.Close();
for (int i = 1; i <= items;i++ )
{
DropDownList1.Items.Add(i.ToString());
}
}
Upvotes: 0
Reputation: 2180
To fill the dropdown with series value we can use Enumerable.Range(starting_number, range); Please see the tutorial https://www.dotnetperls.com/enumerable-range
sprintLengthDropDown.DataSource = Enumerable.Range(1, 7);
sprintLengthDropDown.DataBind();
You can make the range dynamic as the value from database.
Upvotes: 1