Hi nauna,
Check this example. Now please take its reference and correct your code.
HTML
<asp:DropDownList runat="server" ID="ddlExchangeRates" Width="100px">
</asp:DropDownList>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
KeyValuePair<string, object> rates = GetExchangeRates();
ddlExchangeRates.DataSource = rates.Value;
ddlExchangeRates.DataTextField = "Key";
ddlExchangeRates.DataValueField = "Value";
ddlExchangeRates.DataBind();
ddlExchangeRates.Items.Insert(0, new ListItem("Select", "0"));
string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ipAddress))
{
ipAddress = Request.ServerVariables["REMOTE_ADDR"];
}
string apiKey = "0168838d1e0c43769834c924d06e3366";
SetDropDownListSelection(ddlExchangeRates, ipAddress, apiKey);
}
}
private void SetDropDownListSelection(DropDownList ddl, string ipAddress, string apiKey)
{
string url = string.Format("https://api.ipgeolocation.io/ipgeo?apiKey={0}&ip={1}", apiKey, ipAddress);
string json = (new System.Net.WebClient()).DownloadString(url);
System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
KeyValuePair<string, object> rate = js.Deserialize<Dictionary<string, object>>(json)
.Where(x => x.Key == "currency").ToList().FirstOrDefault();
string code = (rate.Value as Dictionary<string, object>)["code"].ToString();
if (ddl.Items.FindByText(code) != null)
{
ddl.Items.FindByText(code).Selected = true;
}
}
private KeyValuePair<string, object> GetExchangeRates()
{
string json = (new System.Net.WebClient()).DownloadString("https://api.exchangeratesapi.io/latest");
System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
KeyValuePair<string, object> rates = js.Deserialize<Dictionary<string, object>>(json)
.Where(x => x.Key == "rates").FirstOrDefault();
return rates;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim rates As KeyValuePair(Of String, Object) = GetExchangeRates()
ddlExchangeRates.DataSource = rates.Value
ddlExchangeRates.DataTextField = "Key"
ddlExchangeRates.DataValueField = "Value"
ddlExchangeRates.DataBind()
ddlExchangeRates.Items.Insert(0, New ListItem("Select", "0"))
Dim ipAddress As String = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If String.IsNullOrEmpty(ipAddress) Then
ipAddress = Request.ServerVariables("REMOTE_ADDR")
End If
Dim apiKey As String = "0168838d1e0c43769834c924d06e3366"
SetDropDownListSelection(ddlExchangeRates, ipAddress, apiKey)
End If
End Sub
Private Sub SetDropDownListSelection(ByVal ddl As DropDownList, ByVal ipAddress As String, ByVal apiKey As String)
Dim url As String = String.Format("https://api.ipgeolocation.io/ipgeo?apiKey={0}&ip={1}", apiKey, ipAddress)
Dim json As String = (New System.Net.WebClient()).DownloadString(url)
Dim js As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
Dim rate As KeyValuePair(Of String, Object) = js.Deserialize(Of Dictionary(Of String, Object))(json).Where(Function(x) x.Key = "currency").ToList().FirstOrDefault()
Dim code As String = (TryCast(rate.Value, Dictionary(Of String, Object)))("code").ToString()
If ddl.Items.FindByText(code) IsNot Nothing Then
ddl.Items.FindByText(code).Selected = True
End If
End Sub
Private Function GetExchangeRates() As KeyValuePair(Of String, Object)
Dim json As String = (New System.Net.WebClient()).DownloadString("https://api.exchangeratesapi.io/latest")
Dim js As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
Dim rates As KeyValuePair(Of String, Object) = js.Deserialize(Of Dictionary(Of String, Object))(json).Where(Function(x) x.Key = "rates").FirstOrDefault()
Return rates
End Function
Screenshot