【问题标题】:How to populate a dropdown list with SQL values in ASP.net Web application如何在 ASP.net Web 应用程序中使用 SQL 值填充下拉列表
【发布时间】:2022-01-12 17:38:04
【问题描述】:

我是 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>

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

【问题讨论】:

  • “它不工作是什么意思??”
  • It's Not Working "但它不起作用" - 什么不起作用?对数据库的调用?引发异常?值已添加到 ddl,但未填充?
  • "它不工作" = "我没有费心让它工作" = "你在 Stack Overflow 上得不到帮助"。

标签: c# sql asp.net c#-5.0 dropdownlistview


【解决方案1】:

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

与桌面(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 />

上面还让我从某个表中选择*,因为哪一列应该填充下拉列表中的哪个设置? (所以,这就是上面的 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();
                }
            

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多