简体   繁体   中英

gridview on page won't refresh, even when calling databind again

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

        }
    }
}

I would suggest putting your grid into an Update Panel. It looks like when you click the button, the page isn't refreshed in the postback thus the grid isn't...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM