javascript - How to insert latitude and longitude in database from click event? -
using asp. trying store user's location (latitude, longitude) database. presents problem:
i want values of longitude , latitude of google map clicking button got 0 values in database.. database ha 2 filed longitude , latitude has decimal datatype.
and getting error
an-unhandled-exception-of-type-system-data-sqlclient-sqlexception-occured-in-system-data-dll in cmd1.executenonquery(); line
here's sample code of first attempt:
<html xmlns="http://www.w3.org/1999/xhtml"> <head id="head1" runat="server"> <title></title> </head> <body> <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> <script type="text/javascript"> var long = 0; var lat = 0; window.onload = function () { var mapoptions = { center: new google.maps.latlng(18.9300, 72.8200), zoom: 14, maptypeid: google.maps.maptypeid.roadmap }; var infowindow = new google.maps.infowindow(); var latlngbounds = new google.maps.latlngbounds(); var map = new google.maps.map(document.getelementbyid("dvmap"), mapoptions); google.maps.event.addlistener(map, 'click', function (e) { long = e.latlng.lng(); lat = e.latlng.lat(); document.getelementbyid("lat").value = lat; document.getelementbyid("lng").value = long; alert("latitude: " + lat + "\r\nlongitude: " + long); }); } </script> <form id="myform" runat="server"> <div id="dvmap" style="width: 300px; height: 300px"> </div> <asp:hiddenfield id="lat" runat="server" /> <asp:hiddenfield id="lng" runat="server" /> <asp:button id="button1" runat="server" text="button" onclick="button1_click" /> </form> </body>
and below .aspx code-behind file. save longitude , latitude database during click event of button:
protected void button1_click(object sender, eventargs e) { decimal latitude = convert.todecimal(lat.value); decimal longitude = convert.todecimal(lng.value); string constr = configurationmanager.connectionstrings["constr"].connectionstring; sqlconnection con = new sqlconnection(constr); string query1 = "insert courses(longi,lati) values (@lati, @longi)"; sqlcommand cmd1 = new sqlcommand(query1, con); cmd1.parameters.addwithvalue("@lati", latitude); cmd1.parameters.addwithvalue("@longi", longitude); con.open(); cmd1.executenonquery(); con.close(); }
you may not getting value in hidden fields null
values going database causing error. , not getting values in hidden fields because have made these runat="server"
there id
changed , setting values in javascript id
id
have changed. need set clientidmode
property of hidden fields static
this
<asp:hiddenfield id="lat" clientidmode="static" runat="server" /> <asp:hiddenfield id="lng" clientidmode="static" runat="server" />
Comments
Post a Comment