Click here to Skip to main content
14,638,458 members
Rate this:
Please Sign up or sign in to vote.
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.
Rate this:
Please Sign up or sign in to vote.

Solution 1

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[^]
0x01AA 8-Aug-20 12:05pm
A 5 to balance the uncomented 1
Sandeep Mewara 8-Aug-20 12:34pm
Thanks! :thumbsup:
Rate this:
Please Sign up or sign in to vote.

Solution 2

"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.

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100