SelectedValue which is invalid because it does not exist in the list of items.

I have been trying to get around this error: SelectedValue which is invalid because it does not exist in the list of items. I tried changing when I populate the drop down boxes and it still doesn't work. The logic is, whenever dropdownlist4's selected index is changed, a gridview with the driver's information. I would like to populate the gridview info into the corresponding dropdownlist. Please see code below.
public partial class ChangeDriverStatus : System.Web.UI.Page
    {
        string DV_DRIVER_NUM;
        string DriverID;
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            WaltsSqlConnClass ConnClass = new WaltsSqlConnClass();
            DataSet ds1 = ConnClass.FillDropDown("SELECT BoardType FROM Board");

            DropDownList3.DataSource = ds1;
            DropDownList3.DataBind();

            if (IsPostBack)
            {
                testclass();
                int driverStatus = 0;
                int board = 0;
                int Location = 0;
                string truck = "%";
                int EmpStatus = 0;
                int SortBy = 1;
                string DV_DRIVER_NUM = "";
                string Date = DateTime.Now.Date.ToString();

                GridView12.DataSource = null;
                string DriverID = DropDownList4.SelectedValue;
                string cmdStringDVLookup = @"SELECT DVMAST.DV_DRIVER_NUM FROM DVMAST WHERE DV_DRIVER_NAME = '" + DropDownList4.SelectedValue + "'";
                WaltsSqlConnClass connClass = new WaltsSqlConnClass();
                DV_DRIVER_NUM = connClass.CreateMySqlDataReader(cmdStringDVLookup);
                HiddenField2.Value = DV_DRIVER_NUM;
                Label1.Text = DropDownList4.Text;

                WaltsSqlConnClass Conn1Class = new WaltsSqlConnClass();
                DataSet ds = Conn1Class.RunStoredProc(driverStatus, board, Location, truck, SortBy, EmpStatus, DV_DRIVER_NUM, Date);
                Session["myDataset"] = ds;
                GridView12.DataSource = ds;
                GridView12.DataBind();
                GridView12.Visible = true;
                string dv_status = "";
                string dv_location = "";
                string dv_board = "";
                string dv_emp = "";
                string dv_truck = "";
                if (GridView12.Rows.Count > 0)
                {
                    dv_emp = ds.Tables[0].Rows[0][1].ToString();
                    DropDownList6.SelectedValue = dv_emp;

                    dv_board = ds.Tables[0].Rows[0][4].ToString();
                    DropDownList3.SelectedValue = dv_board;
                    dv_status = ds.Tables[0].Rows[0][3].ToString();
                    DropDownList1.SelectedValue = dv_status;
                    dv_truck = ds.Tables[0].Rows[0][5].ToString();
                    DropDownList5.SelectedValue = dv_truck;
                    dv_location = ds.Tables[0].Rows[0][2].ToString();
                    DropDownList2.SelectedValue = dv_location;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            string strName = HttpContext.Current.User.Identity.Name.ToString();
            HiddenField1.Value = strName;

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //GridView12.DataSourceID = "SqlDataSource2";
            GridView12.DataBind();
            //Label1.Text = txtDate.Text;
            Label1.Text += " is selected";
        }

        protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
        {

            Label1.Text += " is selected";
            DropDownList1.Visible = true;
            DropDownList2.Visible = true;
            DropDownList3.Visible = true;
            DropDownList5.Visible = true;
            DropDownList6.Visible = true;
            Button7.Visible = true;
            Button5.Visible = true;
            Button4.Visible = true;
            Button3.Visible = true;

            Label3.Text = "Change Status";
            GridView12.Visible = true;
            Button4.Visible = true;
            Button8.Visible = true;
            Button7.Visible = true;
            txtDate.Visible = true;

            Label2.Text = "";

        }
        public void testclass()
        {
            int driverStatus = 0;
            int board = 0;
            int Location = 0;
            string truck = "%";
            int EmpStatus = 0;
            int SortBy = 1;
            string DV_DRIVER_NUM = "";
            string Date = DateTime.Now.Date.ToString();

            //GridView12.DataSource = null;
            //string DriverID = DropDownList4.SelectedValue;
            string cmdStringDVLookup = @"SELECT DVMAST.DV_DRIVER_NUM FROM DVMAST WHERE DV_DRIVER_NAME = '" + DropDownList4.SelectedValue + "'";
            WaltsSqlConnClass connClass = new WaltsSqlConnClass();
            DV_DRIVER_NUM = connClass.CreateMySqlDataReader(cmdStringDVLookup);
            HiddenField2.Value = DV_DRIVER_NUM;
            Label1.Text = DropDownList4.Text;

            WaltsSqlConnClass ConnClass = new WaltsSqlConnClass();
            DataSet ds = ConnClass.RunStoredProc(driverStatus, board, Location, truck, SortBy, EmpStatus, DV_DRIVER_NUM, Date);
            Session["myDataset"] = ds;
            GridView12.DataSource = ds;
            GridView12.DataBind();
            GridView12.Visible = true;
            string dv_status = "";
            string dv_location = "";
            string dv_board = "";
            string dv_emp = "";
            string dv_truck = "";

            Label1.Text += " is selected";
            DropDownList1.Visible = true;
            DropDownList2.Visible = true;
            DropDownList3.Visible = true;
            DropDownList5.Visible = true;
            DropDownList6.Visible = true;
            Button7.Visible = true;
            Button5.Visible = true;
            Button4.Visible = true;
            Button3.Visible = true;

            Label3.Text = "Change Status";
            GridView12.Visible = true;
            Button4.Visible = true;
            Button8.Visible = true;
            Button7.Visible = true;
            txtDate.Visible = true;

            Label2.Text = "";
        }
        protected void Button6_Click(object sender, EventArgs e)
        {
            DropDownList4.SelectedIndex = 0;
            GridView12.Visible = false;
            Label1.Text = "";
            DropDownList1.Visible = false;
            DropDownList2.Visible = false;
            DropDownList3.Visible = false;
            DropDownList6.Visible = false;
            Button5.Visible = false;
            Button3.Visible = false;
            Button4.Visible = false;
            Label3.Text = "";
            txtDate.Visible = false;
            Button8.Visible = false;
            Button3.Visible = false;
            DropDownList5.Visible = false;
            Button7.Visible = false;
            txtDate.Visible = false;

        }
        
        public void search()
        {
            int driverStatus = 0;
            int board = 0;
            int Location = 0;
            string truck = "%";
            int EmpStatus = 0;
            int SortBy = 1;
            string DV_DRIVER_NUM = "";
            string Date = DateTime.Now.Date.ToString();

            GridView12.DataSource = null;
            string DriverID = DropDownList4.SelectedValue;
            string cmdStringDVLookup = @"SELECT DVMAST.DV_DRIVER_NUM FROM DVMAST WHERE DV_DRIVER_NAME = '" + DropDownList4.SelectedValue + "'";
            WaltsSqlConnClass connClass = new WaltsSqlConnClass();
            DV_DRIVER_NUM = connClass.CreateMySqlDataReader(cmdStringDVLookup);
            HiddenField2.Value = DV_DRIVER_NUM;

            WaltsSqlConnClass ConnClass = new WaltsSqlConnClass();
            DataSet ds = ConnClass.RunStoredProc(driverStatus, board, Location, truck, SortBy, EmpStatus, DV_DRIVER_NUM, Date);
            Session["myDataset"] = ds;
            GridView12.DataSource = ds;
            GridView12.DataBind();
            GridView12.Visible = true;
        }

Open in new window

fishbowlstudiosAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rajkumar GsSoftware EngineerCommented:
Once I faced similar problem and fixed it.

When dropdownlist binds again, there may a previous selection in it. On the second rebind there may less records than previous selection.
For eg: First there may 10 records in dropdownlist. We select 8th one. Next bind may have only 5 records. In this scenario, the selection of 8th selection will become invalid and crash.

I remember, I fixed it like setting the dropdownlist's selectedindex to null just before binding the dropdownlist always.

Something like
Dropdownlist.selectedindex = null
Or try
dropdownlist.Selectedindex = 0 or -1

Hope this helps
Raj
0
fishbowlstudiosAuthor Commented:
Thanks Raj. However, it still did the same thing!
0
Rajkumar GsSoftware EngineerCommented:
Post here the line causing the crash. Double-click on the error message

Raj
0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

fishbowlstudiosAuthor Commented:

System.ArgumentOutOfRangeException was unhandled by user code
  Message="'DropDownList3' has a SelectedValue which is invalid because it does not exist in the list of items.\r\nParameter name: value"
  Source="System.Web"
  ParamName="value"
  StackTrace:
       at System.Web.UI.WebControls.ListControl.set_SelectedValue(String value)
       at WaltsWebBibleApp.ChangeDriverStatus.Page_LoadComplete(Object sender, EventArgs e) in C:\Users\erhoades\Documents\Visual Studio 2008\waltsweb\waltsweb\Bible\Administrator\ChangeDriverStatus.aspx.cs:line 81
       at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
       at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
       at System.EventHandler.Invoke(Object sender, EventArgs e)
       at System.Web.UI.Page.OnLoadComplete(EventArgs e)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException:

0
Rajkumar GsSoftware EngineerCommented:
I mean the line in the code you posted that causing crash. Not the error details

Raj
0
fishbowlstudiosAuthor Commented:
DropDownList3.SelectedValue = dv_board;
0
Rajkumar GsSoftware EngineerCommented:
Ok. I got the cause of this crash :)

The dropdownlist 'DropDownList3' is filled using this dataset
DataSet ds1 = ConnClass.FillDropDown("SELECT BoardType FROM Board");

>> DropDownList3.SelectedValue = dv_board;
And you are trying to select the value in the variable 'dv_board' to that dropdownlist.
But in that dropdownlist, the value of 'dv_board' is not present. In the case, it will crash.

You need to first verify whether the value is in that dropdownlist. If yes only, try to select it.

To check whether the data in 'dv_board' is present in dropdownlist 'DropDownList3' , use the code attached instead of
DropDownList3.SelectedValue = dv_board;

Hope this helps
Raj



DropDownList3.ClearSelection();
        if(DropDownList3.Items.FindByValue(dv_board)!=null)  
                DropDownList3.Items.FindByValue(dv_board).Selected = true;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fishbowlstudiosAuthor Commented:
Ok I got that far yesterday. However, I verified that the dv_board variable should be in the ddl but it is not. Does it have to do with the binding in the page states? Also, in the ddl, when I count the number of items, it says 13 which it should only count 4. Can you help me with these items? Thanks!
0
fishbowlstudiosAuthor Commented:
Ok I figured it out. Part of the problem was the fact that the drop downs were not bound to the datasource before the page rendered. So I went ahead and add a trusty dropdownlist.databind(); to the function and it mostly worked. I noticed that the dropdown3 was not working so I went rebinded the ddl to another sqldatasource and volia! It works!
0
fishbowlstudiosAuthor Commented:
See my solution below.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.