Reputation: 429
I may be going at this from the wrong direction. I'm fairly new to .net web services and was looking for a little help.
I have a geolocation webservice I got online and I wanted to bind the results to a listbox or a dataview but am unable too.
I've created a web proxy called net.webservicex.www that points to the webservice at.. http://www.webservicex.net/geoipservice.asmx
Here's my c# code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace web_services
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
net.webservicex.www.GeoIPService myProxy = new net.webservicex.www.GeoIPService(); // proxy object
string ipAddress, result;
ipAddress = txtIpAddress.Text;
result = myProxy.GetGeoIP("64.106.166.130");
lstResults.DataSource = result;
lstResults.DataMember = "IP";
}
}
}
The error I'm recieving is Error
Cannot implicitly convert type 'web_services.net.webservicex.www.GeoIP' to 'string' at line 24
If someone could give me some tips or idea's that would be great.
Thanks! Paul
Upvotes: 1
Views: 2151
Reputation: 16
[WebMethod]
public double ProcitajKursNaDan(DateTime datum, string valuta) {
List<string> podaci = GetLines("valute.txt");
double kurs = 0.0;
for (int i = 0; i < podaci.Count; i++) {
string[] linija = podaci[i].Split('|');
string dat = linija[0];
string val = linija[1];
string vrednost = linija[2];
dat = dat.Trim();
val = val.Trim();
vrednost = vrednost.Trim();
DateTime datIzFajla = DateTime.ParseExact(dat, "d/M/yyyy", null);
double kursIzFajla = Convert.ToDouble(vrednost);
if (DateTime.Compare(datIzFajla, datum) == 0 && val == valuta)
kurs = kursIzFajla;
}
return kurs;
}
[WebMethod]
public bool UpisiKursNaDan(DateTime datum, string valuta, double Kurs) {
string date = datum.ToString("d/M/yyyy");
string linijaZaUpis = date + " | " + valuta + " | " + Kurs.ToString();
bool success = false;
try
{
StreamWriter sw = new StreamWriter(Server.MapPath("podaci/valute.txt"), true);
sw.WriteLine(linijaZaUpis);
sw.Close();
success = true;
}
catch {
success = false;
}
return success;
}
[WebMethod]
public List<string> ProcitajSveValute() {
List<string> linije = GetLines("valute.txt");
List<string> ValuteIzFajla = new List<string>();
for (int i = 0; i < linije.Count; i++) {
string linija = linije[i];
string valuta = linija.Split('|')[1];
valuta = valuta.Trim();
ValuteIzFajla.Add(valuta);
}
List<string> ValuteKraj = ValuteIzFajla.Distinct().ToList();
return ValuteKraj;
// try
// {
// if (!IsPostBack)
// {
// Service1 servis = new Service1();
// List<string> lista = servis.ProcitajSveValute().ToList<string>();
// for (int i = 0; i < lista.Count(); i++)
// {
// DropDownList1.Items.Add(lista[i]);
// }
// }
// }
// catch (Exception ex)
// {
// Response.Write(ex.Message);
// }
// }
// protected void Button1_Click(object sender, EventArgs e)
// {
// try
// {
// Service1 servis = new Service1();
// DateTime datum = Calendar1.SelectedDate;
// string valuta = DropDownList1.SelectedItem.ToString();
// double kurs = Convert.ToDouble(TextBox1.Text);
// bool nesto = servis.UpisiKursNaDan(datum, valuta, kurs);
// }
// catch (Exception ex)
// {
// Response.Write(ex.Message);
// }
// TextBox1.Text = " ";
}
}
}
// try
// {
// if (!IsPostBack)
// {
// Service1 servis = new Service1();
// List<string> lista = servis.ProcitajSveValute().ToList<string>();
// for (int i = 0; i < lista.Count(); i++)
// {
// DropDownList1.Items.Add(lista[i]);
// }
// }
// }
// catch (Exception ex)
// {
// Response.Write(ex.Message);
// }
//}
//protected void Button1_Click(object sender, EventArgs e)
//{
// try
// {
// Service1 servis = new Service1();
// DateTime datum = Calendar1.SelectedDate;
// string valuta = DropDownList1.SelectedItem.ToString();
// double kurs = servis.ProcitajKursNaDan(datum, valuta);
// Label1.Text = kurs.ToString();
// if (kurs == 0)
// {
// Label1.Text = "Ne postoji kursna lista za tu valutu na taj datum!";
// }
// }
// catch (Exception ex)
// {
// Response.Write(ex.Message);
// }
//}
Upvotes: 0
Reputation: 299
I'm a very beginner with the C# and .net but here's how I solved this and I'm sure it'll help beginners like me:
The result object looks like this:
<GeoIP><ReturnCode>1</ReturnCode><IP>11.22.33.44</IP><ReturnCodeDetails>Success</ReturnCodeDetails><CountryName>Germany</CountryName><CountryCode>GER</CountryCode></GeoIP>
So obviously (yeah right...after spitting blood on this...lol), the result cannot be a simple STRING type. So, taking samples from the above solutions I've made it like that:
Default.aspx.cs:
mygeoip.GeoIPService getIP = new mygeoip.GeoIPService();
string myIP = IPTextBox.Text;
GeoIPService.mygeoip.GeoIP resultList = new GeoIPService.mygeoip.GeoIP();
resultList = getIP.GetGeoIP(myIP);
sCountry.Text = resultList.CountryName;
sCountryCode.Text = resultList.CountryCode;
sIP.Text = resultList.IP;
Where - "mygeoip" is my WebService name (instead of "net.webservicex.www") and GeoIPService is my namespace.
Default.aspx:
<asp:TextBox ID="IPTextBox" runat="server"></asp:TextBox> <asp:Button
ID="GetWhois" runat="server" Text="Get Whois" onclick="GetWhois_Click" />
<p><asp:Label ID="sCountry" runat="server" Text="Country: "></asp:Label></p>
<p><asp:Label ID="sCountryCode" runat="server" Text="Country: "></asp:Label></p>
<p><asp:Label ID="sIP" runat="server" Text="Country: "></asp:Label></p>
That's it - I hope I've helped beginners like me :)
Upvotes: 2
Reputation: 3557
You don't need to put it in the result string
lstResults.DataSource = myProxy.GetGeoIP("64.106.166.130");
Since the object returned by your webservice is not enumerable, You could trick it by putting it into an a enumerable Type:
List<web_services.net.webservicex.www.GeoIP> resultList = new List<web_services.net.webservicex.www.GeoIP>();
resultList.Add(myProxy.GetGeoIP("64.106.166.130"));
lstResults.DataSource = resultList;
Upvotes: 4
Reputation: 7595
GeoIP result;
ipAddress = "196.36.153.129";
result = myProxy.GetGeoIP("64.106.166.130");
Upvotes: 1
Reputation: 107606
This line is at fault:
result = myProxy.GetGeoIP("64.106.166.130");
The object returned by that method is not a string, it's a web_services.net.webservicex.www.GeoIP
.
You've declared result
as a string, which doesn't match. Perhaps there is a ToString()
method on the GeoIP
class. If so, you could change your code to:
string ipAddress;
web_services.net.webservicex.www.GeoIP result;
// or maybe: object result;
ipAddress = txtIpAddress.Text;
result = myProxy.GetGeoIP("64.106.166.130");
And depending on what result
looks like, you might just be able to make it your data source.
Upvotes: 2
Reputation: 17014
Looking at the wsdl for this web service it seems that the call to the GetGeoIP method returns an object, not a string.
This is why the compiler is complaining. You could either change the type of result to the object it is expecting or use the var keyword.
Upvotes: 2