J
JohnE
Hello. I am working on an ASP.net webapplication (using C#) and I have a
gridview that fills with data when it opens. Below is the code that fills
the gridview. Now, what I am getting is conflicting information from
co-workers that I need to open the connection and then close it. To me, for
this gridview there is no need to open and close the connection since I'm
filling an underlying dataview. There is a Refresh button available that
restores the gridview back to its original settings but since it is true, the
refresh does not enter into the if statement of the page_load.
private DataView bindgrid()
{
string connStr =
ConfigurationManager.ConnectionStrings["ProteusConnectionString"].ConnectionString;
string sql = "SEL_FillChangeRequestListGridView";
SqlDataAdapter sqlDa = new SqlDataAdapter(sql, connStr);
DataSet ds = new DataSet();
sqlDa.Fill(ds);
DataView dv = new DataView();
if (ViewState["sortExpr"] != null)
{
dv = new DataView(ds.Tables[0]);
dv.Sort = (string)ViewState["sortExpr"] + " " +
ViewState["sortDir"];
}
else
dv = ds.Tables[0].DefaultView;
return dv;
}
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!Page.IsPostBack)
{
DataView dv = new DataView();
dv = bindgrid();
gvwChangeRequestList.DataSource = dv;
gvwChangeRequestList.DataBind();
}
lblSortColumn.Visible = true;
lblSortColumn.Text = "Default sort: STATUS, workflow in
descending order.";
PageCount();
TotalCount();
}
catch
{
string message = "The connection failed on the page load. Try
again or contact Administrator.";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script type='text/javascript'>");
sb.Append("window.onload=function(){");
sb.Append("alert('");
sb.Append(message);
sb.Append("')};");
sb.Append("</script>");
ClientScript.RegisterClientScriptBlock(GetType(), "alert",
sb.ToString());
}
}
So, I guess my question is, do I need to open and close the connection on
this? Also, what is the "standard" (or rule of thumb) for when connection is
opened and closed?
Thanks.
John
gridview that fills with data when it opens. Below is the code that fills
the gridview. Now, what I am getting is conflicting information from
co-workers that I need to open the connection and then close it. To me, for
this gridview there is no need to open and close the connection since I'm
filling an underlying dataview. There is a Refresh button available that
restores the gridview back to its original settings but since it is true, the
refresh does not enter into the if statement of the page_load.
private DataView bindgrid()
{
string connStr =
ConfigurationManager.ConnectionStrings["ProteusConnectionString"].ConnectionString;
string sql = "SEL_FillChangeRequestListGridView";
SqlDataAdapter sqlDa = new SqlDataAdapter(sql, connStr);
DataSet ds = new DataSet();
sqlDa.Fill(ds);
DataView dv = new DataView();
if (ViewState["sortExpr"] != null)
{
dv = new DataView(ds.Tables[0]);
dv.Sort = (string)ViewState["sortExpr"] + " " +
ViewState["sortDir"];
}
else
dv = ds.Tables[0].DefaultView;
return dv;
}
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!Page.IsPostBack)
{
DataView dv = new DataView();
dv = bindgrid();
gvwChangeRequestList.DataSource = dv;
gvwChangeRequestList.DataBind();
}
lblSortColumn.Visible = true;
lblSortColumn.Text = "Default sort: STATUS, workflow in
descending order.";
PageCount();
TotalCount();
}
catch
{
string message = "The connection failed on the page load. Try
again or contact Administrator.";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script type='text/javascript'>");
sb.Append("window.onload=function(){");
sb.Append("alert('");
sb.Append(message);
sb.Append("')};");
sb.Append("</script>");
ClientScript.RegisterClientScriptBlock(GetType(), "alert",
sb.ToString());
}
}
So, I guess my question is, do I need to open and close the connection on
this? Also, what is the "standard" (or rule of thumb) for when connection is
opened and closed?
Thanks.
John