我现在做个按类别的查询,不过我我做的怎么老是出错,请高手赐教!
我在窗体上放一个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();
 

解决方案 »

  1.   

    这样改,试一下
    SqlCommand cmd1=new SqlCommand("select * from v_tty_proInfo where lbName='"+DropDownList1.SelectedItem.Text+"',co);
      

  2.   

    楼上也写错了:P  应该是SqlCommand cmd1=new SqlCommand("select * from v_tty_proInfo where lbName='"+DropDownList1.SelectedItem.Text+"'",co);
      

  3.   

    SqlCommand cmd1=new SqlCommand("select * from v_tty_proInfo where lbName="+"'"+DropDownList1.SelectedItem.Text+"'",co);
      

  4.   

    回复人: zhangci226(三只熊熊) 
    谢谢你的回复。
    语句是没有错误了,不过每次选择dropdownlist时,其Text值都不边,都是默认的第一个数。
    查询结果不会变的。请赐教!
      

  5.   

    没想到这的这么热情,我在此就都一并谢谢了
    不过
    语句是没有错误了,不过每次选择dropdownlist时,其Text值都不边,都是默认的第一个数。
    查询结果不会变的。请赐教!
      

  6.   

    将 
    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)
    即可
      

  7.   

    按照上面的程序已经没错,不过每次选择dropdownlist时,其Text值都不边,都是默认的第一个数。
    查询结果不会变的。请赐教!
      

  8.   


    DropDownList1.SelectedItem.Text
    改成
    DropDownList1.Items[DropDownList1.SelectedIndex].Text
    试一下
      

  9.   

    还是不行,是不是由于我程序里的Page_Load中加了下面程序的关系,怎样修改。
    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();

    }
      

  10.   

    把你Page_Load里面的所有代码外面套上 if(!IsPostBack){...}