4

I am using ASP.NET Webforms and I have the following function which returns multiple lat and lon values:

directionsService.route(request, function (response, status) {
 if (status == google.maps.DirectionsStatus.OK) {
 directionsDisplay.setDirections(response);
 if (response.routes && response.routes.length > 0) {
 var routes = response.routes;
 for (var j = 0; j < routes.length; j++) {
 var points = routes[j].overview_path;
 var ul = document.getElementById("vertex");
 for (var i = 0; i < points.length; i++) {
 var li = document.createElement('li');
 li.innerHTML = getLiText(points[i]);
 ul.appendChild(li);
 }
 }
 }
 }
});
function getLiText(point) {
 var lat = point.lat(),
 lng = point.lng();
 $.ajax({
 type: "POST",
 contentType: "application/json; charset=utf-8",
 url: "Default.aspx/gpsdata",
 data: { 'lat': point.lat() },
 dataType: "json",
 success: function (data) {
 // response(data.d);
 alert(point);
 },
 error: function (result) {
 alert("Error");
 }
 });
 return "lat: " + lat + " lng: " + lng;
}
<div id="vertex-container">
 <label>Points</label>
 <ul id="vertex">
 </ul>
</div>

So not it will give me output as below:

enter image description here

Now I want to send these all data to c# method. For that I have add below method:

[WebMethod]
public static string gpsdata(string lat)
{
 List<string> result = new List<string>();
 SqlConnection conn = new SqlConnection();
 conn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["keyConn"].ToString();
 using (SqlCommand cmd = new SqlCommand("Insert into latdata values(username)", conn))
 {
 //my code
 }
}

But it is not working..

From the javascript ajax method it will not pass the any values to the c# web method and I am not able to insert that all values to my database.

So how can I do this task?

Tolga Evcimen
7,39011 gold badges62 silver badges97 bronze badges
asked Dec 25, 2014 at 9:44

2 Answers 2

2

Try this:-

 data: JSON.stringify({ 'lat': point.lat() })

Use JSON.Stringfy function while passing your json data.

answered Dec 25, 2014 at 9:47
Sign up to request clarification or add additional context in comments.

1 Comment

Have u placed a break-point in code behind file? It should hit the server method.
0

I notice an error in your webmethod

public static string gpsdata(string lat) 

should be

public static string gpsdata(string lat, string lng)
//jQuery
var geoLoc = { 
 lat: point.lat(),
 lng: point.lat()
}
var parameter = JSON.stringify(geoLoc); 
$.ajax({
 type: "POST",
 url: "Default.aspx/gpsdata",
 data: parameter,
 dataType: "json",
 contentType: "application/json",
 async: true,
 beforeSend: function () { },
 success: function (response, status, xhr) {
 debugger;
 },
 error: function (xhr, status, error) {
 debugger;
 }
});
Tolga Evcimen
7,39011 gold badges62 silver badges97 bronze badges
answered Dec 25, 2014 at 9:51

3 Comments

Well I should have been specific lol try this url: "Default.aspx/gpsdata",
@Tolga What is the proper way of formatting? I am using 4 spaces.
4 spaces is fine. Go with it ;)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.