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

コードの問題は見つかりませんでしたが、クエリの問題が見つかりました..

サブクエリが 1 つの値のみを返すことを確認します...

複数の値を返す場合 は、クエリでに変更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 に答える