简体   繁体   中英

How to pull data from database and display in dropdownlist

I want to display details of a receipt number when the user enters the number and do a search. After the user should be able to edit the details. I pull the information for the driver; however, when I click to edit the list of drivers from the database is not shown; but just the actual data.

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;

    }
}

Your BindData() method is a good start, but it's little cluttered. And I am by no means an expert, but I'm going trim out some of the stuff you don't need for now and we'll see if we can get your drop down populated.

First you'll need to add a couple using directives at the top of your code behind page if they're not there already:

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

This is how I was shown:

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();
        }
    }
}

And if you'd like the default option in your drop down to say something other than whatever comes first from your stored procedure you can set a property called AppendDataBoundItems to true, then manually add a ListItem to your drop down, and set its Value to -1 (to get it to show at the top):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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