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.
Related
I'm actually pursuing a way to create csv file which records could vary on type and order. They are defined by the user on both ways and I'm actually handling their types safely.
I loved using FileHelpers library in order to read/write files on C# since it's fast, reliable and trustable, so I was wondering how could I perform this export operation using it, and reviewing questions like this one the evil part comes when needing to populate the class with the desired values, so I could write the file. All the related questions are focusing on reading registries and I need to write them.
Am I right thinking that I might need to use Reflection so I could roam this new type and its properties or is there any way to "add a record" specifying the value while creating the fields?
This FileHelpers way was an option and of course the second one was doing this manually, but I was curious if there is an easier way.
var builder = new DelimitedClassBuilder("DynamicDocument", ";");
builder.AddField("Date", typeof(DateTime));
var dynamicType = builder.CreateRecordClass();
//...
Using the class builders is the best way to do this with FileHelpers. You do need to keep a copy of the type that is created so it can be used by your generic classes.
Just remember, that you must do all the work before calling CreateRecordClass() as that then generates the type.
Here is a link to another S/O question with a whole bunch of code that shows how to do it: FileHelpers.Dynamic.ClassBuilder.CreateRecordClass Error
Now, if you are working purely with properly formatted files, you can let FileHelpers do all that work for you as long as they are always properly delimited and you handle any type conversion based on the column name.
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 am trying to dynamically add field properties to a record class that I am also building dynamically using FileHelpers.Dynamic.DelimitedClassBuilder. I have no issues creating the class object and I currently add a field using the AddField(String) method.
As my apps grows I now have a need to declare specific field properties in various situations. So in the same sense I wanted to use FileHelpers.Dynamic.DelimitedFieldBuilder to create a field object and then pass that to my DelimitedClassBuilder object using the method AddField(DelimitedFieldBuilder).
However I am unable to instantiate a new object using FileHelpers.Dynamic.DelimitedFieldBuilder. When I issue the following code I get an error stating that DelimitedFieldBuilder does not contain a constructor that takes two arguments.
FileHelpers.Dynamic.DelimitedFieldBuilder fb = new FileHelpers.Dynamic.DelimitedFieldBuilder("ClassName", "Type");
Looking at the documentation it appears that this class does only have properties associated with it, so I am kind of stuck on how to actually implement this. It seems like it should be fairly easy but I cant seem to figure it out. Thanks for any help.
Not familiar with that functionality of file helpers; however, in the vast majority of functions/methods across .NET there is usually a way to assign properties after the class is instantiated.
Try something like this:
FileHelpers.Dynamic.DelimitedFieldBuilder fb = new FileHelpers.Dynamic.DelimitedFieldBuilder();
fb.Whatever = "ClassName";
fb.otherwhatever = "Type";
Just a stab. I have no idea if it will work or not.
The constructors of DelimitedFieldBuilder are internal so you'll run into difficulty with your approach. However AddField(String) returns a DelimitedFieldBuilder, so you might be able to use that.
It might be easier to make your own class MyFieldBuilder which calls the standard AddField(String).
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
This may seem like a simple question however i've spent the last hour trying to solve this.
I want to create a custom subsonic collection so that i can fill it with data from a query with multiple joins using the .ExecuteAsCollection<>(); method.
i've created the custom class and a custom collection and even a controller with the load method but i keep getting a null refernce exception from the ExecuteAsCollection<>();
the stack track says its an error coming from the SubSonic.Load method.
i have left out the class's "SQLProps" that all the other subsonic classes have, but i was hoping i wouldn't have to go through each field painstakingly.
There has to be something simple i'm missing. Can someone who's done this give me a quick run down on what is required by subsonic to fill a custom collection with a query?
thanks
Doug
UPDATE:
i forgot to mention that i also added the public Columns struct with all my columns.
ExecuteAsCollection<T>() will only work with SubSonic-generated collections. You can map the result of a query to an arbitrary object type with ExecuteTypedList<T>(). This will match the columns returned from your query to properties of type T with the same name and give you List<T>.