First you need get a data from Table Twice . Like my Example
i'm use struct do a json object base And use two SqlDataSource get a Table1 & Table2 Data
Table2 Data will filter by Table1 .
Last , I save Table2 Data in my struct List & Ditionary
you can refer ConvertDataTabletoJsonString
public struct PersonScore
{
public string ID { get; set; }
public string Name { get; set; }
public List<Dictionary<string, object>> Score { get; set; }
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable Table1Data = (SqlDataSource1.Select(new DataSourceSelectArguments()) as DataView).Table;
SqlDataSource2.SelectParameters["SutdentID"].DefaultValue = Table1Data.Rows[0]["ID"].ToString();
DataView Table2Data = SqlDataSource2.Select(new DataSourceSelectArguments()) as DataView;
DataTable DT = Table2Data.Table;
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (DataRow DR in DT.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn Col in DT.Columns)
{
row.Add(Col.ColumnName, DR[Col]);
}
rows.Add(row);
}
PersonScore NewPersonScore = new PersonScore
{
ID = Table1Data.Rows[0]["ID"].ToString(),
Name = Table1Data.Rows[0]["StudentName"].ToString(),
Score=rows
};
string jsontxt = new JavaScriptSerializer().Serialize(NewPersonScore);
Response.Write(jsontxt);
}