dt = sql.SelectSql("select * from einfo where SchID ='" + dt.Rows[0]["SchID"].ToString() + "' and class=" + comboClass.SelectedValue.ToString() + " order by class,srid"); 是这句,不好意思.
dt.Rows[0]["SchID"].ToString() 改用变量
dt.Rows[0]["SchID"].ToString()从哪儿来的?代码贴全来看
class=" + comboClass.SelectedValue.ToString() + " order by class,srid" 少了单引号 改成 class='" + comboClass.SelectedValue.ToString() + "' order by class,srid"
private void button2_Click(object sender, EventArgs e) { dt = sqlc.SelectSql("select * from SchInfo where flag='1'"); if (comboClass.SelectedValue.ToString() == "0") { dt = sql.SelectSql("select * from einfo where SchID ='" + dt.Rows[0]["SchID"].ToString() + "' order by class,srid"); } else { dt = sql.SelectSql("select * from einfo where SchID ='" + dt.Rows[0]["SchID"].ToString() + "' and class=" + comboClass.SelectedValue.ToString() + " order by class,srid"); } lblStatus.Text = "搜索到的记录数:" + dt.Rows.Count + " 条"; r.SetDataSource(dt); r.Refresh(); ReportViewer.ReportSource = r; } 刚开始到 if (comboClass.SelectedValue.ToString() == "0")这里的时候就不行了 后来改成if (comboClass.SelectedValue!= null && comboClass.SelectedValue.ToString() == "0") 然后到这里就停住了
把sql语句打印出来,copy到查询分析器中运行,一眼看出错误 dt = sql.SelectSql("select * from einfo where SchID ='" + dt.Rows[0]["SchID"].ToString() + "' and class=" + comboClass.SelectedValue.ToString() + " order by class,srid");
是这句,不好意思.
改成
class='" + comboClass.SelectedValue.ToString() + "' order by class,srid"
{
dt = sqlc.SelectSql("select * from SchInfo where flag='1'");
if (comboClass.SelectedValue.ToString() == "0")
{
dt = sql.SelectSql("select * from einfo where SchID ='" + dt.Rows[0]["SchID"].ToString() + "' order by class,srid");
}
else
{
dt = sql.SelectSql("select * from einfo where SchID ='" + dt.Rows[0]["SchID"].ToString() + "' and class=" + comboClass.SelectedValue.ToString() + " order by class,srid");
}
lblStatus.Text = "搜索到的记录数:" + dt.Rows.Count + " 条"; r.SetDataSource(dt);
r.Refresh();
ReportViewer.ReportSource = r;
}
刚开始到 if (comboClass.SelectedValue.ToString() == "0")这里的时候就不行了
后来改成if (comboClass.SelectedValue!= null && comboClass.SelectedValue.ToString() == "0")
然后到这里就停住了
comboClass.SelectedValue.ToString()
这个出问题了,因为你sql语句里也有这个建议你在if之前把comboClass.SelectedValue.ToString()打印出来看看到底是个什么值
dt = sql.SelectSql("select * from einfo where SchID ='" + dt.Rows[0]["SchID"].ToString() + "' and class=" + comboClass.SelectedValue.ToString() + " order by class,srid");