Click here to Skip to main content
15,880,651 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello friends
i have to convert my linq result set to datatable
for this i am using the code

char HeadType = _session.HeadType;
     var res = db.DispHed(_session.CompId;
     DataView dv = _global.ToDataTable(db, res).DefaultView;

and ToDataTable methos in class file is

public DataTable ToDataTable(System.Data.Linq.DataContext ctx, object query) 
    {
        DataTable dt = new DataTable();
        if (query != null)
        {
            IDbCommand cmd = ctx.GetCommand(query as IQueryable);
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = (SqlCommand)cmd;
            try
            {
                cmd.Connection.Open();
                adapter.FillSchema(dt, SchemaType.Source);
                adapter.Fill(dt);
            }
            finally
            {
                cmd.Connection.Close();
            }
        }
        return dt; 
    } 

but on reaching line
IDbCommand cmd = ctx.GetCommand(query as IQueryable);
it is giving error

MSIL
Value cannot be null.
Parameter name: query


thanks in advance
Posted
Updated 13-Mar-11 20:40pm
v2
Comments
Sharma Richa 14-Mar-11 2:36am    
I got the reason but not the solution.
the reason is that when it convert query to IQueryable. it is null. but why?

1 solution

The problem is to do with whatever is calling ToDataTable: the object passed as "query" is not null, but it does not implement the IQueryable interface.
As a result, the cast query as IQueryable returns null, and ctx.GetCommand complains.
 
Share this answer
 

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