【问题标题】:Select Query not filling my DataSet ASP.NET C#选择查询不填充我的数据集 ASP.NET C#
【发布时间】:2014-05-13 15:19:02
【问题描述】:

当我直接执行这个 SELECT 查询时,它可以工作。但是,在调试时,我看到我的数据集是空的。可能是什么问题?

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");
                }
            }
        }

我正在关注this 教程。

【问题讨论】:

  • 你有什么异常吗?
  • ItemName & ItemCode 应该是 Item_Name & Item_Code 吧?
  • @RajeevKumar 没有例外。填充数据集后它只是空的。
  • @markpsmith 是的。但是在它到达那里之前它是空的。
  • @sna2stha con.str 正在从我的连接对象中提取连接字符串。这是完美的填充。

标签: c# asp.net sql gridview


【解决方案1】:

也许这会对你有所帮助

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;
            }
        }
    }
}

【讨论】:

  • 非常感谢!在我的下拉 itemName 的 SelectedIndexChanged 上,我希望它在同一 gridview 行中填充 itemCode 文本框。我该怎么办?
  • 你的解决方案出现在那个链接dotnetsolutionsbyvenkat.blogspot.in/2013/11/…希望这对你有帮助
【解决方案2】:

您的值和文本字段:

ddl.DataTextField = "ItemName";
ddl.DataValueField = "ItemCode";

应该匹配表选择语句中的字段名:

"Select Item_Code,Item_Name from...

【讨论】:

    猜你喜欢
    • 2013-12-07
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多