再次调用数据绑定时,页面上的GridView不会刷新,甚至 [英] gridview on page won't refresh, even when calling databind again

查看:1395
本文介绍了再次调用数据绑定时,页面上的GridView不会刷新,甚至的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有我做过的研究似乎表明,如果我只是调用DataBind()再然后我的GridView控件将得到更新。这只似乎是,如果我调试,并通过我的code踏步的情况下,在GridView刷新罚款。但是,如果我不通过我的code步骤,同时运行在调试模式下的应用程序,该btnFileImport_Click下面的方法不刷新我的GridView控件。难道有什么关系的事实,我被加载使用SSIS包中的文件更新gridview的使用数据?下面是codebehind:

 命名空间InternationalWires
{
    公共部分类Default_Corporate:System.Web.UI.Page
    {
        康涅狄格州的SqlConnection =新的SqlConnection(ConfigurationManager.ConnectionStrings [InternationalWiresConnection]的ToString());
        CMD的SqlCommand = NULL;
        SQLSERVERAGENT SQLAGENT =新SQLSERVERAGENT();        保护无效的Page_Load(对象发件人,EventArgs的发送)
        {
            如果(!的IsPostBack)
            {
                BindRatesGrid();
            }
        }        公共无效BindRatesGrid()
        {
            康恩=新的SqlConnection(ConfigurationManager.ConnectionStrings [InternationalWiresConnection]的ToString());
            SqlDataAdapter的大=新SqlDataAdapter的(spGetRates,康恩);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataSet的DS =新的DataSet();
            da.Fill(DS);
            grdRates.DataSource = ds.Tables [0] .DefaultView;
            grdRates.DataBind();
        }        保护无效btnFileImport_Click(对象发件人,EventArgs的发送)
        {
            //获取用户名和路径。必须是UNC格式或SSIS将失败
            字符串文件名= Path.GetFullPath(fileSelect.PostedFile.FileName);            //更新设置表,从上面的值
            尝试
            {
                康恩=新的SqlConnection(ConfigurationManager.ConnectionStrings [InternationalWiresConnection]的ToString());
                CMD =新的SqlCommand(更新设置SET settingValue ='+文件名+'WHERE settingName ='SSISRatesImportFile',conn);在
                conn.Open();
                cmd.ExecuteNonQuery();
            }
            赶上(SQLEXCEPTION EX)
            {
                // TO DO:处理异常
            }
            最后
            {
                如果(参数conn!= NULL)conn.Dispose();
                如果(CMD!= NULL)cmd.Dispose();
            }            //设置SSIS包的名称来运行
            。sqlAgent.SSISName = ConfigurationManager.AppSettings [ratesImportPackage]的ToString();            //开始作业
            sqlAgent.SQL_SSISPackage();            //什么也不做,等待作业完成
            而(sqlAgent.SQL_IsJobRunning())
            {}            如果(sqlAgent.SQL_JobSucceeded())
            {
                lblStatus.Text =导入成功;
                BindRatesGrid();
            }
            其他
            {
                lblStatus.Text =导入失败,请与IT有关SSIS包导入失败的细节。
            }        }
    }
}


解决方案

我的脑袋在桌子上的多敲打之后,我终于通过这个答案迷迷糊糊的一种迂回的方式。

我SQL_IsJobRunning和SQL_JobSucceeded方法使用sp_help_job在SQ​​L弄清楚作业是否仍在运行,并且它是否成功。我工作的成功/错误信息在我的标签显示,当我得到了错误的那些我意识到,'current_execution_status'是显示作业已完成,但last_run_outcome'还没有被时间更新了我code在看的价值。所以,我把两种方法之间的停顿(Thread.sleep代码(4000))得到数据库有机会记录last_run_outcome之前我检查。

