2
votes

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?

1
Never a good idea to have an empty catch block. If something fails on the server you have no way to know what was wrong.Steve
Define a while, it could be a time out if it's a long enough while, which means you're not actually ever hitting the service itself.Mr. B
@Mr.B - It's about 5 seconds.XardasLord
Could be a timeout. The point is that more detail is needed to figure out what went wrong. Have you tried using the debugger to step into the WCF call?Mr. B
@Mr.B - Yes I have tried. public System.IAsyncResult BeginPobierzHandlowca method went fine (in reference.cs) and it stucked in public 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 somethingXardasLord

1 Answers

1
votes

Ok I've found a solution.

I have created a WCF service on a Local IIS:

enter image description here

A also had to install some .NET, HTTP and IIS features in Turn Windows features on or off (Control Panel -> Program features).

Now I can get data in my Windows Phone from a database which is located on my computer.