Link to home
Start Free TrialLog in
Avatar of dodgerfan
dodgerfanFlag for United States of America

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.Configuration.ConfigurationManager.ConnectionStrings["MyProject"].ConnectionString))
    {
        SqlCommand cm = new SqlCommand("MyStoredProcedure", cn);
        cm.CommandType = CommandType.StoredProcedure;
        cm.Parameters.Add("@UserID", SqlDbType.Int).Value = SessionHelper.GetUserId();
        cn.Open();
        using (SqlDataReader dr = cm.ExecuteReader())
        {
            MyGrid.DataSource = dr;
            MyGrid.DataBind();
        }
    }
}
ASKER CERTIFIED SOLUTION
Avatar of Carlos Villegas
Carlos Villegas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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:
Open late, Close soon
Avatar of dodgerfan

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