c# - 如何从数据库中提取数据并显示在下拉列表中
问题描述
我想在用户输入号码并进行搜索时显示收据号码的详细信息。之后用户应该能够编辑详细信息。我为司机拉信息;但是,当我单击以编辑数据库中的驱动程序列表时,未显示;但只是实际数据。
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 指令:
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();
}
}
}
如果您希望下拉列表中的默认选项说出存储过程中首先出现的内容之外的内容,您可以将一个名为 true 的属性设置AppendDataBoundItems
为 true,然后手动将 a 添加ListItem
到您的下拉列表中,并将其设置Value
为 -1(让它显示在顶部):
<asp:DropDownList runat="server" ID="ddlDriver" AppendDataBoundItems="true">
<asp:ListItem Enabled="true" Text="Please Select" Value="-1"></asp:ListItem>
</asp:DropDownList>
推荐阅读
- javascript - 如果 window.hash.location 与 data-attr 匹配,则将类添加到 div
- javascript - 一页上的多个幻灯片效果不好
- excel-formula - 添加值直到达到另一个单元格值
- html - href="#" 是如何工作的?
- android - 在 9patch 背景上应用时,可绘制的波纹效果会变形
- laravel - Laravel Nova 在应用程序中使用 nova 的组件和工具(在 Nova 之外)
- php - 无法在 PHP 中将 mcrypt 升级为 openssl 解密
- php - Laravel - Blade @yield 在向用户提供 html 时忽略选项卡
- javascript - X 次访问后隐藏 div(cookie)
- sql - Vertica/SQL、Case ELSE、类型 numeric 和 boolean 不一致