Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, I've got a script on my server which will return a simple string; I am trying to use Silverlight C# to return a string from my database so I can bind it into my site.

I've got PHP and Silverlight working together; I just need a way so I can call a script from a website and have the returned value get stored in a C# string.

Anyway I can do this?

Thanks
Posted
Updated 6-Aug-13 5:28am
v2

1 solution

I have face the exact problem like you.
Using PHP script to connect SilverLight to MySQL is my first approach.

But later, I had discovered a more convenient/easier way, by using C# Web Services.

I have written an article explaining the steps in details with illustrations.
>> Connecting MySQL From SilverLight With Web Services[^]

Simple explanation:
1. You need to create a C# web services.
2. Configure the web service access permission
(by default, web service only allow to be access by localhost)
3. At SilverLight application, add a service reference of the WebService that you have created.

There, at your SilverLight, you access MySQL just as executing a method.

Example:

code at SilverLight
C#
public partial class MainPage : UserControl
{
    ServiceReference1.WebService1SoapClient myService;
 
    public MainPage()
    {
        InitializeComponent();
        myService = new ServiceReference1.WebService1SoapClient();
        myService.ExecuteScalarCompleted += myService_ExecuteScalarCompleted;
    }
  
    void myService_ExecuteScalarCompleted(object sender, ServiceReference1.ExecuteScalarCompletedEventArgs e)
    {
        MessageBox.Show(e.Result);
    }
 
    private void button_ExecuteScalar_Click(object sender, RoutedEventArgs e)
    {
        myService.ExecuteScalarAsync(textBox1.Text);
    }
}


code at Web Service
C#
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using MySql.Data.MySqlClient;
using System.Data;

namespace MyWebService
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class WebService1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string ExecuteScalar(string sql)
        {
            string result = "";
            string constr = "server=localhost;user=root;pwd=1234;database=member;";
            using (MySqlConnection conn = new MySqlConnection(constr))
            {
                MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = conn;
                conn.Open();

                cmd.CommandText = sql;
                result = cmd.ExecuteScalar() + "";

                conn.Close();
            }

            return result;
        }
    }
}


The above gives you an idea how SilverLight communicate MySQL through WebServices.

There are some details that I didn't mentioned above.

You have to search google on below details:
1. How to create a C# Web Service.
2. How to add a Web Service reference at SilverLight

From the above example, you can see that SilverLight is accessing the Web Service with this:
C#
ServiceReference1.WebService1SoapClient myService;

ServiceReference1.WebService1SoapClient is an class automatically generated by Visual Studio. We only need to locate the Web Services location (example: http://www.myweb.com/WebServices.asmx), then data binding will be automatically performed.

Web Services has a file extension of ".asmx".
 
Share this answer
 
v6
Comments
Kieran Partridge 7-Aug-13 7:50am    
Hello! Thanks, that's really helping me out, managed to get data from the database!

My only problem now is that when I use this code:
var Service = new Service.WebServiceSoapClient();
string membername = Service.GetData("select name from member where id = 1;");

It isn't able to find the method in the web service; I've referenced it but it's still not able to find it, do you know a way I can call the method?

Thanks.
Kieran Partridge 7-Aug-13 8:03am    
Never mind, I managed to find a way to do it by creating an event :

void Service_GetDataCompleted(object sender, ServiceSythnet.GetDataCompletedEventArgs e)
{
MessageBox.Show(e.Result);
}
adriancs 7-Aug-13 9:50am    
Oppss, sorry I have provided one wrong information. xD
You are right. It's glad that you have figured out.
The example provided in my answer is for a ASP.NET page accessing the web services. XD
For SilverLight, the result is returned in event GetDataCompleted. ^^
But anyway, I think you have learnt something new by yourself, which is a good for you. Congratulation.
adriancs 8-Aug-13 1:45am    
I have written an article of connecting MySQL from SilverLight by using web service.
http://www.codeproject.com/Articles/633789/Connecting-MySQL-From-SilverLight-With-Web-Service
adriancs 8-Aug-13 1:52am    
I have update my answer above to provide the correct information.

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