Click here to Skip to main content
15,668,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have this code in c#

public string DataTableToJSONWithJSONNet(DataTable table)
        string JSONString = string.Empty;
        JSONString = JsonConvert.SerializeObject(table);
        return JSONString;

 protected void btnpoint_Click(object sender, EventArgs e)
        string ConnectionString = Config.GetConnectionString();
        SqlDataAdapter da = new SqlDataAdapter("select latitude,longitude from FrmEducateReport01   ", new SqlConnection(ConnectionString));
        DataTable dt = new DataTable();                                        
        jsondata = DataTableToJSONWithJSONNet(dt);


and in javascript

var arr = JSON.parse('<%=jsondata%>');

but not work and I want to access to items in jsondata

What I have tried:

dont work for example alart(arr); not work
Updated 3-Aug-20 1:04am
Sandeep Mewara 3-Aug-20 2:58am    
Can you explain on what is the issue you are facing when you tried?

UPDATE: Second thought, will share what I think.

Something like below should help:
Client Code:
$("#Customer").change(function () {
            var name = $("[id*='Customer'] :selected").text();
                url: '/Sale/getCustomerId',
                datatype: "json",
                type: "POST",
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify({ CusName: name }),
                async: true,
                processData: false,
                cache: false,

                success: function (Data) {
                    cusid= Data;
                error : function (xhr) {
                    //  alert("error");
                    return null;
                error: function (exception) {
                   // alert('Exeption:' + exception);
                    swal("Ooops", 'Exeption:' + exception, "error");

Server code:
public JsonResult getCustomerId(string CusName)

           // get data

           return Json (result);

Similar question, refer: Ajax call in ASP MVC not working[^]
Share this answer
[no name] 8-Aug-20 12:05pm    
A 5 to balance the uncomented 1
Sandeep Mewara 8-Aug-20 12:34pm    
Thanks! :thumbsup:
"Doesn't work" doesn't give anyone enough information to help you. Do you get an error or do you just not get what you expect? Your code works fine for me, if you want to get meaningful data from the json then "alert(arr)" isn't going to do it, you can parse the data like this

var arr = JSON.parse('<%=jsondata%>');
for (var i = 0; i < arr.length; i++) {
    alert(arr[i].latitude + ", " + arr[i].longitude);

If that's not your problem then you'll have to explain more about what the issue is.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900