Question

i am building a simple website which takes the address of the user,converts the address into corresponding latitude and longitude and stores it in the database,when ever the user presses the button. my back end code is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class registration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConn"].ConnectionString);
            conn.Open();
            string checkuser = "select count(*) from userdata where username='" + TextBoxUN.Text + "'";
            SqlCommand com = new SqlCommand(checkuser, conn);
            int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
            if (temp == 1)
            {
                Response.Write("username already exists");

            }

            conn.Close();

        } 

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        try
        {
            Guid newGUID = Guid.NewGuid();
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConn"].ConnectionString);
            conn.Open();
            string insertquery = "insert into userdata(Id,username,email,password,country) values(@ID,@uname,@email,@password,@country)";
            SqlCommand com = new SqlCommand(insertquery, conn);
            com.Parameters.AddWithValue("@ID", newGUID.ToString());
            com.Parameters.AddWithValue("@uname", TextBoxUN.Text);
            com.Parameters.AddWithValue("@email", TextBoxEM.Text);
            com.Parameters.AddWithValue("@password", TextBoxPW.Text);
            com.Parameters.AddWithValue("@country", DropDownListCN.SelectedItem.ToString());
            com.ExecuteNonQuery();
            Response.Redirect("Manager.aspx");
            Response.Write("registration successful");
            conn.Close();
        }
        catch (Exception ex)
        {
            Response.Write("Error:" + ex.ToString());
        }

    }

}

this code just saves the actual address of the user not the longitude and latitude.please can any one tell me how should i have to modify the code so that i can convert the address into longitude and latitude.i searched many sites.i got answer that i have to use google apis .but how should i have to include google apis in my code?please help me.

Was it helpful?

Solution

Yuo can use the Google Geocoding API directly from your code-behind in c#. See this library: https://code.google.com/p/geocoding-net/

Also see similar questions here and here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top