The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive
Asked Answered
E

6

7

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.

Emma answered 6/8, 2013 at 4:9 Comment(5)
i wrote it in the title but i will bring it down to the question sectionEmma
which line/part of the code is giving this error ?Junkojunkyard
I have added a commented line which shows the error. It's at the databind.Emma
From what I can tell it is due to the fact that calling FillDropdown(ddlpid2); will set the SelectedIndex value and after that you call ddlpid2.SelectedValue = (String) Session["pid2"]; which sets the SelectedValue, which you may not do in a single event? forums.asp.net/t/1189510.aspxDiapason
@astander - But he is getting error inside FillDropdown function databind line, before this line he is not setting selected index or selected value. Everything is set after this line.Montoya
L
6

Before databind set SelectedIndex to -1 it should help

Lander answered 6/8, 2013 at 4:20 Comment(0)
H
5

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

Handoff answered 15/4, 2015 at 19:19 Comment(1)
This is exactly what caused the error for me. The SelectedValue was being set in code, but I wasn't calling DataBind anywhere, so this was being handled at the usual point in the page lifecycle. The error only occurred during data binding, which had none of my code in the markup. Calling DataBind manually immediately before setting the SelectedValue avoided the error.Unceasing
Y
2

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.

Yuyuan answered 11/12, 2014 at 7:44 Comment(0)
H
1

you can add this before

ddlst.SelectedIndex = -1;
if (ddlst.SelectedValue.Length>0)
{
    ddlst.SelectedValue.Remove(0);
}
Histrionic answered 13/7, 2016 at 14:27 Comment(0)
S
0

Check whether dataset ds2has value or not. If it has some policeId as value,then add these two lines and bind it.

ddl.DataSource = ds2;
ddl.DataTextField = "policeid";
ddl.DataValueField = "policeid";
ddl.DataBind();
Shahjahanpur answered 6/8, 2013 at 6:34 Comment(0)
S
0

I know this is necroposting, but none of the tricks found on the net worked for me.
So, looking at the source code of DropDownList, I discovered how to really reset the selection state of the control before loading items again, to avoid checks on SelectedValue or SelectedItem:

ddl.Items.Clear()
ddl.SelectedItem = null; //null, not empty string
ddl.SelectedIndex = -1;

Doing this assigments, in this exact order, the internal selection status is resetted.

After that you can assign DataSource and call DataBind():

ddl.DataTextField = "TextField";
ddl.DataValueField = "ValueField";
ddl.DataSource = source;
ddl.DataBind();
Succinate answered 7/4, 2023 at 7:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.