Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive

To be honest, this is my first time seeing such error

The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive

This is where the error occurs. It occurs at the databind.

protected void FillDropdown(DropDownList ddl)
{
    using (var connAdd = new SqlConnection("Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI"))
    {
        connAdd.Open();

        var sql = "Select policeid from PoliceAccount where status ='available' and handle ='offcase' and postedto='" + ddllocation.SelectedValue + "'";
        using (var cmdAdd = new SqlDataAdapter(sql, connAdd))
        {
            DataSet ds2 = new DataSet();
            cmdAdd.Fill(ds2);

            ddl.Items.Clear();
            ddl.DataSource = ds2;
            //error occurs here
            ddl.DataBind();
            ddl.Items.Insert(0, new ListItem("Police ID", ""));
            ddl.SelectedIndex = 0;
        }
    }
}

I'm not very sure why would they say such a thing. I used this FillDropdown in my dropdownlist.

protected void ddlpid1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (ddlpid1.SelectedIndex > 0)
    {
        Session["pid1"] = ddlpid1.SelectedValue;

        ListItem removeItem2 = ddlpid2.Items.FindByValue(ddlpid1.SelectedValue);
        ddlpid2.Items.Remove(removeItem2);
        ListItem removeItem3 = ddlpid3.Items.FindByValue(ddlpid1.SelectedValue);
        ddlpid3.Items.Remove(removeItem3);
        ListItem removeItem4 = ddlpid4.Items.FindByValue(ddlpid1.SelectedValue);
        ddlpid4.Items.Remove(removeItem4);
        ListItem removeItem5 = ddlpid5.Items.FindByValue(ddlpid1.SelectedValue);
        ddlpid5.Items.Remove(removeItem5);
    }
    else if (ddlpid1.SelectedItem.Text.Equals("Police ID"))
    {
        FillDropdown(ddlpid1);
        FillDropdown(ddlpid2);
        FillDropdown(ddlpid3);
        FillDropdown(ddlpid4);
        FillDropdown(ddlpid5);

        ddlpid2.SelectedValue = (String) Session["pid2"];
        ddlpid2_SelectedIndexChanged(this, EventArgs.Empty);

        ddlpid3.SelectedValue = (String) Session["pid3"];
        ddlpid3_SelectedIndexChanged(this, EventArgs.Empty);

        ddlpid4.SelectedValue = (String) Session["pid4"];
        ddlpid4_SelectedIndexChanged(this, EventArgs.Empty);

        ddlpid5.SelectedValue = (String) Session["pid5"];
        ddlpid5_SelectedIndexChanged(this, EventArgs.Empty);
    }
}

I look at my codes multiple time and i see a clash in the selectedindex and selected value. All of them works individually as separate functions.

like image 963
Bryan Avatar asked Aug 06 '13 04:08

Bryan


4 Answers

Before databind set SelectedIndex to -1 it should help

like image 163
Piotr Stapp Avatar answered Oct 26 '22 01:10

Piotr Stapp


This can happen also if you have the selectedValue set before DataBind() call

like image 29
user4594658 Avatar answered Oct 26 '22 00:10

user4594658


There was already a similar question asked on the ASP.NET Forums - The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive.

The reason there was setting both the SelectedIndex and SelectedValue properties in the Page_Load event.

like image 43
t3chb0t Avatar answered Oct 26 '22 02:10

t3chb0t


you can add this before

ddlst.SelectedIndex = -1;
if (ddlst.SelectedValue.Length>0)
{
    ddlst.SelectedValue.Remove(0);
}
like image 36
Farhad Shekari Avatar answered Oct 26 '22 02:10

Farhad Shekari