创建了一个下拉列表。我的数据库表包含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();
}
}
下拉列表中未显示任何值。为什么?我的代码中有任何错误吗?
答案 0 :(得分:1)
我没有在Code上发现任何问题..但在Query上发现了一些问题..
确认您的子查询只返回一个值...
如果它返回多个值,请在查询中将DeptName =(
更改为DeptName in(
我也注意到了这里的空间 - &gt; ' " + Session["new"].ToString() + " '
将该空格移至'" + Session["new"].ToString() + "'
更正后的查询:
select FacultyName from facultyregtable1 where DeptName in(select department from
Studentregtable where UserName='" + Session["new"].ToString() + "')"
可能会帮助你...
答案 1 :(得分:0)
在aspx页面中尝试这个..............
<asp:DropDownList ID="DropDownList1" runat="server" datatextfield="FacultyName" datavaluefield="FacultyName" Height="20px" Width="147px">
</asp:DropDownList>
答案 2 :(得分:0)
select FacultyName from facultyregtable1 where DeptName=(select department from Studentregtable where UserName=' " + Session["new"].ToString() + " '
在上面的查询中,我认为你缺少deptName ='//它应该是你的子查询并返回一个值'附近的引号'