I have a SQL Table with values like:
Main_group Sub_group CstCmpCode
COMBO SET DD-101 AH01
COMBO SET DD-102 AH01
I need to create nested json string like:
{
"CstCmpCode": "AH01",
"Main_Group": "COMBO SET",
"sub_group": [
{
"Sub_Group": "DD-101",
},
{
"Sub_Group": "DD-102",
}
]
}
My code as below for converting datatable to nested json string :
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TallyWeb"].ToString());
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select distinct Main_group, Sub_group, CstCmpCode from TlyStkSumm where CstCmpCode = @CstCmpCode";
cmd.Parameters.AddWithValue("@CstCmpCode", CstCmpCode);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.SelectCommand.Connection = con;
da.Fill(dt);
con.Close();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row = null;
foreach (DataRow rs in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, rs[col]);
}
rows.Add(row);
}
Pls. check in the above what i am going to wrong.
Thanks.
Yogesh.Sharma