我现在做个按类别的查询,不过我我做的怎么老是出错,请高手赐教!
我在窗体上放一个dropdownlist控件,和一个DataGrid1
代码如下:private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
SqlConnection co=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["dbConnection"]);
SqlCommand cmd1=new SqlCommand("select * from v_tty_proInfo where lbName="+DropDownList1.SelectedItem.Text,co);
co.Open();
DataGrid1.DataSource=cmd1.ExecuteReader();
DataGrid1.DataBind();
co.Close();
}
错误!异常详细信息: System.Data.SqlClient.SqlException: 列名 '百商财富' 无效。源错误:
行 100: SqlDataAdapter da = new SqlDataAdapter(cmd1);
行 101: DataSet ds = new DataSet();
行 102: da.Fill(ds,"nu1");
行 103: DataGrid1.DataSource=ds.Tables["nu1"].DefaultView;
行 104: DataGrid1.DataBind();
我在窗体上放一个dropdownlist控件,和一个DataGrid1
代码如下:private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
SqlConnection co=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["dbConnection"]);
SqlCommand cmd1=new SqlCommand("select * from v_tty_proInfo where lbName="+DropDownList1.SelectedItem.Text,co);
co.Open();
DataGrid1.DataSource=cmd1.ExecuteReader();
DataGrid1.DataBind();
co.Close();
}
错误!异常详细信息: System.Data.SqlClient.SqlException: 列名 '百商财富' 无效。源错误:
行 100: SqlDataAdapter da = new SqlDataAdapter(cmd1);
行 101: DataSet ds = new DataSet();
行 102: da.Fill(ds,"nu1");
行 103: DataGrid1.DataSource=ds.Tables["nu1"].DefaultView;
行 104: DataGrid1.DataBind();
解决方案 »
- winform怎么取得浏览器chrome网页中,鼠标当前指向的链接
- c# webbroswer textarea成功导入后却无法正确提交
- 100元人民币+100分请求您的答复,决不食言!!!!!!
- C#应用程序,打开浏览对话框,怎么在文本框里记录选择图片的文件名称,而不是整个路径.在线
- <<一头雾水>>委托事件以及回调函数
- DotNet软件开发技术群,热情欢迎CSDN朋友加入
- 数据项使用DateTime类型有什么好处?为何不用字符串类型呢?
- C#控制while循环周期。。。各路大大们进来看看啊。。。
- byte[4] 如何赋值给 uint
- ASP.net 不能正确解析 Gobal.asax 是怎么回事?急!
- C# 调用API (DsBrowseForContainer)问题
- c# 技术揭秘 第二版有看过的吗.能给个评价吗
SqlCommand cmd1=new SqlCommand("select * from v_tty_proInfo where lbName='"+DropDownList1.SelectedItem.Text+"',co);
谢谢你的回复。
语句是没有错误了,不过每次选择dropdownlist时,其Text值都不边,都是默认的第一个数。
查询结果不会变的。请赐教!
不过
语句是没有错误了,不过每次选择dropdownlist时,其Text值都不边,都是默认的第一个数。
查询结果不会变的。请赐教!
SqlCommand cmd1=new SqlCommand("select * from v_tty_proInfo where lbName="+DropDownList1.SelectedItem.Text,co)
改为
SqlCommand cmd1=new SqlCommand("select * from v_tty_proInfo where lbName='"+DropDownList1.SelectedItem.Text + "'",co)
即可
查询结果不会变的。请赐教!
DropDownList1.SelectedItem.Text
改成
DropDownList1.Items[DropDownList1.SelectedIndex].Text
试一下
private void Page_Load(object sender, System.EventArgs e)
{
tty_leibie l=new tty_leibie();
l.LoadAll();
this.DropDownList1.DataSource=l.DefaultView;
this.DropDownList1.DataTextField="Lbname";
this.DropDownList1.DataValueField="LBId";
this.DropDownList1.DataBind();
SqlConnection co=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["dbConnection"]);
co.Open();
SqlCommand cmd1=new SqlCommand("select * from v_tty_proInfo order by Sdate Desc",co);
SqlDataAdapter da = new SqlDataAdapter(cmd1);
DataSet ds = new DataSet();
da.Fill(ds,"nu1");
DataGrid1.DataSource=ds.Tables["nu1"].DefaultView;
DataGrid1.DataBind();
co.Close();
}