Click here to Skip to main content
15,902,893 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am setting up a page to display results from a league based competition, and the system stores results for numerous seasons.

I have a table (tblSeason) that stores SeasonID, SeasonName and a bit field called Current - only one record has the bit set - it is the default season for the results - this allows me to have historic data, the current season, and future seasons. I want to populate a drop down list, with the current season as the default selection.

Populating the dropdown is easy, but I can't work out how to pre-select the current season.

Data in the season table is like this

SeasonID, Season, Current

The SQL string I use is:
SELECT [SeasonID], [Season], [Current] FROM [tblSeason] ORDER BY [Season]

The markup for the dropdown list is:

<asp:DropDownList ID="ddlSeason" runat="server" DataSourceID="sqlSeason"
DataTextField="Season" DataValueField="SeasonID"
AutoPostBack="True" onselectedindexchanged="ddlLeague_SelectedIndexChanged">

As the value in the data that I am looking for is not in the text or value propert of the dropdown, I can't use the FindByValue or FindByText methods. How can I do this?

Try following code:
ddlSeason.SelectedIndex = ddlSeason.Items.IndexOf(ddlSeason.Items.FindByValue("4"));

Hope this will help you.

Share this answer
Kenneth Haugland 1-Oct-12 3:51am    
From OP:
Sorry - that is not what I asked four - that find the item with key value 4 - I am trying to find the item where the value of the "Current" column is true.
I created a procedure to retrieve the seaon that has the Current marker set, then used this value with the FindByText method of the dropdown:

string strCurrent = getCurrentSeason();
          ddlSeason.SelectedIndex = ddlSeason.Items.IndexOf(ddlSeason.Items.FindByText(strCurrent));

protected string getCurrentSeason()

           SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
           string queryString = "SELECT [Season] FROM tblSeason WHERE [Current] = 1";
           SqlCommand command = new SqlCommand(queryString, conn);
           string strReturn = "";

           SqlDataReader reader = null;
           reader = command.ExecuteReader();

           if (reader.Read())
              strReturn = reader.GetString(0);
           return strReturn;

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900