Create object at runtime based on SqlQuery executed - c#

The overall objective is to get a Json representation of the query results of the SqlQuery executed. This Json will be used to create visualizations/reports on the browser using js based charting tools.
Now, controls like gridview are able to read the column names as well as the data and give us an html representation of the data. So I think it should be possible to write code such that it can read from a sql data reader and come up with a json representation.
I could not find anything in my searches which does what I want. How do I go about doing this? any pointers?

You could use an SqlDataAdapter to fill a DataSet. This blog post describes a way of converting a DataTable or DataSet into its JSON representation.

You could use the Json.Net serializer. It supports serializing a Dictionary<string,object> to a JSON object.
Another big shot would be using NHibernate and serializing the resulting objects.
Here is another link to using the Json.Net serializer for DataSets:
If you scroll down to the comments on this page you see a much shorter solution using the Dictionary approach.

Related

Custom jQGrid summaryType in JSON string

I am dynamically building the colModel for my jQGrid using fields from a DataTable. Nearly all of it works as I hoped. However, I am unable to use a custom summaryType because I can't serialize without the quotes and jQgrid doesn't look for the method if in quotes.
Presently, if I don't remove the ", I get the following error when loading the grid:
Uncaught jqGrid Grouping No such method: mysum
If I remove the quotes in the table, I get the following error when serializing:
Invalid JSON primitive: mysum.
What is the best way to tackle this problem?
You will probably have to create a solution in the view using javascript. If you manually serialize the colModel to send to the view, you will have to manually deserialize. You will not be able to parse it with a JSON parser because it will not be a valid JSON string.
One possible approach would be to use eval() on the string like this:
myObject.property = eval("mysum");
It should replace the string with the function. I am not sure if it meets your needs, but will avoid both errors you listed above.

How to get json output from a webservice as a datatable in c#?

i have a webservice (wsdl) generated from a java project. its output is getting as json format.
something like this
[{"pid":163686,"chartno":null,"lName":"Bec&&kwith","fName":"Burt","mName":null,"line1":"312 HILL ROAD","line2":null,"city":"Hillsboro","state":"Missouri","pinCode":null,"phone":"123456879","dob":"1947-01-01","gender":"Male","ssn":null,"martialStatus":null,"guarantor":null,"sig":null,"priInsname":null,"priPolNo":null,"secInsname":null,"secPolNo":null}]
i want to use this data in my c# web application to display it in a grid.. how can i get this data into a datatable?
how to format the json output to a table structure if multiple data comes?
The heading will be pid, chartno, ... etc .
i dont want to make it too complex... i just want the json output to store into a dictnory or somthing ..
thank u
I would deserialize the JSON in to a custom c# object instead of a DataTable.
Deserialize JSON string to c# object
Essentially you can use the JavaScript serializer to deserialize your JSON string into the object. I recommend this over the data table since you would have to manually map it to the table The grid should be able to consume a List of your custom object instead of a data table
You need to Parse json , you can JQuery to parse json object.

Populate Cellset using Objects

I have been working with MDX and cellset in recent times. I was given a MDX query which can show up the data in 3 dim format and I able to get the data using CELLSET in .Net code. Later I am converting the cellset to datatable to make it lot easier to manipulate and display in the application. (similiar to the code from : http://asmdx.blogspot.in/2008/05/code-utility-code-for-converting.html )
I was just wondering why do I need to use Datatable which eats up considerable amount of memory.. I got to think of replacing the datatable with Objects. i.e., Converting a Cellset to a collection of user defined objects.. Is tat possible? Any help please ?
You could get the MDX query results in a XML format using the ExecuteXmlReader method of ADOMD.NET: your memory problems would be solved, and then you could with (relative) ease consume the resulting XML in your application (you could, for instance, use Linq for XML to transform the XML into business objects).

how to insert data into db as a serialized object

my basic question is how to insert data into DB as a serialized object and how to extract and use it then ... any suggestion !!?
e.g :
{id:1, userId:1, type:PHOTO, time:2008-10-15 12:00:00, data:{photoId:2089, photoName:A trip to the beach}}
as you see how could I insert data into column Data and then to use it !?
another question is that if I stored the photoName inside Data instead of using JOINS and get the name from it's table (photos) according to it's Id thats will not implement the last update on the photoName (right !?) besides that I'll not be able to make a relation between table photos and the Current table - (Id => photoId) - if I stored data like that .. so part of the problem is that I don't know exactly what kind of information are going to be stored in colum Data So I can't customize a separate column for every type of these information ...
Typically I see two options for you here.
You can store an XML serialized object into the database, and simply use standard XML Serialization, here is an example that you can adapt for your needs.
You can create a true table for this object, and do things the "Standard" way.
With option 1, filtering/joining/searching on the information in the "data" column although still technically possible, is NOT something i would recommend and would be more for a static storage process in my opinion. Something like a user settings entity, or some other item that is VERY unlikely to be needed for a backend query.
With option 2, yes, you have to do more work, but if you define the object well, it will be possible.
Clarification
With regards to my example in #1 above. You would write out to a memory stream, etc for the serialization rather than a file.
If you don't want to store the data relationally, you're really better off not using a relational database. Several object databases speak JSON and would be able to handle this kind of problem pretty easily.
You can store it as JSON string and use JSONSerializer of JSON lib
http://json-lib.sourceforge.net/apidocs/index.html
to convert javabean into json string/object and vice versa.
Generally we use this to store the configuration where no of config parameters are unknown.
Regarding saving an object in your database; you can serialize your object into xml using XDocument.ToString() and save it in database's xml datatype column.
cmd.Parameters.AddWithValue("#Value", xmldoc.ToString());
Checkout, Work with XML Data Type in SQL Server

XML tag name being overwritten with a type defined

We are communicating with a 3rd party service using via an XML file based on standards that this 3rd party developed. They give us an XML template for each "transaction" and we read it into a DataSet using System.Data.DataSet.ReadXML, set the proper values in the DataSet, and then write the XML back using System.Data.DataSet.WriteXML. This process has worked for several different files. However, I am now adding an additional file which requires that an integer data type be set on one of the fields. Here is a scaled down version:
<EngineDocList>
<DocVersion>1.0</DocVersion>
<EngineDoc>
<MyData>
<FieldA></FieldA>
<FieldB></FieldB>
<ClientID DataType="S32"></ClientID>
</MyData>
</EngineDoc>
</EngineDocList>
When I look at the DataSet created by my call to ReadXML to this file, the MyData table has columns of FieldA, FieldB, and MyData_ID. If I then set the value of MyData_ID and then make the call to WriteXML, the export XML has no value for ClientID. Once again, if I take a way the DataType, then I do have a ClientID column, I can set it properly, and the exported XML has the proper value. However, the third party requires that this data type be defined.
Any thoughts on why the ReadXML would be renaming this tag or how I could otherwise get this process to work? Alternatively, I could revamp the way we are reading and writing the XML, but would obviously rather not go down this path although these suggestions would also be welcome. Thanks.
I would not do this with a DataSet. It has a specific focus on simulating a relational model. Much XML will not follow that model. When the DataSet sees things that don't match it's idea of the world, it either ignores them or changes them. In neither case is it a good thing.
I'd use an XmlDocument for this.

Categories