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

时间:2014-04-03 16:23:58

标签: c# asp.net sql-server-2008

这是我的代码

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

    }

我收到错误

&#39; DropDownList3&#39;具有一个无效的SelectedValue,因为它不存在于项列表中。参数名称:值

我正在使用visual studio 2010 asp.net,SQL Server 2008

1 个答案:

答案 0 :(得分:0)

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

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

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

尝试检查DropDownList3.Items在此行上设置断点并查看我是否正确。

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