I try to connect to MySQL database on my mobile device with Windows Phone 8.1 OS. I am using WCF to create this connection (just for testing):
public class Service1 : IService1
{
public string PobierzHandlowca()
{
String sql = "SELECT nazwa FROM Handlowcy WHERE id_handlowcy=1";
MySqlConnection connection = new MySqlConnection("Server=localhost; Database=msoh; Uid=root; Pwd=;");
try
{
connection.Open();
MySqlCommand command = new MySqlCommand(sql, connection);
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
return (string)reader["nazwa"];
}
}
catch { }
finally
{
connection.Dispose();
}
return null;
}
}
Then in my Windows Phone Silverlight Application I add a service reference. And then I just simply do this:
public partial class MainPage : PhoneApplicationPage
{
ServiceReference1.Service1Client proxy;
public MainPage()
{
InitializeComponent();
proxy = new Service1Client();
proxy.PobierzHandlowcaCompleted += proxy_PobierzHandlowcaCompleted;
}
void proxy_PobierzHandlowcaCompleted(object sender, PobierzHandlowcaCompletedEventArgs e)
{
if (e.Result != null)
{
tbHandlowiec.Text = e.Result; // tbHandlowiec is a TextBox control
}
else
{
tbHandlowiec.Text = "Error!";
}
}
// button
private void btnPobierzHandlowca_Click(object sender, RoutedEventArgs e)
{
proxy.PobierzHandlowcaAsync();
}
}
When I click on this button after a while i get this error - An exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.ni.dll but was not handled in user code
What is wrong with this code? What I'm missing?
public System.IAsyncResult BeginPobierzHandlowca
method went fine (inreference.cs
) and it stucked inpublic string EndPobierzHandlowca(System.IAsyncResult result)
method but I really don't know why. In this line -string _result = ((string)(base.EndInvoke("PobierzHandlowca", _args, result)));
- if it tells you something – XardasLord