选择查询不填充我的数据集ASP.NET C#
当我直接执行这个SELECT查询时,它就工作了。但是,在调试时,我看到我的数据集是空的。问题可能是什么选择查询不填充我的数据集ASP.NET C#,c#,asp.net,sql,gridview,C#,Asp.net,Sql,Gridview,当我直接执行这个SELECT查询时,它就工作了。但是,在调试时,我看到我的数据集是空的。问题可能是什么 protected void Grid_ItemList_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { Connection con = new Co
protected void Grid_ItemList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Connection con = new Connection();
con.con = new SqlConnection(con.str);
try
{
con.con.Open();
con.cmd = new SqlCommand("Select Item_Code,Item_Name from Pharmacy_Item_M", con.con);
var ddl = (DropDownList)e.Row.FindControl("ddlnames");
SqlDataAdapter da = new SqlDataAdapter(con.cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.con.Close();
ddl.DataSource = ds;
ddl.DataTextField = "ItemName";
ddl.DataValueField = "ItemCode";
ddl.DataBind();
}
catch (Exception ex)
{
log.Warn("Unable to open connection");
}
}
}
我正在学习教程。这可能会对您有所帮助
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the DropDownList in the Row
DropDownList ddlnames = (e.Row.FindControl("ddlnames") as DropDownList);
ddlRole.DataSource = GetData("Select Item_Code,Item_Name from Pharmacy_Item_M");
ddlRole.DataTextField = "Item_Name";
ddlRole.DataValueField = "Item_Code";
ddlRole.DataBind();
//Add Default Item in the DropDownList
ddlnames.Items.Insert(0, new ListItem("Please select"));
//Select the role of user in DropDownList
string x = (e.Row.FindControl("lblnames") as Label).Text;
ddlnames.Items.FindByValue(x).Selected = true;
}
}
和在GetData函数中
private DataSet GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["xxxx"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter da = new SqlDataAdapter())
{
cmd.Connection = con;
da.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
da.Fill(ds);
return ds;
}
}
}
}
也许这对你有帮助
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the DropDownList in the Row
DropDownList ddlnames = (e.Row.FindControl("ddlnames") as DropDownList);
ddlRole.DataSource = GetData("Select Item_Code,Item_Name from Pharmacy_Item_M");
ddlRole.DataTextField = "Item_Name";
ddlRole.DataValueField = "Item_Code";
ddlRole.DataBind();
//Add Default Item in the DropDownList
ddlnames.Items.Insert(0, new ListItem("Please select"));
//Select the role of user in DropDownList
string x = (e.Row.FindControl("lblnames") as Label).Text;
ddlnames.Items.FindByValue(x).Selected = true;
}
}
和在GetData函数中
private DataSet GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["xxxx"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter da = new SqlDataAdapter())
{
cmd.Connection = con;
da.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
da.Fill(ds);
return ds;
}
}
}
}
您的值和文本字段:
ddl.DataTextField = "ItemName";
ddl.DataValueField = "ItemCode";
应与表select语句中的字段名匹配:
"Select Item_Code,Item_Name from...
您的值和文本字段:
ddl.DataTextField = "ItemName";
ddl.DataValueField = "ItemCode";
应与表select语句中的字段名匹配:
"Select Item_Code,Item_Name from...
你有任何异常吗?ItemName和ItemCode应该是Item_Name和Item_Code,也许吧?@RajeevKumar没有异常。它在填充数据集后为空。@markpsmith True。但是在到达之前它是空的。@sna2stha con.str正在从我的连接对象中提取连接字符串。这是完美的填充。您有任何异常吗?ItemName和ItemCode应该是Item_Name和Item_Code,也许吧?@RajeevKumar没有异常。它在填充数据集后为空。@markpsmith True。但是在到达之前它是空的。@sna2stha con.str正在从我的连接对象中提取连接字符串。非常好,非常感谢!在我的下拉列表itemName的SelectedIndexChanged上,我希望它填充同一gridview行中的itemCode文本框。我该怎么做呢?你在链接中给出的解决方案希望这会对你有很大帮助!在我的下拉列表itemName的SelectedIndexChanged上,我希望它填充同一gridview行中的itemCode文本框。我该怎么做呢?您在该链接中提供的解决方案希望对您有所帮助