如何在点击事件中插入数据库中的纬度和经度?

时间:2015-06-01 08:51:25

标签: javascript c# asp.net

使用ASP。我试图将用户的位置(纬度,经度)存储到数据库。这就出现了问题:

我希望通过点击按钮得到谷歌地图的经度和纬度值我在数据库中得到0值。我的数据库ha两个经度和纬度都有十进制数据类型。

我也得到了这个错误

  

cmd1.ExecuteNonQuery()中的未处理异常类型系统数据-sqlclient-sqlexception-occured-in-system-data-dll;线

以下是我第一次尝试的示例代码:

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

以下是.aspx代码隐藏文件。我们在按钮的Click事件期间保存数据库的经度和纬度:

  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 into 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();    
    }

1 个答案:

答案 0 :(得分:1)

您可能无法在隐藏字段中获取值,因此null值将转到导致错误的数据库。并且您没有在隐藏字段中获取值,因为您已经创建了这些runat="server",因此ID已更改,您在Id的javascript中设置了值,但Id已设置了ClientIDMode改变。因此,您需要做的是将隐藏字段的static属性设置为<asp:HiddenField ID="lat" ClientIDMode="Static" runat="server" /> <asp:HiddenField ID="lng" ClientIDMode="Static" runat="server" /> ,如此

    protected override void LoadViewState(object savedState)
    {
        object[] allStates = (object[])savedState;
        if (allStates[0] != null)
            base.LoadViewState(allStates[0]);
        if (allStates[1] != null)
            Page.Title = (string)allStates[1];
    }

    protected override object SaveViewState()
    {
        object[] allStates = new object[2];
        object baseState = base.SaveViewState();
        string pageTitle = Page.Title;
        allStates[0] = baseState;
        allStates[1] = pageTitle;
        return allStates;
    }