[英]How to pull data from database and display in dropdownlist
我想在用戶輸入收據號碼並進行搜索時顯示收據號碼的詳細信息。 之后用戶應該可以編輯詳細信息。 我為駕駛員提供信息; 但是,當我單擊以編輯數據庫中的驅動程序列表時,未顯示; 但只是實際數據。
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;
}
}
BindData()
方法是一個不錯的開始,但是有點混亂。 而且我絕不是專家,但是我將整理一些您現在不需要的內容,我們將看看是否可以填充您的下拉菜單。
首先,如果尚不存在指令,則需要在頁面后面的代碼頂部添加一些指令:
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();
}
}
}
而且,如果您希望下拉菜單中的默認選項說的不是存儲過程中的首選項,則可以將一個名為AppendDataBoundItems
的屬性設置為true,然后將一個ListItem
手動添加到下拉菜單中,並將其Value
設置為-1(使其顯示在頂部):
<asp:DropDownList runat="server" ID="ddlDriver" AppendDataBoundItems="true">
<asp:ListItem Enabled="true" Text="Please Select" Value="-1"></asp:ListItem>
</asp:DropDownList>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.