我想在用户输入收据号码并进行搜索时显示收据号码的详细信息。之后用户应该可以编辑详细信息。我为驾驶员提供信息;但是,当我单击以编辑数据库中的驱动程序列表时,未显示;但只是实际数据。
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;
}
}
答案 0 :(得分:0)
您的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,然后手动添加一个{{1} },然后将其ListItem
设置为-1(使其显示在顶部):
Value