.Net to .Net Core Schema data set transfer

danielarmbruster0314 - May 15 '23 - - Dev Community

So recently I ran into a scenario that I felt did not have much resources. I wanted to share so that any dev who faces a similar problem could have some point of reference.

The problem: I had a dataset in a .Net C# application that I wanted to be sent through an api call to a .Net Core application that was to act as a separate service handler but had the data required to service the request.

The obstacle is that in .Net data sets with DB.Null values are allowed for int, bool, DateTime, and decimal(converted into null values when read). However, in .Net Core it is of stricter typing so just copying the data set from one application to the other would not work, because any time it would attempt to evaluate a DB.Null for those types it would throw an exception.

possible solution: The work around would be to locate the properties field for each value of those types in the schema .xsd file and change the NullValue field from (Throw Exception) to a default value for that type (example: int a default value of 0, bool a default value of false, DateTime a default value of 00/00/0000 00:00:00:00,etc). this means that if it was to receive a data set that was pointing to the same schema file(copy paste) or read a DB.Null value it would allow the dataset object to replace those DB.Null members with the default values instead of throwing an exception.

I tried many times to write in the xml elements attributes to allow nillable or would allow for those members to be the equivalent of a nullable type (example: int?) though this didn't seem to alleviate the issue. Also did set the AllowDBNull field in the properties panel to true but still got exceptions.

Another solution which is the route I opted for is to create a object that mirrors the data table classes and members for the data set and to map them accordingly and send that object to the .Net Core application but be warned you have to make mirroring objects and first map in the .Net application to send to .Net Core because the api call will fail if you attempt to have it mapped in .Net Core due to the fact that you can not cast a type dataset to object directly, this is because there are multiple rows and columns you are attempting to cast into one object presumably.

if you look at the generated designer file for the data set and make a mirroring classes note that there are multiple rows which is the equivalent of multiple object instances so either map through a specific one (example: dataset.table.FirstOrDefault() ) or through all of them to map them to the mimicked object(example: foreach(var row in datatable){ object dto = row})

Image description

reference link: https://learn.microsoft.com/en-us/visualstudio/data-tools/create-and-configure-datasets-in-visual-studio?view=vs-2022

additional Notes: for those wondering, I did attempt to generate classes and data sets with the xsd.exe terminal commands and though it generated classes or a dataset the classes did not behave the same way with the ability to access members and their values as easily and the data set would still throw exceptions in .Net Core. If any other dev's have insight into potential options or alternatives to this problem your input is welcome

. . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player