0

I Fill a dataset by sql query and the dataset contains a large datatable.

DataSet dataSet1 = new DataSet();
SqlDataAdapter ndaGlobalClass = new SqlDataAdapter(Query, cn);
ndaGlobalClass.SelectCommand.CommandTimeout = 0; 
cn.Open();
ndaGlobalClass.Fill(dataSet1);
cn.Close();
string s=JsonConvert.SerializeObject(dataSet1.Tables[0]);

the Query return a large datatable and when i convert it to json by serializing System.OutOfMemoryException' was thrown. How can i fix this problem? i need to serialize large datatable.

MAT14
  • 129
  • 4
  • 17
  • Did you try increasing heap memory while running the program ? – geekprogrammer Apr 03 '15 at 09:39
  • Obviously, the process runs out of available memory to serialize the object into a string. This may be caused by circular references, or simply the object being too large. Research what is causing the issue. If it's simply too large, you can't use your current approach, then you need to [serialize it into a stream](http://stackoverflow.com/questions/9845741/json-net-fails-to-serialize-to-a-stream-but-works-just-fine-serializing-to-a-st) instead of a string. What do you need it in a string for anyway? – CodeCaster Apr 03 '15 at 09:40
  • @Anjith no, just, no. [Even if you could](http://stackoverflow.com/questions/2325370/c-sharp-increase-heap-size-is-it-possible), that's entirely the wrong approach to this problem. – CodeCaster Apr 03 '15 at 09:40
  • I am using this code in webmethod and it return the string as ajax response. and its not cirular issue the issue is it is too large. object size is nearly 1GB and this causes the problem. if i serialize into stream then how can i pass data as ajax response @CodeCaster – MAT14 Apr 03 '15 at 09:51
  • 1
    Why on earth would you want to return 1 GB of JSON? Who will possibly consume that properly? Anyway see [Returning binary file from controller in ASP.NET Web API](http://stackoverflow.com/questions/9541351/returning-binary-file-from-controller-in-asp-net-web-api), which shows you how to use `StreamContent` from WebAPI. – CodeCaster Apr 03 '15 at 09:52

0 Answers0