【发布时间】:2012-03-01 06:50:53
【问题描述】:
我所做的所有研究似乎都表明,如果我再次调用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.";
}
}
}
}
【问题讨论】:
-
我认为您的
// TO DO: handle exceptions是 GridView 未更新的原因 ;-) -
当我单步执行代码时,它从未遇到异常,所以我不确定为什么会这样。
-
在该部分添加一些代码没有任何区别。
标签: c# asp.net gridview data-binding