创建一个下拉列表。我的数据库表包含2台使用下拉列表绑定值时出错
Studentregtable : ID int,FullName varchar,UserName varchar,department varchar.
facultyregtable1 : ID int,FacultyName varchar,DeptName varchar.
我的aspx代码:
<asp:DropDownList ID="DropDownList1" runat="server" Height="20px" Width="147px">
</asp:DropDownList>
我的C#代码:
public partial class studentfeedbackarea : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Database1ConnectionString1"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
con.Open();
SqlCommand cmd = new SqlCommand("select FacultyName from facultyregtable1 where DeptName=(select department from Studentregtable where UserName=' " + Session["new"].ToString() + " ')", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "FacultyName";
DropDownList1.DataValueField = "FacultyName";
DropDownList1.DataBind();
}
}
在drodown列表中显示的任何值。为什么?我的代码中有任何错误?
我不知道,*是*有错误吗?你是否已经浏览了代码并确认你确实从SQL查询中获取了任何数据? – tnw 2013-04-10 14:19:59
查询应该是'DeptName in(...)'而不是'DeptName =(...)' – Amitd 2013-04-10 14:20:48
您可以确认'从Studentregtable中选择部门,其中UserName ='“+ Session [”new“]。ToString()+ “''这个查询只返回一个值...? – Pandian 2013-04-10 14:23:12