这解决了我的标签错误消息的问题,只好也解决我的问题的GridView愉快的副作用。有了暂停在GridView也成功地更新。一定有什么情况发生得太快了gridview的正常更新。我只希望我能知道。也许他的数据在数据库中提交之前正在运行BindRatesGrid()方法。

  //什么也不做,等待作业完成
        而(sqlAgent.SQL_IsJobRunning())
        {}        Thread.sleep代码(4000);        如果(sqlAgent.SQL_JobSucceeded())
        {
            lblStatus.Text =导入成功;
            BindRatesGrid();
        }
        其他
        {
            lblStatus.Text =导入失败,请与IT有关SSIS包导入失败的细节。
        }

All the research I've done seems to indicate that if I simply call DataBind() again then my gridview will get updated. This only seems to be the case if I'm debugging and stepping through my code, the gridview refreshes fine. However, if I don't step through my code while running the app in debug mode, the btnFileImport_Click method below doesn't refresh my gridview. Could it have anything to do with the fact that I'm updating the data the gridview uses by loading a file using an SSIS package? Below is the codebehind:

namespace InternationalWires
{
    public partial class Default_Corporate : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["InternationalWiresConnection"].ToString());
        SqlCommand cmd = null;
        SqlServerAgent sqlAgent = new SqlServerAgent();

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

        public void BindRatesGrid()
        {
            conn = new SqlConnection(ConfigurationManager.ConnectionStrings["InternationalWiresConnection"].ToString());
            SqlDataAdapter da = new SqlDataAdapter("spGetRates", conn);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataSet ds = new DataSet();
            da.Fill(ds);
            grdRates.DataSource = ds.Tables[0].DefaultView;
            grdRates.DataBind();
        }

        protected void btnFileImport_Click(object sender, EventArgs e)
        {
            // Get the filename and path from the user.  Must be in UNC format or SSIS will fail
            string filename = Path.GetFullPath(fileSelect.PostedFile.FileName);

            // Update the settings table to the value from above
            try
            {
                conn = new SqlConnection(ConfigurationManager.ConnectionStrings["InternationalWiresConnection"].ToString());
                cmd = new SqlCommand("UPDATE Settings SET settingValue = '" + filename + "' WHERE settingName = 'SSISRatesImportFile'", conn);
                conn.Open();
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                // TO DO: handle exceptions
            }
            finally 
            {
                if (conn != null) conn.Dispose();
                if (cmd != null) cmd.Dispose();
            }

            // set the name of the ssis package to run
            sqlAgent.SSISName = ConfigurationManager.AppSettings["ratesImportPackage"].ToString();

            // start the job
            sqlAgent.SQL_SSISPackage();

            // do nothing while waiting for job to finish
            while (sqlAgent.SQL_IsJobRunning())
            { }

            if (sqlAgent.SQL_JobSucceeded())
            { 
                lblStatus.Text = "Import Succeeded";
                BindRatesGrid();
            }
            else
            { 
                lblStatus.Text = "Import Failed.  Please contact IT for failure details on SSIS import package."; 
            }

        }
    }
}

解决方案

After much banging of my head on the desk, I finally stumbled across the answer in a roundabout way.

My SQL_IsJobRunning and SQL_JobSucceeded methods use sp_help_job in SQL to figure out whether or not the job is still running, and whether or not it succeeded. I was working on the success/error messages to display in my label and when I was getting the wrong ones I realized that the 'current_execution_status' was showing the job was done, but the 'last_run_outcome' had not yet been updated by the time my code was looking at the value. So I put a pause (Thread.Sleep(4000)) in between the two methods to give the database a chance to log the last_run_outcome before I checked it.

This solved my label error message issue, and had the pleasant side effect of also solving my gridview problem. With the pause in place the gridview also updates successfully. There must have been something happening too fast for the gridview to update properly. I just wish I knew what. Perhaps the BindRatesGrid() method was being run before he data was committed in the database.

        // do nothing while waiting for job to finish
        while (sqlAgent.SQL_IsJobRunning())
        { }

        Thread.Sleep(4000);

        if (sqlAgent.SQL_JobSucceeded())
        { 
            lblStatus.Text = "Import Succeeded";
            BindRatesGrid();
        }
        else
        { 
            lblStatus.Text = "Import Failed.  Please contact IT for failure details on SSIS import package."; 
        }

这篇关于再次调用数据绑定时,页面上的GridView不会刷新,甚至的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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