Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best way to reuse a datable

Status
Not open for further replies.

princesszea

Technical User
Aug 11, 2008
33
GB
Hi,
I have a method that returns a datatable and fills a grid. At the moment I do a round trip the database 3 times to get the same data that the fills the grid to do some calculations to fill some charts.
Can someone please tell me how I can avoid going back to the database and reusing the first databtable .

At the moment I have the code below which actually populates a Grid and I have added some code that actually reuses the datatable and brings back the results need to populate the chart.i’m not sure if this the best way of doing it. The only problem is I do not want the data that brings back the datable for the grid and chart in the same method I just want to get hold of that databtable once it is returned and do some calculations to populate my charts.

I have 3 methods below in 3 different classes that is called to populate the grid


Code:
public static GridData GetDATA()
        {
            GridData datatable = new GridData();

                dataAccess.GetGridData(datatable);
           

           
            return datatable;
        }


     public void GetGridData(GridData gd)
        {
            if (ConnectionState.Closed == _sqlConn.State)
                _sqlConn.Open();
       
            gd.GetData(ref _sqlConn);
        }


This method gets the data to populate table

       internal void GetData(ref SqlConnection sqlConn)
        {
            
            DataTable dt = new DataTable();

            try
            {
                using (SqlCommand cmd = sqlConn.CreateCommand())
                {
                   // some code

                    }

                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        da.Fill(dt);
                    }
                    this._data = dt;


              ///////////at the monment I’m using the data table to get the results for my chart in the same method which I want to move once I can get hold of the datatable as the results below can be used to populate my chart///////////
                    var query = from row in dt.AsEnumerable()
                                group row by row["name"] into grp
                                
                                let totalcount = dt.AsEnumerable().Count()

                                select new
                                {
                                    Id = grp.Key,
                                    Count = grp.Count(),
                                    TotalCount = totalcount,
                                    Ratio = grp.Count() / totalcount

                                };
                    foreach (var grp in query)
                    {
                        string name = grp.Id.ToString();
                        int Count = grp.Count;
                        int TotalCount = grp.TotalCount;
                        int Ratio = Count * 100 / TotalCount;

                    }
}
                }

Thanks in advance
 
It's ok guys I manged to work it out I will let you know if i have any other problems with the method i intend to use.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top