I'm just looking for a couple of pointers to get me on the right path. I have 2 SQL queries, one returning a list of customers and one returning the list of orders that customer has made. Working in c# how am I able to populate a class that then can be serialized into json.
I've tried multiple different way and this is now the closest I've got for my class...
public class jsonOrder
{
public string order_no { get; set; }
public string customer { get; set; }
public List<orderitem> items { get; set; }
public decimal grandtotal { get; set; }
public jsonOrder()
{
this.items = new List<orderitem>();
}
}
public class orderitem
{
public string itemcode{ get; set; }
public int quantity { get; set; }
public decimal amount { get; set; }
}
Using this I can get my json to look like this...
[
{
"order_no": "12345",
"customer": "12",
"items": [],
"grand_total": 6.0000,
}
{
...another order...
}
]
How can I get the items to list the times in the order?
For example
{
"order_no": "12345",
"customer": "12",
"items": [
{"itemcode":"a","quantity":1,"amount":12.34}
{"itemcode":"b","quantity":2,"amount":6.12}
],
"grand_total": 24.5800
}
at the moment my code is
List<readOrder> orderhistory = new List<Models.readOrder>(ordHead.Rows.Count);
if (ordHead.Rows.Count > 0)
{
foreach (DataRow orders in ordHead.Rows)
{
orderhistory.Add(new readOrder(orders));
}
}
but this is only bringing back header details.
I currently get my SQL from the following, but this I am flexible on...
_con = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
// Get all orders
DataTable ordHead = new DataTable();
var queryHead = "Select * from ORDERHEADER where customer = " + customer;
_Header_adapt = new SqlDataAdapter
{
SelectCommand = new SqlCommand(queryHead,_con)
};
_Header_adapt.Fill(ordHead);
//Get items within orders
DataTable ordDetail = new DataTable();
var queryDetail = "Select * from ORDERHISTORY where customer = " + customer;
_adapt = new SqlDataAdapter
{
SelectCommand = new SqlCommand(queryDetail, _con)
};
_adapt.Fill(ordDetail);
```