对DropDownList控件进行动态数据绑定时遇到的sql语句书写难题
部分代码:
String strSQL1 = "select TYPE_NAME from sm where TYPE_PCODE=0 ";
SqlCommand Comm1 = new SqlCommand(strSQL1,Conn);
SqlDataAdapter da = new SqlDataAdapter();
Conn.Open();
da.SelectCommand = Comm1;
DataSet ds = new DataSet();
da.Fill(ds, "grade1 ");
DropDownList1.DataSource = ds.Tables[ "grade1 "].DefaultView;
DropDownList1.DataTextField= "TYPE_NAME ";
DropDownList1.DataBind();
DropDownList1.SelectedIndex=0;
String strSQL2 = "select TYPE_NAME from sm where TYPE_POCDE=(select TYPE_CODE from sm where TYPE_NAME= "+DropDownList1.SelectedItem.Text+ ") ";
SqlCommand Comm2 = new SqlCommand(strSQL2,Conn);
da.SelectCommand = Comm2;
da.Fill(ds, "grade2 ");
DropDownList2.DataSource = ds.Tables[ "grade2 "].DefaultView;
DropDownList2.DataTextField= "TYPE_NAME ";
DropDownList2.DataBind();
其中DropDownList1和DropDownList2是两个DropDownList控件对象,数据库连接没有问题,肯定是这句sql语句错了:String strSQL2 = "select TYPE_NAME from sm where TYPE_POCDE=(select TYPE_PCODE from sm where TYPE_NAME= "+DropDownList1.SelectedItem.Text+ ") ";
请高手指教,谢谢!
数据表如下:
TYPE_CODE TYPE_PCODE TYPE_NAME TYPE_XH
1 0 日用品 1
2 0 食品 2
3 0 文具 3
21 2 饮料 1
31 3 铅笔 1
211 21 可口可乐 1
...
------解决方案--------------------String strSQL2 = "select TYPE_NAME from sm where TYPE_POCDE=(select TYPE_PCODE from sm where TYPE_NAME= "+DropDownList1.SelectedItem.Text+ ") ";
改成
String strSQL2 = "select TYPE_NAME from sm where TYPE_POCDE in (select TYPE_PCO