程序如下:
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();
}
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();
}
{
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之一
<asp:DropDownList ID="DropDownList1" runat="server" Width="91px" ForeColor="Olive">
<asp:ListItem Value=""few"">缺货产品</asp:ListItem>
<asp:ListItem Value=""too_much"">积压产品</asp:ListItem>
</asp:DropDownList>
感觉它只能是"few"或"too_much"了
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();
}