2013-04-10 43 views
0

创建一个下拉列表。我的数据库表包含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列表中显示的任何值。为什么?我的代码中有任何错误?

+0

我不知道,*是*有错误吗?你是否已经浏览了代码并确认你确实从SQL查询中获取了任何数据? – tnw 2013-04-10 14:19:59

+0

查询应该是'DeptName in(...)'而不是'DeptName =(...)' – Amitd 2013-04-10 14:20:48

+1

您可以确认'从Studentregtable中选择部门,其中UserName ='“+ Session [”new“]。ToString()+ “''这个查询只返回一个值...? – Pandian 2013-04-10 14:23:12

回答

1

我没有找到代码中的任何问题..但找到关于查询的一些问题..

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

如果返回多个值那么在您的查询

而且我在这里注意到一个空间改变DeptName =(DeptName in( - >' " + Session["new"].ToString() + " '删除空间'" + Session["new"].ToString() + "'

Correcte d查询:

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

可能它会帮助你...

0

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

<asp:DropDownList ID="DropDownList1" runat="server" datatextfield="FacultyName" datavaluefield="FacultyName" Height="20px" Width="147px"> 
      </asp:DropDownList> 
0
select FacultyName from facultyregtable1 where DeptName=(select department from Studentregtable where UserName=' " + Session["new"].ToString() + " ' 

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