为什么数据集显示旧数据 [英] Why is dataset showing old data

查看:100
本文介绍了为什么数据集显示旧数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小网站,用户从Excel工作表上传名称,这些名称存储在表格中。然后我有一个存储过程,使用这些名称从其他表中提取数据,并将其输出到另一个Excel工作表。我还有一个显示上传名称的gridview。我的问题是,每次我上传新名称时,gridview要么显示以前上传的名字,要么是空白。当我在sql上运行存储过程时,正确的名称显示。我一直在搞砸我的大脑尝试弄清楚为什么gridview没有显示像sql上传的最新名字



我尝试过:



i have a small website where users upload names from excel sheet and those names get stored into a table. then i have a stored procedure that uses those names to extract data from other tables and output it into another excel sheet. i also have a gridview showing the names uploaded. my problem is that every time i upload new names the gridview either shows the previous names uploaded or is blank. when i run the stored procedure on sql the correct names show. i have been wracking my brain tryna figure out why the gridview doesnt show the latest names uploaded like on sql

What I have tried:

protected void Page_Load(object sender, EventArgs e)
    {
            SqlConnection con = new SqlConnection(strConnString);

            con.Open();

            SqlCommand command = new SqlCommand("BenReport", con) { CommandType = System.Data.CommandType.StoredProcedure };

            SqlDataAdapter sda = new SqlDataAdapter();


            command.Connection = con;
            sda.SelectCommand = command;

            DataSet ds = new DataSet();

            sda = new SqlDataAdapter("BenReport", con);

            sda.Fill(ds);

            GRDBencount.DataSource = ds.Tables[0];
            GRDBencount.DataBind();
            con.Close();
        }

推荐答案

不要以这种方式编写所有逻辑



1)为逻辑创建saparate函数

2)管理页面回发事件

3)从页面回发事件条件调用函数



Don't write all logic in this manner ever

1) Create saparate function for the logic
2) Manage page postback event
3) Call function from page postback event condition

protected void Page_Load(object sender, EventArgs e)
{
              if (!IsPostBack)
                {
                   BindData();
                }


}




public void BindData(){

            SqlConnection con = new SqlConnection(strConnString);
            con.Open();
            SqlCommand command = new SqlCommand("BenReport", con) { CommandType = System.Data.CommandType.StoredProcedure };

            SqlDataAdapter sda = new SqlDataAdapter();
            command.Connection = con;
            sda.SelectCommand = command;

            DataSet ds = new DataSet();
            sda = new SqlDataAdapter("BenReport", con);
            sda.Fill(ds);

            GRDBencount.DataSource = ds.Tables[0];
            GRDBencount.DataBind();
            con.Close();
}





了解更多信息



Page.IsPostBack属性(System.Web.UI) | Microsoft Docs [ ^ ]


感谢您的输入!

i修改了我的存储过程,现在我能够在上传名称的新工作表之前清理表格
Hi , thanks for the input!
i modified my stored procedures , now im able to clean the table before new sheet with names are uploaded


这篇关于为什么数据集显示旧数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