2012-02-08 57 views
2

我所做的所有研究似乎都表明,如果我再次調用DataBind(),那麼我的GridView將會更新。這似乎只是如果我正在調試和逐步通過我的代碼,gridview刷新罰款。但是,如果我在調試模式下運行應用程序時沒有瀏覽代碼,則下面的btnFileImport_Click方法不會刷新我的gridview。這與我通過使用SSIS包加載文件來更新gridview使用的數據有什麼關係?下面是代碼隱藏:即使再次調用數據綁定,頁面上的gridview也不會刷新

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

     } 
    } 
} 
+0

我假設你'// TO DO:處理exceptions'就是爲什麼當我通過它從來沒有碰到異常的代碼步驟GridView控件沒有更新;-) – 2012-02-08 19:37:45

+0

的原因,所以我不知道爲什麼會這樣。 – BeachBum 2012-02-08 20:54:58

+0

在該部分添加一些代碼沒有任何區別。 – BeachBum 2012-02-08 21:05:23

回答

0

在我的頭在桌子上敲了很多,我終於在迂迴中偶然找到答案辦法。

我的SQL_IsJobRunning和SQL_JobSucceeded方法使用SQL中的sp_help_job來判斷作業是否仍在運行,以及它是否成功。我正在研究成功/錯誤消息以顯示在我的標籤中,當我得到錯誤的消息時,我意識到「current_execution_status」顯示工作已完成,但'last_run_outcome'尚未更新到我的時間代碼正在看價值。因此,我在兩種方法之間放了一個暫停(Thread.Sleep(4000)),以便數據庫在我檢查之前記錄last_run_outcome。

這解決了我的標籤錯誤信息問題,並且還解決了我的gridview問題的愉快的副作用。隨着暫停,gridview也成功更新。一定有一些事情發生得太快,GridView無法正確更新。我只是希望我知道什麼。也許BindRatesGrid()方法在數據在數據庫中被提交之前正在運行。

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

我會建議把你的網格放入更新面板。它看起來像當你點擊按鈕,頁面不刷新回發,因此網格不是...

+0

看起來像一個好主意,但顯然FileUpload不能很好地與UpdatePanel一起玩。我沒有多少運氣。 – BeachBum 2012-02-09 18:30:20

相關問題