What arguments to send to the CopyToDataTable Method

Posted by Sharpcnet under C# on 10/31/2013 | Points: 10 | Views : 5153 | Status : [Member] | Replies : 7
With the procedure from -http://msdn.microsoft.com/en-us/library/bb669096%28v=vs.110%29.aspx and some great help at DNF, I could make it to this point.

But, I get the following error, at the CopyToDataTable method.

ArgumentNullException
Value cannot be null.
Parameter name: source

Where am I going wrong.
public DataTable GetAllRecords()
{
try
{
DataTable dt = new DataTable();
IEnumerable<DataRow> query = ((from p in MedianDB.tblCountries
select p).OrderBy(p => p.CountryName)) as IEnumerable<DataRow>;
query.CopyToDataTable<DataRow>(dt,LoadOption.PreserveChanges);

return dt;
}
catch (Exception ex)
{
throw ex;
}
}
Using .Net Framework 4.0,C#,Linq
Thank you.




Responses

Posted by: Bandi on: 10/31/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi,

That is not the issue with CopyToDataTable() method... While coverting table data to DAtaRow itself it is getting null....

 DataTable dt = new DataTable();
IEnumerable<DataRow> query = ((from p in MedianDB.tblCountries select p).OrderBy(p => p.CountryName)) as IEnumerable<DataRow>;

if (query == null)
{ throw new NullReferenceException("Cannot be null!!!"); }


To get know that put a condition for checking whether query returns null or datarows

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 10/31/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
For binding data to GridView you can do as follows:
using (studyEntities1 sdContext = new studyEntities1())
{
var selectQuery = GetAllRecords();
fetchGridView.DataSource = selectQuery;
fetchGridView.DataBind();
}

public List<JOB> GetAllRecords()
{
try
{
DataTable dt = new DataTable();
studyEntities1 sdContext = new studyEntities1();
var query = (from p in sdContext.JOBS select p).OrderBy(p => p.MIN_SALARY).AsEnumerable();
return query.ToList();
}
catch (Exception ex)
{
throw ex;
}
}



references for other detailed approaches:
http://msdn.microsoft.com/en-us/library/bb386921.aspx
http://stackoverflow.com/questions/4460654/best-practice-convert-linq-query-result-to-a-datatable-without-looping?rq=1
http://www.codeproject.com/Articles/493917/Dynamic-Querying-with-LINQ-to-Entities-and-Express

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sharpcnet on: 10/31/2013 [Member] Starter | Points: 25

Up
0
Down
Hi, Your second solution would work for me, but what If I have to use joins.
My Fundamental requirement is to return a datatable.
I could happily go with your soln. in http://www.dotnetfunda.com/forums/show/16704/copytodatatable-does-not-show-up-as-a-method-in-linq, but,if the no. of columns are many, we need to name every column. I have many forms that need a similar function to return datatable and hence thought, CopyToDataTable would be a fine way.
Could you please suggest, how can I change the linq so that I can use the CopyToDataTable method.

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 10/31/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
For paging and Sorting we should follow some approaches..

Use DataView for binding data
http://dotnetarchitect.wordpress.com/2009/03/18/using-linq-to-manipulate-data-in-datasetdatatable/
http://blogs.msdn.com/b/erickt/archive/2007/08/24/linq-to-dataset-data-binding.aspx
http://msmvps.com/blogs/deborahk/archive/2009/07/23/linq-sorting-a-datatable.aspx
http://www.asp.net-crawler.com/articles/linq/Using-LINQ-to-objects-query-collections-datatable-filter-manipulate-rows-syntax.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sharpcnet on: 10/31/2013 [Member] Starter | Points: 25

Up
0
Down
AsDataView does not show up in the intellisense. I do have System.Data.DataSetExtensions as reference.

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 10/31/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
refer
http://social.msdn.microsoft.com/Forums/en-US/39183afa-6891-486b-a957-293661e05927/linq-to-dataset-and-asdataview?forum=adodotnetentityframework
http://stackoverflow.com/questions/2393223/binding-linq-query-to-datagridview

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sharpcnet on: 10/31/2013 [Member] Starter | Points: 25

Up
0
Down
But, I'm emulating just as in here http://msmvps.com/blogs/deborahk/archive/2009/07/23/linq-sorting-a-datatable.aspx
var query = from p in MedianDB.tblCountries.AsEnumerable()
select p;
DataView dv = qry.AsDataView();
Why it isnt working? I dont even see the Field method.
Is it because of the Entity Data Model.

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response