[英]How to populate dropdown list with data from database
我有一個存儲過程,該過程從數據庫中提取數據並將其顯示在文本框中; 它顯示在文本框中,但不顯示在下拉列表或具有日期文本模式的文本框中。
我已刪除選擇值選項,因為它拋出錯誤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;
}
我希望能夠在下拉列表中顯示數據庫中的數據以及文本框中的日期
首先創建一個ListItem
,然后將其添加到下拉列表中。
ListItem li = new ListItem(dt.Rows[0]["custName"].ToString());
ddlCustomer0.Items.Add(li);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.