1

创建了一个下拉列表。我的数据库表包含 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();
        }
    }

下拉列表中未显示任何值。为什么?我的代码中有任何错误吗?

4

3 回答 3

1

我在 Code 上没有发现任何问题.. 但在 Query 上发现了一些问题..

确认您的子查询仅返回一个值...

如果它返回多个值,则 在您的查询中更改DeptName =(DeptName in(

我也注意到这里有一个空格 ->' " + Session["new"].ToString() + " '删除那个空格'" + Session["new"].ToString() + "'

更正的查询:

select FacultyName from facultyregtable1 where DeptName in(select department from
Studentregtable where UserName='" + Session["new"].ToString() + "')"

也许它会帮助你...

于 2013-04-10T14:35:09.127 回答
0

在 aspx 页面中试试这个............

<asp:DropDownList ID="DropDownList1" runat="server" datatextfield="FacultyName" datavaluefield="FacultyName" Height="20px" Width="147px">
            </asp:DropDownList>
于 2013-04-10T14:56:12.453 回答
0
select FacultyName from facultyregtable1 where DeptName=(select department from Studentregtable where UserName=' " + Session["new"].ToString() + " '

在上面的查询中,我认为您在 DeptName='//它应该是返回一个值的子查询附近缺少引号'

于 2013-04-10T15:18:15.307 回答