Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have three dropdownlist for date,month,and Year and a button. I am getting the error. I am getting:Cannot have multiple items selected in a DropDownList
C#
 protected void Page_Load(object sender, EventArgs e)
        {
         for (int month = 1; month < 13; month++)
            {
            
                ddlMonth.Items.Add(month.ToString());

            }
            ddlMonth.Items.FindByValue(System.DateTime.Now.Month.ToString()).Selected = true;
            for (int year = 1947; year < 2015; year++)
            {

                ddlYear.Items.Add(year.ToString());

            }
           ddlYear.Items.FindByValue(System.DateTime.Now.Year.ToString()).Selected = true;
FillDays();
}
public void FillDays()
        {
            ddlDate.Items.Clear();
            //getting number of days in selected month & year
            int noofdays = DateTime.DaysInMonth(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt32(ddlMonth.SelectedValue));

            //Fill days
            for (int i = 1; i <= noofdays; i++)
            {
                ddlDate.Items.Add(i.ToString());
            }
            ddlDate.Items.FindByValue(System.DateTime.Now.Day.ToString()).Selected = true;// Set current date as selected
        }
 protected void save_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Insert_Reminder";
            cmd.CommandType = CommandType.StoredProcedure;
            string datestring = string.Format("{0}-{1}-{2}", ddlMonth.SelectedValue, ddlDate.SelectedValue.PadLeft(2, '0'), ddlYear.SelectedValue.PadLeft(2, '0'));
            string timestring = ddlHour.SelectedValue +":"+ddlMinute.SelectedValue +":"+ddlsecond.SelectedValue;
            cmd.Parameters.Add("@birthdate",SqlDbType.Date).Value=datestring;
            cmd.Parameters.Add("@reminder_time", SqlDbType.Time).Value = timestring.Trim();
            cmd.Parameters.Add("@reminder_detail", SqlDbType.Text).Value = txt_reminder.Text.Trim();
            cmd.Connection=con;
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
                Response.Write("<script>alert('Data Inserted Successfully')</script>");
            }
            catch(Exception ex)
            {
                   Response.Write(ex.Message);
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }
Posted
Updated 29-Apr-14 21:26pm
v4

1 solution

ASP.NET DropDownList is only for single selection. You may use ListBox which has property for selection mode and can select multiply values...
Start reading here: Multi select Dropdown list in ASP.NET[^]
 
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