13 Replies - 8849 Views - Last Post: 13 May 2010 - 05:14 AM

#1 Guest_megan111*


Reputation:

connecting to a database via silverlight & wcf

Posted 10 May 2010 - 06:20 AM

Hi, I've been looking at a number of tutorials on how to connect/display data from a database using silverlight and wcf.

I've been following this one http://www.thesilver...IAServices.html but I got very confused at the end from 'Read data from client side'

Can anyone tell me what he means, or know of a better tutorial?

Is This A Good Question/Topic? 0

Replies To: connecting to a database via silverlight & wcf

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: connecting to a database via silverlight & wcf

Posted 11 May 2010 - 11:27 AM

Personally, I think the RIA services tutorials aren't very good at explaining what is going on. I just use a normal WCF service to read from a database. I can do a quick tutorial.
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: connecting to a database via silverlight & wcf

Posted 11 May 2010 - 01:51 PM

I submitted a tutorial.

http://www.dreaminco...e-database-wcf/

The link may not work until the tutorial is approved.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_megan111*


Reputation:

Re: connecting to a database via silverlight & wcf

Posted 12 May 2010 - 03:43 AM

just saw this, thank you for taking the time to post a tutorial. i will check it out now :)
Was This Post Helpful? 0

#5 Guest_megan111*


Reputation:

Re: connecting to a database via silverlight & wcf

Posted 12 May 2010 - 03:57 AM

sorry, it is not clear where the event handler main page loaded goes?
Was This Post Helpful? 0

#6 Guest_megan_111*


Reputation:

Re: connecting to a database via silverlight & wcf

Posted 12 May 2010 - 04:12 AM

I have followed all your steps and have the same filenames, but I get an error for 'DatabaseServiceClient'

Error
Error	1	The type or namespace name 'DatabaseServiceClient' could not be found (are you missing a using directive or an assembly reference?)



Is there anything I could be missing?
Was This Post Helpful? 0

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: connecting to a database via silverlight & wcf

Posted 12 May 2010 - 04:48 AM

View Postmegan111, on 12 May 2010 - 05:57 AM, said:

sorry, it is not clear where the event handler main page loaded goes?


sorry. It goes into the "MainPage.xaml.cs" file.

namespace SilverlightWCFTutorial
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            // The DatabaseServiceClient class was automatically created for us.
            // The autogeneration takes the name of the service, then appends "Client" to it,
            //   giving us the "DatabaseServiceClient" class.
            DatabaseServiceClient client = new DatabaseServiceClient();

            // Since requests in Silverlight are asynchronous, we have a Completed method
            //   that will be fired when the request has been completed.
            client.GetNamesCompleted += delegate(object s, GetNamesCompletedEventArgs es)
            {
                // when the request has been completed, we want to bind the data to the grid.
                // the Result property of the EventArgs contains the returned data.
                // ObservableCollection is a common collection that is used when databinding to
                //    a DataGrid.
                ObservableCollection<string> myList = es.Result;

                dataGridPerson.DataContext = myList;
            };

            client.GetNamesAsync();
        }
    }
}



View Postmegan_111, on 12 May 2010 - 06:12 AM, said:

I have followed all your steps and have the same filenames, but I get an error for 'DatabaseServiceClient'

Error
Error	1	The type or namespace name 'DatabaseServiceClient' could not be found (are you missing a using directive or an assembly reference?)



Is there anything I could be missing?


Did you name everything the same as I did? The "Client" class is autogenerated using the name of the service. So if you named the service "SomeOtherName.svc", then your "client" class name would be "SomeOtherNameClient".
Was This Post Helpful? 0
  • +
  • -

#8 Guest_megan111*


Reputation:

Re: connecting to a database via silverlight & wcf

Posted 12 May 2010 - 05:41 AM

Hi :)

Yes I have restarted your tutorial several times and keep having that same error, along with

Quote

