0

I have the following code:

GenericManager gm = new GenericManager();
DataSet ds = new DataSet();
ds = gm.ExecuteQuery(gr.TableName, gr.ColumnName, gr.WhereClause);
return Json(ds, JsonRequestBehavior.AllowGet);

In which ExecuteQuery is the method of the class GenericManager which will return dataset and that dataset is saved into the object ds.

I am implementing a functionality that will fetch data from different table everytime. It means that to populate the DataSet i am passing the name of the table of which data would be populated into the DataSet. So, I have to pass the dataset from Json everytime because i am not sure about the columns returned by the query.

If I try to return the dataset through Json then the following exception is thrown: A circular reference was detected while serializing an object of type 'System.Globalization.CultureInfo'.

So, i am looking for the way that will pass my DataSet to Json.

The following code that i have already tried is:

return Json(ds.Tables[0].Rows[0], JsonRequestBehavior.AllowGet);

Also,

 return Json(ds.Tables.AsQueryable(), JsonRequestBehavior.AllowGet);

Please help to fix it, as i have to submit it by the end of the day

tereško
  • 58,060
  • 25
  • 98
  • 150
HarshSharma
  • 630
  • 3
  • 9
  • 34
  • You don't want to return the `DataSet`. You want to return the data. Although `DataSet`s have been XML serializable from the beginning, it was never recommended to do so because they are too platform specific. – Paulo Morgado Feb 27 '14 at 11:22
  • yeah @PauloMorgado but why i am returning DataSet because i have to select data from different table every time, because i am incorporating the sql like functionality at the front end from which user can pick the table name and write there `where` query and then can make the statement `GO` – HarshSharma Feb 27 '14 at 11:33
  • why don't you make a query to explicitly select what you want to return? Do you really want to return culture infos? – Paulo Morgado Feb 27 '14 at 12:13
  • @PauloMorgado sorry but i dnt understand u, can u please explain a bit more what do you want to say – HarshSharma Feb 27 '14 at 12:14
  • Something like `from t in ds.Tables select new { Table = t.TableName, Data = from r in t.Rows select new { ... } }`. – Paulo Morgado Feb 27 '14 at 12:32
  • sorry but i am new to mvc, i want a proper explanation to understand this, you can post it as an answer i will not mind, but please help me understanding it fully. – HarshSharma Feb 27 '14 at 12:37
  • And I know almost nothing about MVC. But from what I've looked up, `Controller.Json` serializes an object to JSON. What I'm recommending you to do is not to use the `DataSet` but to build an object that has only what you explicitly want. I would build the JSON myself instead of relying on such a generic serializer, though. – Paulo Morgado Feb 27 '14 at 12:42

1 Answers1

0

This Code helped me a lot. For those who run into the same problem I suggest to check the solution.

Muhammad Musavi
  • 2,512
  • 2
  • 22
  • 35
HarshSharma
  • 630
  • 3
  • 9
  • 34