DropDownList SelectedValue 无效,因为它不存在于项目列表中



这是我的代码

ASPX 代码

<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True" 
                            DataSourceID="SqlDataSource2" DataTextField="Type" DataValueField="days" 
                            onselectedindexchanged="DropDownList3_SelectedIndexChanged" 
                            Visible="False">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:prevaluesConnectionString %>" 
                            SelectCommand="select * from [plans USA] order by CAST([Type] as decimal)">
            </asp:SqlDataSource>

C#

protected void Button5_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["techconn"].ToString());
            SqlCommand com1 = new SqlCommand("select * from tech where accid0v =" + Session["update"], con);
            SqlCommand com2 = new SqlCommand("select * from techs where accid0v =" + Session["update"], con);
            SqlCommand com3 = new SqlCommand("select * from techh where accid0v =" + Session["update"], con);
            con.Open();
            SqlDataReader reader1 = com1.ExecuteReader();
            SqlDataReader reader2 = com2.ExecuteReader();
            SqlDataReader reader3 = com3.ExecuteReader();            
            if (reader1.Read())//Personal details
            {
                //Label30.Text = reader1["accid0v"].ToString();
                DropDownList1.Text = reader1["count0v"].ToString();
                TextBox12.Text = reader1["count0v"].ToString();
                DropDownList2.Text = reader1["piq0v"].ToString();
                TextBox1.Text = reader1["billin0v"].ToString();
                TextBox2.Text = reader1["fcustn0v"].ToString();
                TextBox3.Text = reader1["lcustn0v"].ToString();
                TextBox4.Text = reader1["contph0v"].ToString();
                TextBox5.Text = reader1["email0v"].ToString();
                TextBox6.Text = reader1["altph0v"].ToString();
                TextBox7.Text = reader1["pass0v"].ToString();               
            }
            if (reader2.Read())//Subscription Details
            {
                DropDownList3.Text = reader2["subst0v"].ToString();
                TextBox8.Text = reader2["actd0v"].ToString();
                TextBox9.Text = reader2["expir0v"].ToString();
                TextBox10.Text = reader2["pric0v"].ToString();
                TextBox11.Text = reader2["subst0v"].ToString();
                DateTime today = new DateTime();                    
                today = System.DateTime.Now;
                DateTime exp = new DateTime();
                exp = Convert.ToDateTime(TextBox9.Text);
                TimeSpan ts = exp.Subtract(today).Duration();

                TextBox20.Text = ts.Days.ToString();
                if(ts.TotalDays<=0)
                {
                    Label43.Text = "Expired";
                }
                else
                {
                    Label43.Text = "Active";
                }
            }
            if (reader3.Read())//Hardware Details
            {
                DropDownList5.Text = reader3["tof0v"].ToString();
                DropDownList6.Text = reader3["brand0v"].ToString();
                DropDownList7.Text = reader3["os0v"].ToString();
                TextBox21.Text = reader3["mac0v"].ToString();
            }
            con.Close();
        }
        {//Country check
            if ( TextBox12.Text == "us")
            {
                Label14.Text = "$";
                DropDownList3.Visible = true;
                DropDownList4.Visible = false;

                //plan name check start
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["preconn"].ToString());
                SqlCommand com1 = new SqlCommand("select * from [plans USA] where Type ='" + TextBox10.Text + "'", con);
                con.Open();
                SqlDataReader reader1 = com1.ExecuteReader();
                if (reader1.Read())//plans check
                {
                    Label20.Text = reader1["pname"].ToString();
                }
                con.Close();//plan name check end
            }
            if (TextBox12.Text == "ca")
            {
                Label14.Text = "$";
                DropDownList3.Visible = true;
                DropDownList4.Visible = false;
            }
            else if (TextBox12.Text == "uk")
            {
                Label14.Text = "£";
                DropDownList4.Visible = true;
                DropDownList3.Visible = false;

                //plan name check start
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["preconn"].ToString());
                SqlCommand com1 = new SqlCommand("select * from [PLANS UK] where Type ='" + TextBox10.Text + "'", con);
                con.Open();
                SqlDataReader reader1 = com1.ExecuteReader();
                if (reader1.Read())//plans check
                {
                    Label20.Text = reader1["pname"].ToString();
                }
                con.Close();//plan name check end

            }
            else
            {
            }
        }//country check end
    }

我收到错误

"DropDownList3"有一个无效的SelectedValue,因为它不存在于项目列表中。参数名称:值

我正在使用Visual Studio 2010 ASP.NET,SQL Server 2008

您在编码时将 DropDownList3 的选定项设置为 reader2 的内容:

DropDownList3.Text = reader2["subst0v"].ToString()

但是,您在 DropDownList3 中设置了此数据,该数据为空或没有您使用 reader2["subst0v"].ToString() 设置的项目

尝试检查 DropDownList3.在此行上放置断点的项目,看看我是否正确。

如果下拉列表为空,请检查正在向控件填充数据的 SQL。如果它不为空,只需检查是否有值等于 reader2["subst0v"].ToString()DropDownList3.Items .

最新更新