程序如下:
string strsql;
protected void Button1_Click1(object sender, EventArgs e)
    {
        
        if (DropDownList1.SelectedItem.Value == "few")
        {
            strsql = "select * from warehouse where status='紧缺'";
        }
        else if (DropDownList1.SelectedItem.Value == "too_much")
        {
            strsql = "select * from warehouse where status='积压'";
        }
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["DSN_PM"]);
        SqlCommand cmd = new SqlCommand(strsql, conn);
        
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();//这句出现异常,报错说CommandText未初始化
        if (!dr.Read())
            Label1.Visible = true;
        dr.Close();
        dgrd2.DataSource = cmd.ExecuteReader();
        dgrd2.DataBind();
        conn.Close();
      
    }

解决方案 »

  1.   

    很正常啊!        if (DropDownList1.SelectedItem.Value == "few")
            {
                strsql = "select * from warehouse where status='紧缺'";
            }
            else if (DropDownList1.SelectedItem.Value == "too_much")
            {
                strsql = "select * from warehouse where status='积压'";
            }说明了你的DropDownList1.SelectedItem.Value不是few或者too_much之一
      

  2.   

    不可能啊,这个控件具体如下:
    <asp:DropDownList ID="DropDownList1" runat="server" Width="91px" ForeColor="Olive">
                    <asp:ListItem Value="&quot;few&quot;">缺货产品</asp:ListItem>
                    <asp:ListItem Value="&quot;too_much&quot;">积压产品</asp:ListItem>
                </asp:DropDownList>
    感觉它只能是"few"或"too_much"了
      

  3.   

    噢,我明白了,是不是&quot;的原因?它代表什么意思?
      

  4.   

    string strsql="";
    if (DropDownList1.SelectedItem.Value == "few")
            {
                strsql = "select * from warehouse where status='紧缺'";
            }
            else if (DropDownList1.SelectedItem.Value == "too_much")
            {
                strsql = "select * from warehouse where status='积压'";
            }
    if(strsql!="")
    {
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["DSN_PM"]);
            SqlCommand cmd = new SqlCommand(strsql, conn);
            
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();//这句出现异常,报错说CommandText未初始化
            if (!dr.Read())
                Label1.Visible = true;
            dr.Close();
            dgrd2.DataSource = cmd.ExecuteReader();
            dgrd2.DataBind();
            conn.Close();
    }
      

  5.   

    两个条件都不满足,sql就不会初始化if ..else if ...else..你的sql明明只是一个局部变量用途,建议做成局部变量掌握调试吧同学..