首页 > 解决方案 > 如何从数据库中提取数据并显示在下拉列表中

问题描述

我想在用户输入号码并进行搜索时显示收据号码的详细信息。之后用户应该能够编辑详细信息。我为司机拉信息;但是,当我单击以编辑数据库中的驱动程序列表时,未显示;但只是实际数据。

private void BindData()

{
    int parsedValue;

    DataTable dt = new DataTable();
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "PP_spSearchReturnCrate";
    if (!string.IsNullOrEmpty(txtReceiptNo.Text.Trim()))
    {
        cmd.Parameters.Add("@receiptNo", SqlDbType.VarChar).Value = txtReceiptNo.Text.Trim();
    }
    cmd.Connection = sqlConn;
    sqlConn.Open();
    SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
    sqlDa.Fill(dt);



    if (dt.Rows.Count > 0)
    {



        String DATE = Convert.ToDateTime(dt.Rows[0]["returnDte"]).ToString("yyyy-MM-dd");
        txtReturnDte.Text = DATE;
        txtReceipt.Text = dt.Rows[0]["receiptNo"].ToString(); //Where ColumnName is the Field from the DB that you want to display
        ddlCustomer.Text = dt.Rows[0]["CUSTNAME"].ToString();

        //ddlDriver.Text = dt.Rows[0]["driverName"].ToString();
        //ListItem lis = new ListItem(dt.Rows[0]["driverName"].ToString());
        //ddlDriver.Items.Add(lis);
        ddlUnitId.Text = dt.Rows[0]["unitId"].ToString();
        txtNumber.Text = dt.Rows[0]["qtyReturned"].ToString();
        txtLocation.Text = dt.Rows[0]["custLocation"].ToString();
        //ddlDriver.DataSource = cmd.ExecuteReader();
        //ListItem lis = new ListItem(dt.Rows[0]["driverName"].ToString());
        //ddlCustomer.Items.Add(lis);
        ddlDriver.DataSource = dt;
        ddlDriver.DataBind();
        ddlDriver.DataTextField = "driverName";
        ddlDriver.DataValueField = "driverName";
        ddlDriver.DataBind();
        //ListItem li = new ListItem(dt.Rows[0]["driverName"].ToString());
        //ddlDriver.Items.Add(li);
        Panel1.Visible = true;

    }
}

标签: c#asp.net

解决方案


你的BindData()方法是一个好的开始,但它有点混乱。而且我绝不是专家,但我会删除一些你现在不需要的东西,我们会看看我们是否可以填充你的下拉菜单。

首先,如果它们不存在,您需要在代码后面的页面顶部添加几个 using 指令:

using System.Configuration;
using System.Data.SqlClient;

这就是我的显示方式:

private void BindData()
{
    // Wrap the whole thing in a using() because it automatically closes the connection
    // when it's done so you don't have to worry about doing that manually
    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["name of your connection string"].ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            // Set the releveant properties like you already had                
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "PP_spSearchReturnCrate";

            // Double check that the connection is open                    
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }

            // Create your SqlDataAdapter and fill it with the data from your stored procedure                     
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);

            // Then set that as the DataSource, and finally bind it to your drop down
            ddlDriver.DataSource = ds.Tables[0];
            ddlDriver.DataBind();
        }
    }
}

如果您希望下拉列表中的默认选项说出存储过程中首先出现的内容之外的内容,您可以将一个名为 true 的属性设置AppendDataBoundItems为 true,然后手动将 a 添加ListItem到您的下拉列表中,并将其设置Value为 -1(让它显示在顶部):

<asp:DropDownList runat="server" ID="ddlDriver" AppendDataBoundItems="true">
        <asp:ListItem Enabled="true" Text="Please Select" Value="-1"></asp:ListItem>
</asp:DropDownList>

推荐阅读