dodgerfan
asked on
Biding Multiple Gridviews one one page
yv989c:
The code you posted works great. I have another related question. What is the best way to to do the databinding when I need to do it multiple times on one page. I actually have 5 grids on the page, each using a different stored procedure (but the same UserID parameter) to pull data from different tables. I guess I'm looking for the cleanest, best way to do it. And where is the best place to close the connection?
private void BindData()
{
using (SqlConnection cn = new SqlConnection(System.Confi guration.C onfigurati onManager. Connection Strings["M yProject"] .Connectio nString))
{
SqlCommand cm = new SqlCommand("MyStoredProced ure", cn);
cm.CommandType = CommandType.StoredProcedur e;
cm.Parameters.Add("@UserID ", SqlDbType.Int).Value = SessionHelper.GetUserId();
cn.Open();
using (SqlDataReader dr = cm.ExecuteReader())
{
MyGrid.DataSource = dr;
MyGrid.DataBind();
}
}
}
The code you posted works great. I have another related question. What is the best way to to do the databinding when I need to do it multiple times on one page. I actually have 5 grids on the page, each using a different stored procedure (but the same UserID parameter) to pull data from different tables. I guess I'm looking for the cleanest, best way to do it. And where is the best place to close the connection?
private void BindData()
{
using (SqlConnection cn = new SqlConnection(System.Confi
{
SqlCommand cm = new SqlCommand("MyStoredProced
cm.CommandType = CommandType.StoredProcedur
cm.Parameters.Add("@UserID
cn.Open();
using (SqlDataReader dr = cm.ExecuteReader())
{
MyGrid.DataSource = dr;
MyGrid.DataBind();
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Another recommendation about sql connections, when you open a connection, always keep in mind to have it open the less time that you can. Is better to open and close it multiples times inside a long running method that having it opened all the way. That is because there is something called the connection pool, and when you leave a connection opened a long time it is not available to other threads, however, if you close it, it return to the connection pool (and remains open for a while), in a way that can be reused for another thread. I'm not sure if I'm explaining this very well, is a little more complex. Just keep this in mind:
ASKER
Perfect, again. And thanks for the tips on db connections. It's easy to forget about doing things properly when you get caught up in trying to get it done. Thanks again.
You're welcome