我所做的所有研究似乎都表明,如果我再次简单地调用DataBind(),那么我的gridview将得到更新。仅当我调试并逐步执行代码时,GridView才能正常刷新。但是,如果在调试模式下运行应用程序时未逐步执行代码,则下面的btnFileImport_Click方法不会刷新我的gridview。它与我通过使用SSIS包加载文件来更新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.";
            }

        }
    }
}

最佳答案

我建议您将网格放入“更新面板”中。看起来当您单击按钮时,回发页面不会刷新,因此网格也不会...

10-06 08:12
查看更多