Hi atulspider,
I have created sample code which full-fill your requirement. So please refer the below code and modify as per your requirement.
HTML
<div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/json2/20130526/json2.min.js"></script>
<script type="text/javascript">
$(function () {
var obj = {};
$.get("http://freegeoip.net/json/", function (response) {
obj.location = response;
saveDataInData(obj);
}, "jsonp");
});
function saveDataInData(obj) {
$.ajax({
type: "POST",
url: "Default.aspx/SaveIdAddress",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
alert(r.d);
}
});
return false;
}
</script>
</div>
C#
[WebMethod]
public static void SaveIdAddress(Location location)
{
string Constring = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
using (SqlConnection con = new SqlConnection(Constring))
{
using (SqlCommand cmd = new SqlCommand("Insert into UserDetails values(@ip,@country_code,@country_name,@region_code,@region_name,@city,@zip_code,@time_zone,@latitude,@longitude,@metro_code)"))
{
cmd.Parameters.AddWithValue("@ip", location.ip);
cmd.Parameters.AddWithValue("@country_code", location.country_code);
cmd.Parameters.AddWithValue("@country_name", location.country_name);
cmd.Parameters.AddWithValue("@region_code", location.region_code);
cmd.Parameters.AddWithValue("@region_name", location.region_name);
cmd.Parameters.AddWithValue("@city", location.city);
cmd.Parameters.AddWithValue("@zip_code", location.zip_code);
cmd.Parameters.AddWithValue("@time_zone", location.time_zone);
cmd.Parameters.AddWithValue("@latitude ", location.latitude);
cmd.Parameters.AddWithValue("@longitude", location.longitude);
cmd.Parameters.AddWithValue("@metro_code ", location.metro_code);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
public class Location
{
public string ip { get; set; }
public string country_code { get; set; }
public string country_name { get; set; }
public string region_code { get; set; }
public string region_name { get; set; }
public string city { get; set; }
public string zip_code { get; set; }
public string time_zone { get; set; }
public string latitude { get; set; }
public string longitude { get; set; }
public string metro_code { get; set; }
}
Vb.Net
<WebMethod> _
Public Shared Sub SaveIdAddress(location As Location)
Dim Constring As String = ConfigurationManager.ConnectionStrings("Constring").ConnectionString
Using con As New SqlConnection(Constring)
Using cmd As New SqlCommand("Insert into UserDetails values(@ip,@country_code,@country_name,@region_code,@region_name,@city,@zip_code,@time_zone,@latitude,@longitude,@metro_code)")
cmd.Parameters.AddWithValue("@ip", location.ip)
cmd.Parameters.AddWithValue("@country_code", location.country_code)
cmd.Parameters.AddWithValue("@country_name", location.country_name)
cmd.Parameters.AddWithValue("@region_code", location.region_code)
cmd.Parameters.AddWithValue("@region_name", location.region_name)
cmd.Parameters.AddWithValue("@city", location.city)
cmd.Parameters.AddWithValue("@zip_code", location.zip_code)
cmd.Parameters.AddWithValue("@time_zone", location.time_zone)
cmd.Parameters.AddWithValue("@latitude ", location.latitude)
cmd.Parameters.AddWithValue("@longitude", location.longitude)
cmd.Parameters.AddWithValue("@metro_code ", location.metro_code)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub
Public Class Location
Public Property ip() As String
Get
Return m_ip
End Get
Set
m_ip = Value
End Set
End Property
Private m_ip As String
Public Property country_code() As String
Get
Return m_country_code
End Get
Set
m_country_code = Value
End Set
End Property
Private m_country_code As String
Public Property country_name() As String
Get
Return m_country_name
End Get
Set
m_country_name = Value
End Set
End Property
Private m_country_name As String
Public Property region_code() As String
Get
Return m_region_code
End Get
Set
m_region_code = Value
End Set
End Property
Private m_region_code As String
Public Property region_name() As String
Get
Return m_region_name
End Get
Set
m_region_name = Value
End Set
End Property
Private m_region_name As String
Public Property city() As String
Get
Return m_city
End Get
Set
m_city = Value
End Set
End Property
Private m_city As String
Public Property zip_code() As String
Get
Return m_zip_code
End Get
Set
m_zip_code = Value
End Set
End Property
Private m_zip_code As String
Public Property time_zone() As String
Get
Return m_time_zone
End Get
Set
m_time_zone = Value
End Set
End Property
Private m_time_zone As String
Public Property latitude() As String
Get
Return m_latitude
End Get
Set
m_latitude = Value
End Set
End Property
Private m_latitude As String
Public Property longitude() As String
Get
Return m_longitude
End Get
Set
m_longitude = Value
End Set
End Property
Private m_longitude As String
Public Property metro_code() As String
Get
Return m_metro_code
End Get
Set
m_metro_code = Value
End Set
End Property
Private m_metro_code As String
End Class