繁体   English   中英

如何使用 ASP.net Web 应用程序中的 SQL 值填充下拉列表

[英]How to populate a dropdown list with SQL values in ASP.net Web application

我是 ASP.net 的新手,我正在尝试使用 Visual Studio 中本地 SQL 数据库中的值填充下拉列表。

这是我的代码,但它不起作用,有人可以帮忙吗?

           {
            SqlConnection PopulateListCon = new SqlConnection(ConnectionString);
                try
                {
                    if (PopulateListCon.State == ConnectionState.Closed)
                        PopulateListCon.Open();
                    String query = "SELECT * FROM ModuleTable WHERE UserId=@User AND ModuleSemester=@Sem";
                    SqlCommand sqlCmd = new SqlCommand(query, PopulateListCon);
                    sqlCmd.Parameters.Add("@User", SqlDbType.VarChar);
                    sqlCmd.Parameters["@User"].Value = userIdentification;
                    sqlCmd.Parameters.Add("@Sem", SqlDbType.VarChar);
                    sqlCmd.Parameters["@Sem"].Value = semester;

                    SqlDataReader dr1 = sqlCmd.ExecuteReader();
                    while (dr1.Read())
                    {
                        string modName = dr1.GetString(3);
                        Ddl_Module_Info_Time_Allocation_Module_Code.Items.Add(modName);
                    }
                }
                catch (Exception ex)
                {
                    errMsg = ex.Message;
                    Response.Write("<script>alert('Error: " + errMsg + "')</script>");
                }
                finally
                {
                    PopulateListCon.Close();
                }
        }

这是下拉列表的代码:

<asp:DropDownList ID="Ddl_Module_Info_Time_Allocation_Module_Code" runat="server" style="z-index: 3; left: 330px; top: 10px; position: absolute" Height="24px" Width="128px" Visible="False"></asp:DropDownList>

如果有人可以提供帮助,将不胜感激

你的代码很接近。 但您应该显示下拉列表的标记。

与桌面(FoxPro、MS-Access、vb.net、c#)一样,组合框(下拉列表)具有处理两列的能力。

 DataValueField = "column name" - this is the "ID" or often PK from table
 DataTextField = "column name"  - this is the text "display" column from table

所以,假设我需要下拉酒店名称。 并且只说来自给定城市的酒店。

所以标记是这样的:

   <div>
        <asp:DropDownList ID="DropDownList1" runat="server" Width="128px"
            DataValueField = "ID"
            DataTextField = "HoteName"
            >                
        </asp:DropDownList>
        <br />

以上也让我 go SELECT * 来自某个表,因为哪一列应该填写下拉列表中的哪个设置? (所以,这就是上面的 DataValue 和 DataText 设置所做的)。

现在,我们的代码将是这样的:

   protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadCombo();
    }

    void LoadCombo()
    {

        using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
        {
            string testcity = "Banff";    // this could be some user input - whatever
            string strSql = "SELECT ID, HotelName FROM tblHotels Where City = @City";

            using (SqlCommand cmdSQL = new SqlCommand(strSql, conn))
            {
                cmdSQL.Parameters.Add("@City", SqlDbType.NVarChar).Value = testcity;
                conn.open();
                DropDownList1.DataSource = cmdSQL.ExecuteReader();
                DropDownList1.DataBind();
            }
        }
    }

因此,您的代码可能/将是这样的:

            using (SqlConnection PopulateListCon = new SqlConnection(ConnectionString))
            {
                string strquery = "SELECT * FROM ModuleTable";
                using (SqlCommand sqlCmd = new SqlCommand(strquery, PopulateListCon))
                {
                PopulateListCon.Open();
                Ddl_Module_Info_Time_Allocation_Module_Code.DatSource = sqlCmd.ExecuteReader();
                Ddl_Module_Info_Time_Allocation_Module_Code.DataBind();
                }
            }

以上是没有参数的。

对于参数,说你这个:

           using (SqlConnection PopulateListCon = new SqlConnection(ConnectionString))
            {
              string query = "SELECT * FROM ModuleTable WHERE UserId=@User AND ModuleSemester=@Sem";

                using (SqlCommand sqlCmd = new SqlCommand(strquery, PopulateListCon))
                {
                    sqlCmd.Parameters.Add("@User", SqlDbType.Int).Value = User_ID;
                    sqlCmd.Parameters.Add("@Sem", SqlDbType.Int).Value = some sem expression here;

                    PopulateListCon.Open();
                    Ddl_Module_Info_Time_Allocation_Module_Code.DatSource = sqlCmd.ExecuteReader();
                    Ddl_Module_Info_Time_Allocation_Module_Code.DataBind();
                }
            }

那么我们的代码将是:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM