简体   繁体   中英

How to populate dropdown list with data from database

I have a stored procedure that pulls data from a database and displays it in a text box; it displays for the textboxes but doesn't display for the dropdown list or textbox that has textmode of date.

I've removed the select value option as it was throwing error SelectedValue which is invalid because it does not exist in the list of items.\\r\\nParameter name: value"

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("MM/dd/yyyy");
    txtReturnDte.Text = DATE;
    txtReceipt.Text = dt.Rows[0]["receiptNo"].ToString(); //Where ColumnName is the Field from the DB that you want to display
    //ddlCustomer0.Text= dt.Rows[0]["custName"].ToString();
    ddlDriver.Text = dt.Rows[0]["driverName"].ToString();
    ddlUnitId.Text = dt.Rows[0]["unitId"].ToString();
    txtNumber.Text = dt.Rows[0]["qtyReturned"].ToString();
    txtLocation.Text = dt.Rows[0]["custLocation"].ToString();
    Panel1.Visible = true;
}
else
{
    lblmsg.Text = "No Records Found";
    Panel1.Visible = false;
    btnEdit.Visible = false;
}

I want to be able to show the data from the database in a drop down list as well as the date in textbox

Create a ListItem first, then add it to the dropdown.

ListItem li = new ListItem(dt.Rows[0]["custName"].ToString());
ddlCustomer0.Items.Add(li);

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