databinding from database to dropdownlist's problem

Last post 10-06-2006 8:09 AM by rekoms. 4 replies.

Sort Posts:

  • databinding from database to dropdownlist's problem

    10-03-2006, 7:50 AM
    • Member
      125 point Member
    • Jazkyching
    • Member since 09-21-2006, 11:41 AM
    • Posts 25

    let's say i have day's dropdownlist range from 1 to 31, then i also bind the dropdownlist from the database to get the day,

    and the problem that i face is duplicate value show up.

    let's say in database the day is 4. When bind it to dropdownlist, the 4 will be selected. but when click on the dropdownlist to select

    the day to make changes or update, the 1 is missing, and there is another 4 listed. That's mean there will have double 4 in the

    dropdownlist. Any idea to solve this error???

     

  • Re: databinding from database to dropdownlist's problem

    10-03-2006, 10:46 AM
    • Participant
      1,354 point Participant
    • rekoms
    • Member since 06-12-2006, 11:25 AM
    • Czech Republic
    • Posts 275

    Check my last post:

    http://forums.asp.net/thread/1416435.aspx 

    and post your code. 

  • Re: databinding from database to dropdownlist's problem

    10-03-2006, 2:38 PM
    • Member
      125 point Member
    • Jazkyching
    • Member since 09-21-2006, 11:41 AM
    • Posts 25

                string strSelect="Select * From Teacher Where TeacherID='" + strTeacherID + "'";
                SqlConnection conTeacher=new SqlConnection(strConnection);
                SqlCommand cmdSelect=new SqlCommand(strSelect,conTeacher);
                conTeacher.Open();
                SqlDataReader dtrRead=cmdSelect.ExecuteReader();
                if(dtrRead.HasRows)
                {
                    while(dtrRead.Read())
                    {
                        txtTeacherName.Text=dtrRead["TeacherName"].ToString();
                        txtTeacherIcno.Text=dtrRead["TeacherIcno"].ToString();
                        dropGender.SelectedItem.Text=dtrRead["TeacherGender"].ToString();
                        DateTime dtDob=Convert.ToDateTime(dtrRead["TeacherDob"]);
                        dropDobDay.SelectedItem.Text=dtDob.Day.ToString();
                        dropDobMonth.SelectedItem.Text=dtDob.Month.ToString();
                        dropDobYear.SelectedItem.Text=dtDob.Year.ToString();
                    }
                }
                dtrRead.Close();
                conTeacher.Close();
     

     <asp:DropDownList ID="dropGender" Runat="server">
     <asp:ListItem>M</asp:ListItem>
     <asp:ListItem>F</asp:ListItem>
     </asp:DropDownList>

     <asp:dropdownlist id="dropDobMonth" Runat="server">
                                <asp:ListItem runat="server">1</asp:ListItem>
                                <asp:ListItem runat="server">2</asp:ListItem>
                                <asp:ListItem runat="server">3</asp:ListItem>
                                <asp:ListItem runat="server">4</asp:ListItem>
                               ....
     </asp:dropdownlist>

     

    if the month in database is 4, the 4 will be shown as the 1st in the dropdownlist, and when you scroll it down, there is a 4 in the 4th row,

    and this is same as apply to the gender part, if the char's gender is F, there will be duplicate F shown up 

  • Re: databinding from database to dropdownlist's problem

    10-03-2006, 6:47 PM
    Answer
    • Participant
      1,354 point Participant
    • rekoms
    • Member since 06-12-2006, 11:25 AM
    • Czech Republic
    • Posts 275

    dropDobDay.SelectedItem.Text=dtDob.Day.ToString();

    This is the problem

    Try:

     dropDobDay.SelectedItem.Text.Equals(dtDob.Day.ToString()); //Guess this wont work, that was my first idea :-)

     OR

    dropDobDay.SelectedValue=dtDob.Day.ToString();


                String[] days = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" };
                String[] months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
                String[] years = { "2006", "2007", "2008" };
                ddlDays.DataSource = days;
                //Naplneni dnesnim datem
                ddlDays.SelectedValue = Convert.ToString(DateTime.Today.Day);
                ddlDaysB.DataSource = days;
                ddlDaysB.SelectedValue = Convert.ToString(DateTime.Today.AddDays(2).Day);
                ddlMonths.DataSource = months;
                ddlMonths.SelectedIndex = DateTime.Today.Month -1;
                ddlMonthsB.DataSource = months;
                ddlMonthsB.SelectedIndex = DateTime.Today.AddDays(2).Month -1;
                ddlYears.DataSource = years;
                ddlYears.SelectedValue = Convert.ToString(DateTime.Today.Year);
                ddlYearsB.DataSource = years;
                ddlYearsB.SelectedValue = Convert.ToString(DateTime.Today.AddDays(2).Year);
                DataBind();
     

  • Re: databinding from database to dropdownlist's problem

    10-06-2006, 8:09 AM
    • Participant
      1,354 point Participant
    • rekoms
    • Member since 06-12-2006, 11:25 AM
    • Czech Republic
    • Posts 275
    Did it work???
Page 1 of 1 (5 items)