I am working on a project and am struggling after a lot of googling to find the best way to do this without using third party libraries. I would love some advise on this as its critical do getting my program correct.
My program is a C# webforms application with a Html/javascript front end. The C# side runs Sql queries on a Sql compact database and is meant send the data over to the javascript to be taken a part and loaded 1 row at a time.
I have a table of shops which has ID, Name,Address and Category fields.
DataSet ds = new DataSet();
adapter.Fill(ds);
List<Shop> shops = new List<Shop>();
foreach (DataRow row in ds.Tables[0].Rows)
{
string ID = row["ShopID"].ToString();
string Name = row["ShopName"].ToString();
string Address = row["ShopAddress"].ToString();
string Category = row["Category"].ToString();
shops.Add(new Shop(ID, Name, Address, Category));
}
JavaScriptSerializer ser = new JavaScriptSerializer();
hfData.Value = ser.Serialize(shops);
This part seems to work perfectly, the data is loaded correctly and put into a hiddenfield called hfData.
While on the front end I have this line which is supposed to read the hiddenfield
var HiddenValue = document.getElementById("<%=hfData.Value%>").value;
alert(HiddenValue);
The HiddenValue alert comes back in this format.
"[{"ShopID":"1","ShopName":"PubName1","ShopAddress":"1 Example street, exampleTown, postcode","ShopCategory":"English"},{"ShopID":"2","ShopName":"PubName2","ShopAddress":"2 Example street, exampleTown, postcode","ShopCategory":"Irish"}]"
How do I then take that data and formatted so I can output the ShopName etc for the first shop separately so it can be displayed. The format seems to group a row in {} and the "FieldName" : "Data" although I don't have the experience to manipulate this so need advise.
The Text is meant to be converted to variables inside a for loop and displayed in a readable format. The address is meant to be separate string so I can send it to the google maps API to be displayed so its important to split them up
Assuming this is the best way I can pass data across, if there is a better way please tell me, although I would like to know the solution for this problem as well so It doesn't occur again