ayazhiha
2014-12-08 14:00
采纳率: 50%
浏览 2.1k

vs2010C#Web开发3个Dropdown控件从sql数据库中的4个表显示数据

实现效果:第一个下拉控件是选择学院,第二个下拉控件是选择系部,第三个下拉控件是选择班级。
从网上找了个后台代码:
前台:

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="StudInfo.aspx.cs" Inherits="Modules_StudInfo" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
    请选择学院:<asp:DropDownList ID="drdlXY" runat="server">

        </asp:DropDownList><br />
    请选择系部:<asp:DropDownList ID="drdlXB" runat="server">
        </asp:DropDownList><br />
    请选择班级:<asp:DropDownList ID="drdlCL" runat="server">
        </asp:DropDownList><br />
        <asp:Repeater ID="Repeater1" runat="server">
        </asp:Repeater>
    </div>
    </form>
</body>
</html>

后台代码:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Modules_StudInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string ConnString = ConfigurationSettings.AppSettings["SqlConnString"];
            SqlConnection Conn = new SqlConnection(ConnString);
            try
            {
                Conn.Open();
                this.drdlXY.Items.Add("请选择!");
                this.drdlXB.Items.Add("请选择!");
                this.drdlCL.Items.Add("请选择!");
                string strSQL = "select XueYuanName from XueYuan select XiBuName from XiBu select ClassName from Class";
                SqlCommand com = new SqlCommand(strSQL, Conn);
                SqlDataReader dr = com.ExecuteReader();
                if(dr.Read())
                {
                    this.drdlXY.Items.Add(dr["XueYuanName"].ToString());
                    this.drdlXB.Items.Add(dr["XiBuName"].ToString());
                    this.drdlCL.Items.Add(dr["ClassName"].ToString());
                }

            }
            //catch (Exception ex)
            //{
            //    Response.Write("<scirpt>alert('" + ex.Message.ToString() + "')</script>");

            //}
            finally
            {
                Conn.Close();
            }
        }

    }

}

但是这个出不来,如果把while中的后两个删掉,sql语句中只输入select XuYuanName from XuYuan 就不会提示错误,但是只有第一个下拉控件能实现功能。
显示错误

3条回答 默认 最新

相关推荐 更多相似问题