Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I get data through json. I would like to collect those datas in strings and then insert into a
sql table. I used the following code which works fine but I would like to know how to collect them in string for insertion into tables. For instance I would like to collect the countryname,regionname and cityname to the strings. My code:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Net;
using System.Web.Script.Serialization;
using System.Web.UI.WebControls;
public partial class getipaddress_test : System.Web.UI.Page
{
    public class Location
    {
        public string IPAddress { get; set; }
        public string CountryName { get; set; }
        public string CountryCode { get; set; }
        public string CityName { get; set; }
        public string RegionName { get; set; }
        public string ZipCode { get; set; }
        public string Latitude { get; set; }
        public string Longitude { get; set; }
        public string TimeZone { get; set; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (string.IsNullOrEmpty(ipAddress))
        {
            ipAddress = Request.ServerVariables["REMOTE_ADDR"];
        }

        string APIKey = "<your api="" key="">";
        string url = string.Format("http://api.ipinfodb.com/v3/ip-city/?key=7d0f9f121cca47d87c9ad6da04748bcd04a43387b21960902b6c9b6d4a84b34z&ip={1}&format=json", APIKey, ipAddress);
        using (WebClient client = new WebClient())
        {
            string json = client.DownloadString(url);
            //string v_cn = ("cityname");
            Location location = new JavaScriptSerializer().Deserialize<location>(json);
            List<location> locations = new List<location>();
            locations.Add(location);
            gvLocation.DataSource = locations;
            gvLocation.DataBind();
        }
    }
}


Thanks.
Posted
Updated 11-Aug-13 18:53pm
v2
Comments
lukeer 12-Aug-13 1:30am    
I don't see the problem. What exactly does not work the way you want it to?
S.Rajendran from Coimbatore 12-Aug-13 7:11am    
Well. I want to insert the string 'cityname' into a table.

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900