How to set selected value on a databound dropdownlist from a datatable

rezanix

New Member
Ok, so i have a dropdownlist that is originally populated from the database. Now based on a datatable, i want the selected value to be equal to the text residing in the database. Whatever i do, it only shows "---Select One---" which is the only item i added manually to the dropdownlist items list to show a default value if the value i'm pulling is null (or that's what i want to do)\[code\] protected void Page_Load(object sender, EventArgs e) { Master.TopLabel = "Survey Creation"; if (!IsPostBack) { SqlConnection Connection = DatabaseConnection.GetSurveySystemConnection(); string sqlquery = "SELECT S.[Survey_Desc], S.[Start_Date], C.[Category_Name] ,S.[End_Date], S.[Audience] FROM [Survey] S Inner Join Category C On S.Category_ID = C.ID Where S.[ID] =" + Session["Survey_ID"]; SqlCommand cmd = new SqlCommand(sqlquery, Connection); cmd.CommandType = CommandType.Text; SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; DataTable DT = new DataTable(); da.Fill(DT); if (DT != null) { DescriptionMemo.Text = DT.Rows[0]["Survey_Desc"].ToString(); CategoryDropDownList.SelectedIndex = CategoryDropDownList.Items.IndexOf(CategoryDropDownList.Items.FindByText(DT.Rows[0]["Category_Name"].ToString())); StartDateCalender.SelectedDate = DateTime.Parse(DT.Rows[0]["Start_Date"].ToString()); EndDateCalender.SelectedDate = DateTime.Parse(DT.Rows[0]["End_Date"].ToString()); string Audience = DT.Rows[0]["Audience"].ToString(); if (Audience == "Students Only") { AudienceRadioGroup.Items[0].Selected = true; } else if (Audience == "Staff Only") { AudienceRadioGroup.Items[1].Selected = true; } else { AudienceRadioGroup.Items[2].Selected = true; } } Connection.Close(); } }\[/code\]DropDownList in aspx page.\[code\]<asp:DropDownList ID="CategoryDropDownList" runat="server" DataSourceID="SqlDataSource1" DataTextField="Category_Name" DataValueField="Category_Name" AppendDataBoundItems="true" Height="16px" Width="200px"> <asp:ListItem>---Select One---</asp:ListItem> </asp:DropDownList>\[/code\]The sql datasource select command.\[code\]SELECT [Category_Name] FROM [Category]\[/code\]EDIT : This is full code, but i didnt know if it was relevant, sorry.
 
Back
Top