查询为空,大家帮忙看看怎么回事?
protected void Button5_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = 3;
string totalQuery = "";//存放所有的查询条件
string showCols = ""; //存放要显示的所有字段
string showbiao = "";
//获取列表框中的所有查询条件
if (ListBox4.Items.Count == 0)
{
Response.Write("<script language='javascript'> alert('请设定查询条件!')</script>");
}
for (int i = 0; i < ListBox4.Items.Count; i++)
{
totalQuery += ListBox4.Items[i].Text + "";
}
//获取复选框列表的所有被选择的字段
if (ListBox6.SelectedIndex > -1)
{
for (int j = 0; j < ListBox6.Items.Count; j++)
{
showCols += ListBox6.Items[j].Text + ",";
}
}
else
{
Response.Write("<script language='javascript'> alert('请选择要显示的字段名称!')</script>");
}
showCols = " " + showCols.TrimEnd(',') + " ";
if (ListBox5.SelectedIndex > -1)
{
for (int j = 0; j < ListBox5.Items.Count; j++)
{
showbiao += ListBox5.Items[j].Text + ",";
}
}
else
{
Response.Write("<script language='javascript'> alert('请选择要显示的表的名称!')</script>");
}
showbiao = " " + showbiao.TrimEnd(',') + " ";
SqlConnection myconn = new SqlConnection("server=localhost;user id=sa;pwd=;DataBase=cbmsXP_db");
string sql = " select " + showCols + " from " + showbiao + " where " + totalQuery + " ";
SqlDataAdapter adapt = new SqlDataAdapter(sql, myconn);
myconn.Open();
DataSet ds = new DataSet();
adapt.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
Label1.Text = "查询结果:(" + totalQuery + ")";
myconn.Close();
}
ListBox4 ListBox5 ListBox6 都是有数据显示的 就是读取那一块有问题, showbiao showCols totalQuery那为空,我改了几次,还是空值,不知道到底怎么回事?