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 code behind:
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_IsJobRunningandSQL_JobSucceededmethods usesp_help_jobin 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 thelast_run_outcomebefore I checked it.This solved my label error message issue, and had the pleasant side effect of also solving my
GridViewproblem. With the pause in place theGridViewalso updates successfully. There must have been something happening too fast for theGridViewto update properly. I just wish I knew what. Perhaps theBindRatesGrid()method was being run before he data was committed in the database.