Error 3 The type or namespace name 'GetNamesCompletedEventArgs' could not be found (are you missing a using directive or an assembly reference?)


and

Quote

Error 4 The type or namespace name 'ObservableCollection' could not be found (are you missing a using directive or an assembly reference?


I am also running Silverlight 4, on Windows XP
Was This Post Helpful? 0

#9 Guest_megan_111*


Reputation:

Re: connecting to a database via silverlight & wcf

Posted 12 May 2010 - 06:00 AM

Solved! I had to right click and 'resolve' these names. Thanks for your help, it works :)
Was This Post Helpful? 0

#10 Guest_megan_111*


Reputation:

Re: connecting to a database via silverlight & wcf

Posted 13 May 2010 - 12:58 AM

Now that I have this working, I just have some questions.

Is it possible to connect to several database/tables with the one service ref or must you create one for each?

I am now experimenting with a function where if a user ticks a checkbox, it calls a particular database table and displays data, and if the user ticks another check box, it calls another particular database table and displays data.

  void MainPage_Loaded(object sender, RoutedEventArgs e)
        {

            Service1Client coClient = new Service1Client();
            Service1Client apClient = new Service1Client();

            if (checkBoxCo.IsChecked != null)
            {
                // Since requests in Silverlight are asynchronous, we have a Completed method
                //   that will be fired when the request has been completed.
                coClient.CoCompleted += delegate(object s, CoCompletedEventArgs es)
                {
                    // when the request has been completed, we want to bind the data to the grid.
                    // the Result property of the EventArgs contains the returned data.
                    // ObservableCollection is a common collection that is used when databinding to
                    //    a DataGrid.
                    ObservableCollection<string> myList = es.Result;

                    dataGrid1.DataContext = myList;
                };

                coClient.CorkAsync();
            }

            if (checkBoxAp.IsChecked != null)
            {
                // Since requests in Silverlight are asynchronous, we have a Completed method
                //   that wil be fired when the request has been completed.
                apClient.ApCompleted += delegate(object s, ApCompletedEventArgs es)
                 {
                     // when the request has been completed, we want to bind the data to the grid.
                     // the Result property of the EventArgs contains the returned data.
                     // ObservableCollection is a common collection that is used when databinding to
                     //    a DataGrid.
                     ObservableCollection<string> myList = es.Result;

                     dataGrid2.DataContext = myList;
                 };

                apClient.ApAsync();

            }


        }






Can this be done?
Was This Post Helpful? 0

#11 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: connecting to a database via silverlight & wcf

Posted 13 May 2010 - 03:22 AM

you can make calls to as many databases as you want from a service. you do not have to have a separate service for each database.
Was This Post Helpful? 0
  • +
  • -

#12 Guest_megan_111*


Reputation:

Re: connecting to a database via silverlight & wcf

Posted 13 May 2010 - 04:20 AM

I tried to do that earlier with the one service and two client objects (one to call each method for the database) but I got errors until I created a separate service/client.

At the moment I am trying to figure out how to pass a parameter through to the service. Thanks for all your help so far :)
Was This Post Helpful? 0

#13 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: connecting to a database via silverlight & wcf

Posted 13 May 2010 - 04:44 AM

View Postmegan_111, on 13 May 2010 - 06:20 AM, said:

I tried to do that earlier with the one service and two client objects (one to call each method for the database) but I got errors until I created a separate service/client.

At the moment I am trying to figure out how to pass a parameter through to the service. Thanks for all your help so far :)


I haven't tested it, but I assumed it would work fine. Maybe it doesn't. I will try it out.

To pass a parameter, you need to change the service method to accept a parameter, then your client class will show that you need to pass the parameter.
Was This Post Helpful? 0
  • +
  • -

#14 Guest_megan_111*


Reputation:

Re: connecting to a database via silverlight & wcf

Posted 13 May 2010 - 05:14 AM

thank you :)
Was This Post Helpful? 0

Page 1 of 1