I have been searching on how to fix this for many hours now and I cant seem to quite figure it out. What I am trying to do is select a portion of 1 table and copy it to a temp table (my application needs to work with specific groups at a time). When I try to implement the CopyToDataTable() method i get an error saying:
Parameter count mismatch
and it highlights line 137 in the ObjectSchredder.cs file (the one from microsofts website). My code is below:
private void fillTempTable()
{
IEnumerable<DataRow> tempResults = from row in sourceTable.AsEnumerable()
where row.Field<String>("GroupID") == GetCurrentGroupName()
select row;
tempGroup = tempResults.CopyToDataTable<DataRow>(); //Everything seems to work until this line
}
You don't need the CopyToDatatable method which works with any types(even with anonymous types) that Microsoft provides with the ObjectShredder class. You are selecting DataRows from a DataTable, hence the normal CopyToDataTable works without a problem. It is sitting in the DataTableExtensions class in the System.Data.DataSetExtensions.dll.
So i assume this is just a namespace issue. Remove the using from the dynamic CopyToDataTable extension method and try this:
tempGroup = tempResults.CopyToDataTable();
If that doesn't help rename the custom CopyToDataTable<T> method.
Related
Im trying to read a csv file using CSVTools library the code that I have to use to get the csv file in to a data table is,
var dt = DataTable.New.ReadLazy(filename);
But the problem is there is no ".New" keyword. When I write DataTable.New it shows an error. Can someone help me ?
You need to include using statement on top of c# file as below
using DataAccess;
or use
var dt = DataAccess.DataTable.New.ReadLazy(filename);
Which means the DataTable that you are using is not belongs to the
expected namespace, use fully qualified name to get the correct
class.
You may have both using DataAccess; as well as System.Data in your using section, so by declaring DataTable alone will made compiler to assumes that it belongs to System.Data. By specifying the DataTable as DataAccess.DataTable you can help the compiler to find the exact class that you are looking for. This kind of specification is called fully qualified names. Make use of them and come out from that specified error. Your declaration will be like the following:
var dt = DataAccess.DataTable.New.ReadLazy(filename);
I want to make a generalized Data Table to Linq Collection
I am a beggginer so if it's not possible please let me know
public void Something(DataTable dt)
{
var data = from row in dt.AsEnumerable()
select new {
Order = row["Order"].ToString(),
Something = row["Something"].ToString(),
Customer = row["Customer"].ToString(),
Address = row["Address"].ToString()
};
}
That is the code for one table
i want something like this:
public static void convertDatatable(DataTable dt)
{
var results = from myRow in dt.AsEnumerable()
select new
{
foreach(DataColumn column in dt.Columns)
column.ColumnName // linq Variable name
= myRow[column.ColumnName];// linq Variable Value
};
}
I know it doesn't work how i wrote it but is there another way ?
Note: the reason i am doing this is because i can't convert Datatable directly to JSON it serializes it to XMl then sends it as a string containing that xml.
If you want to stay with datatables then there is this, mentioned in another SO: What should I use to serialize a DataTable to JSON in ASP.NET 2.0?, which links to What should I use to serialize a DataTable to JSON in ASP.NET 2.0?.
I highly recommend, however, that you consider moving away from DataTables and DataRows, replacing it instead with an ORM such as Entity Framework (EF Quick Start here) or Linq to Sql - there are others, but since you are a beginner these offer the easiest learning curve; not least because of the full designer support in Visual Studio.
For the standard forms of JSON serialization offered by .Net (e.g. WCFs DataContractSerializer or the Asp.Net JSON serializer) then you need concrete types. The ORM solution will create all your table wrapper types at design-time, giving you a concrete type, potentially, for every table in your database.
As for the idea you've specifically outlined above, it is exceptionally difficult to achieve - because the compiler, in the first example, dynamically generates a type whose members match the names and types of the expressions you use. If you open your compiled code in ILSpy and switch to IL instead of C# you'll see what I mean.
Therefore, to reproduce it dynamically you would need to dynamically emit a class, probably using ILGenerator, doing the same thing; and then dynamically emit the expression tree (using the Expression class' static factory methods) to fill it out; and finally compile and execute it.
I would only look at doing something like that if I literally couldn't do it any other way - I'd be more likely to just write a routine to iterate through each column and write the JSON to a StringBuilder and return that! But if I could use an ORM, then I'd do that instead.
I wondering why it throws a error whenever i not intended to give name of datable.As we know there are constructor for datable class is overloaded.so If i using parameter less constructor is give the error by the basis of serialization .
Can Someone Explain Me why to use Parametrized one but not default for DataTable .
It's because the name is required for the data table to serialize properly.
Why is this? Well, the exact reason seems to be that the serialization process uses the table name as a key, and specifically, an empty data set is created to import it back in. When the name is not present, the part that looks for the table name throws an exception and this is why you see the error you are getting.
You don't have to use the constructor, though, you can set the TableName outside:
DataTable myTable = new DataTable();
myTable.TableName = "PleaseDontKillMySerialization";
If you are interested, you can look at the contents of the class using ILSpy. This way you can see for yourself how the class is created and look at how serialization works for this class.
As Marc mentions, though, using DataTable restricts you to .Net clients only. It's also quite a large object when serialized so more data has to be transferred per request.
I have a DataSet in UserAdmin.xsd with many DataTables. Most of the data come directly from stored procedures. However for one of the tables, I would like to add another column which uses a C# function defined in another file.
I put for the expression for that column: Helper.ObtainUserInfo(user_nm, "displayname"); but that gives me an error "undefined function call".
Helper.cs is located under App_Code/Common/ and the namespace is COM.A.B.C. UserAdmin.xsd is located under App_Code/.
How can I access the function ObtainUserInfo()? Is there something like a using keyword that I could use?
You cannot use DataColumn.Expression to call a .NET-Method to obtain a value. You must refer a column in this table or one of the parent/child tables to calculate the value. For more informations on what you can('t) do with Expressions look here: http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression%28v=VS.100%29.aspx
Instead of using a method or an expression, i would recommend to do this with SQL whenever possible.
if You provide your code, then it would be very helpful to solve your problem, anyway assuming your problem i have solution, i hope it may helped you
You can open the Code File Which Contain ObtainUserInfo() Function, then You can Refer that Class Name, in other File.
For eg : If you hav solution Named WebApplication1, inside that if you have users class, in that users class, ObtainUserinfo() function is defined,
then You Have to Use statement like this,
Using WebApplication1.Users
I have a setup where I used Linq2SQL inheritance. To make queries easier, I expose the derived types in the DataContext as well, like the following:
public IQueryable<Derived> Derivations
{
get { return Bases.OfType<Derived>(); } // filter list on type
}
Calling this works perfectly, and I can see the SQL being correctly generated. The backing type is DataQuery<T>.
The problem comes in when I assigning this IEnumerable to a datasource (either a control or a BindingSource).
From what I can see, the DataQuery object is queried for an IListSource. And it happily supplies this. Then it proceeds to make a BindingList, which fails as the type parameter of the 2 arguments supplied (IEnumerable<Derived> and Table<Base>) does not match. It raises an exception of MissingMethod as the constructor cannot be found.
The simple workaround is just to call ToList() on the IQueryable<Derived> before assigning to the datasource and then it works, but this is quite tiring.
Any suggestions to handle this without 'loosing' the IQueryable?
Thanks
leppie
UPDATE:
The bug has now been reported to MS. More details here. Thanks Marc!
Confirmed. Looks like a bug to me; you should log it on Connect. The team are fixing LINQ-to-SQL bugs, so it might not be ignored. For now, use .ToList() etc.
Sample code:
using (var ctx = new MyDataContext())
{
var qry = ctx.BaseEntities.OfType<DerivedEntity>();
IListSource ls = (IListSource)qry;
IList list = ls.GetList(); // boom
/* Constructor on type
'System.Data.Linq.Provider.DataBindingList`1[snip]'
not found.*/
}
I had the same issue (still not fixed MS guys!).
To keep the IQueryable I did a .Cast<object>() when assigning to the datasource (i use it to output a xls file from any L2S table i want in a DynamicData website).