Click here to Skip to main content
Click here to Skip to main content

Tagged as

Convert DataTable to String by Extension Method

, 28 Sep 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Useful when calling a C# method from JavaScript by using JSON which returns a DataTable.

This article is useful when a user wants to call a C# method from JavaScript which returns a datatable, and then you need to convert the datatable to string.

public static string ConvertDataTableToString(this DataTable dt)
{
    StringBuilder JsonString = new StringBuilder();
 
    //Exception Handling        
    if (dt != null && dt.Rows.Count > 0)
    {
        JsonString.Append("{ ");
        JsonString.Append("\"Head\":[ ");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            JsonString.Append("{ ");
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                if (j < dt.Columns.Count - 1)
                {
                    JsonString.Append("\"" + 
                      dt.Columns[j].ColumnName.ToString().Trim() + "\":" + 
                      "\"" + dt.Rows[i][j].ToString().Trim() + "\",");
                }
                else if (j == dt.Columns.Count - 1)
                {
                    //string cleaned = original.Replace(@"\""", "");
                    JsonString.Append("\"" + 
                      dt.Columns[j].ColumnName.ToString().Trim() + 
                      "\":" + "\"" + 
                      dt.Rows[i][j].ToString().Trim().CleanInput() + "\"");
                }
            }
            /*end Of String*/
            if (i == dt.Rows.Count - 1)
            {
                JsonString.Append("} ");
            }
            else
            {
                JsonString.Append("}, ");
            }
        }
        JsonString.Append("]}");
        return JsonString.ToString();
    }
    else
    {
        return null;
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ram Balak Sharma
Architect Q3 technology
India India
Ram is .Net Architect by profession and passion having 8 year experience. He has extensive experience on Microsoft Development Platform and is also Microsoft Certified Application Developer (MCAD) for Web.
Reach me at rsharma@stackdotnet.com
 
http://www.stackdotnet.com/
6 Freely avaliable E-Books/
Follow on   Twitter

Comments and Discussions

 
GeneralReason for my vote of 5 i USE THIS CODE TO convert datatable... PinmemberShyamSunderVashista2-Oct-11 23:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 28 Sep 2011
Article Copyright 2011 by Ram Balak Sharma
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid