Click here to Skip to main content
15,901,871 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all.
i want to use generic list list<t> in ado.net . currently i am using dataset to copy all my data from database to it . but how can i use generic list...I am asking it becoz i want to bind my
gridview to generic list not to dataset. How can i do it? what i want to do in my DAL is as follows:
XML
namespace datalayer
{
    public class DataLayer
    {
        DataSet dataset;
        SqlDataAdapter dataadapter;
        String constr = ConfigurationManager.ConnectionStrings["Treasury"].ConnectionString;
        SqlConnection connection;
        SqlCommand command;
        DataTable datatable;
        List<string> obj = new List<string>();

        public List<string> fillgridview()
        {
            connection.Open();
                        command = new SqlCommand("spuserdetailsinfo", connection);
                command.CommandType = CommandType.StoredProcedure;
                dataset = new DataSet();
                dataadapter = new SqlDataAdapter();
                dataadapter.SelectCommand = command;
                dataadapter.Fill(dataset);
            obj = List<string> dataset ;

        }

but this line obj = List<string> dataset; is showing error as
Quote:
Error 3 Using the generic type 'System.Collections.Generic.List<t>' requires '1' type arguments d:\substituteofsqlhelp (2)\datalayer\DataLayer.cs 35 19 datalayer
Posted
Updated 7-Aug-13 23:50pm
v2

1 solution

hi , you can do as follow :

XML
List<string> years   = dataSet.Tables[0].AsEnumerable()
                            .Select(r => r.Field<string>(0))
                            .ToList();
List<double> doubles = dataSet.Tables[0].AsEnumerable()
                            .Select(r => r.Field<double>(1))
                            .ToList();


you could not directly assign data to list
 
Share this answer
 
Comments
Rambo_Raja 8-Aug-13 6:01am    
thanx ....!+5...
Rambo_Raja 8-Aug-13 6:26am    
hi...prateek when i use quick watch then it i see count = 5 that is the number of rows..when i bind grid to the years object it says 'A field or property with the name 'name' was not found on the selected data source'. because it is returning int value by which we can't bind gridview how to overcome this problem? thanx...
vyas_pratik20 8-Aug-13 6:31am    
I think you might need to add bound filed in data grid with
<asp:BoundField DataField="name" HeaderText="name"
SortExpression="name" />
Rambo_Raja 8-Aug-13 6:35am    
no no no...that i have done...i think that the list control return only single column .....is it right?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900