Namespace
using System.Text;
code :
/// <summary>
/// Datatable to JSON
/// <CreatedBy>Ashish Srivastava</CreatedBy>
/// <Date>12 Dec 2016</Date>
/// </summary>
/// </summary>
/// <param name="table"></param>
/// <returns></returns>
public string DataTableToJSONWithStringBuilder(DataTable table)
{
var JSONString = new StringBuilder();
if (table.Rows.Count > 0)
{
JSONString.Append("{ \"status\":\"1\" ,\"result\": [");
for (int i = 0; i < table.Rows.Count; i++)
{
JSONString.Append("{");
for (int j = 0; j < table.Columns.Count; j++)
{
if (j < table.Columns.Count - 1)
{
JSONString.Append("\"" + table.Columns[j].ColumnName.ToString() + "\":" + "\"" + table.Rows[i][j].ToString() + "\",");
}
else if (j == table.Columns.Count - 1)
{
JSONString.Append("\"" + table.Columns[j].ColumnName.ToString() + "\":" + "\"" + table.Rows[i][j].ToString() + "\"");
}
}
if (i == table.Rows.Count - 1)
{
JSONString.Append("}");
}
else
{
JSONString.Append("},");
}
}
}
else
{
JSONString.Append("{ \"status\":\"0\" ,\"result\": [");
JSONString.Append("{\"value\":" + "\"No record found\"}");
}
JSONString.Append("]}");
return JSONString.ToString();
}
using System.Text;
code :
/// <summary>
/// Datatable to JSON
/// <CreatedBy>Ashish Srivastava</CreatedBy>
/// <Date>12 Dec 2016</Date>
/// </summary>
/// </summary>
/// <param name="table"></param>
/// <returns></returns>
public string DataTableToJSONWithStringBuilder(DataTable table)
{
var JSONString = new StringBuilder();
if (table.Rows.Count > 0)
{
JSONString.Append("{ \"status\":\"1\" ,\"result\": [");
for (int i = 0; i < table.Rows.Count; i++)
{
JSONString.Append("{");
for (int j = 0; j < table.Columns.Count; j++)
{
if (j < table.Columns.Count - 1)
{
JSONString.Append("\"" + table.Columns[j].ColumnName.ToString() + "\":" + "\"" + table.Rows[i][j].ToString() + "\",");
}
else if (j == table.Columns.Count - 1)
{
JSONString.Append("\"" + table.Columns[j].ColumnName.ToString() + "\":" + "\"" + table.Rows[i][j].ToString() + "\"");
}
}
if (i == table.Rows.Count - 1)
{
JSONString.Append("}");
}
else
{
JSONString.Append("},");
}
}
}
else
{
JSONString.Append("{ \"status\":\"0\" ,\"result\": [");
JSONString.Append("{\"value\":" + "\"No record found\"}");
}
JSONString.Append("]}");
return JSONString.ToString();
}
it is most valuable for me sir G
ReplyDelete