Geeks With Blogs

News

Agha Usman Ahmed JILAWATAN

In this post I will explain you, how can we serialize Datatable to JSON. So that, it can easily pass to JavaScript to get the AJAX done.

First of all fill a Datatable with some results.

DataTable dt = new DataTable();
 
SqlConnection objSqlCon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["AdventureWorksConnectionString"].ToString());
objSqlCon.Open();
 
SqlDataAdapter objSqlDa = new SqlDataAdapter("select * from Production.Product", objSqlCon);
 
objSqlDa.Fill(dt);

Now create a String Builder object that will contain the JSON text and JavascriptSerializer which will serialize the output in JSON.

StringBuilder objSb = new StringBuilder();
JavaScriptSerializer objSer = new JavaScriptSerializer();

Now here we are going to iterate each row and column of data table and put all of them in Dictionary

Dictionary<string, object> resultMain = new Dictionary<string, object>();
int index = 0;
 
foreach (DataRow dr in dt.Rows)
{
    Dictionary<string, object> result = new Dictionary<string, object>();
 
    foreach (DataColumn dc in dt.Columns)
    {
        result.Add(dc.ColumnName, dr[dc].ToString());
    }
    resultMain.Add(index.ToString(), result);
    index++;
}

Notice that, I have created a new dictionary object for every row and finally put all of the dictionaries in Another dictionary I.E. resultMain.

In the end, I have simply Serialize the resultMain Dictionary to render JSON.

A complete post of utilizing Datatable  in JavaScript through AJAX is in the process, I will post that soon.

Posted on Saturday, May 16, 2009 4:52 PM Asp.net , Javascript Tricks , c# , AJAX , .net | Back to top


Comments on this post: Datatable to JSON

# re: Datatable to JSON
Requesting Gravatar...
Thanks for this tutorial.
Mike - the order grow taller 4 idiots dude.
Left by senuke pro on Aug 05, 2009 10:18 PM

# re: Datatable to JSON
Requesting Gravatar...
Is it necessary to create a new dictionary object for every row?

Sam - the Grow Taller 4 Idiots 8/25/09 10.16 PM |

Penis Advantage
Left by Grow Taller 4 Idiots on Aug 25, 2009 11:59 AM

# re: Datatable to JSON
Requesting Gravatar...
Nice. Very helpful. Google
Left by Nia on Nov 08, 2009 8:40 AM

# re: Datatable to JSON
Requesting Gravatar...
this is very helpful for my husband last project.my research is done :)
thanks a million
Roxanne
Left by mattress bedroom furniture on Nov 10, 2009 11:19 AM

# re: Datatable to JSON
Requesting Gravatar...
Can we serialize datatable object directly without using dictionary.
Left by rock on Apr 16, 2010 6:13 PM

# re: Datatable to JSON
Requesting Gravatar...
this is very helpful for my husband last project.my research is done :)
thanks a million
Left by etui ipad 2 on Jun 28, 2011 6:26 AM

# re: Datatable to JSON
Requesting Gravatar...
Notice that, I have created a new dictionary object for every row and finally put all of the dictionaries in Another dictionary I.E. resultMain.

In the end, I have simply Serialize the resultMain Dictionary to render JSON.
Left by etui samsung galaxy s2 on Jun 28, 2011 6:26 AM

# re: Datatable to JSON
Requesting Gravatar...
No need to do it manual(this long logic)..just put [ScriptMethod(ResponseFormat=ResponseFormat.Json)]
for the server side method which returns dataTable,framework will change it to json format automatically.

I really dont know that Agha Usman have some blog(just knew it on FB from his status).I just saw it and then sudden notice this topic with datatable to Json..can't stop myself to comment out for this thread.
Left by Ahmed Kamal on Aug 06, 2011 8:39 AM

Your comment:
 (will show your gravatar)


Copyright © Agha Usman Ahmed | Powered by: GeeksWithBlogs.net