简体   繁体   English

如何使用数据库中的数据填充下拉列表

[英]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" 我已删除选择值选项,因为它抛出错误SelectedValue,该值无效,因为它在项目列表中不存在。\\ r \\ n参数名称: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 ,然后将其添加到下拉列表中。

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

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

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