none
Working with Data Base in Silverlight 4

    Question

  • Hi!

    I have a problem with my data base connection. I created a new project Silverlight Business Application. I added to my solution another project - WCF RIA Services Class Library. In Server Explorer I created a new data base and added 3 tables to it named Project, Resource and User. To Project and User Tables I added some data (right click on Table in Server Explorer => Show Table Data). Then I added to the sever part of my Class Library Project a new ADO.NET Data Entity Model, chose Generate from database then chose my created database. Next I added to the sever part of my Class Library Project a new Domain Service Class checking all my three tables. After that I added to the client part of Class Library Project a new Silverlight User Control. In my new control I try to get some data from database. This is the code:


    TranslateDomainContext tdc = new TranslateDomainContext();
    tdc.Load(tdc.GetProjectQuery());

    TranslateDomainContext tdc = new TranslateDomainContext();

    tdc.Load(tdc.GetProjectQuery());


    But when these lines are executed the Result View for tdc.Projects is empty.

    How can I get my data?

    Thursday, July 29, 2010 8:42 AM

All replies

  • Hi


    Do you check for tdc.Projects being empty in code behind or does the bound usercontrol(s) simply not show anything?

    It might be a problem with your UI binding but it's hard to tell without seing both the XAML and the XAML.cs in question.

    Thursday, July 29, 2010 5:50 PM
  • Hello,

    Load is asynchronous, and therefore you can't see the result after calling that immediately. If you set DataGrids ItemsSource property to tdc.Projects, then you will see the result

     

    Kind Regards

     

    Friday, July 30, 2010 2:08 AM
  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using TranslateService.Web;
    namespace TranslateService
    {
        public partial class SilverlightControl1 : UserControl
        {
            public SilverlightControl1()
            {
                InitializeComponent();
                DomainService1 dom = new DomainService1();
                dom.Load(dom.GetProjectQuery());
                Project proj = new Project();
                foreach (Project p in dom.Projects)
                {
                    if (p.ProjectId == 0)
                    {
                        proj = p;
                    }
                }
                
            }
        }
    }

    Hi,


    Thank you for replies. 


    I try to use foreach for tdc.Projects after Load method, but it does not work because tdc.Projects is empty.


    Hear is the code of SilverlightControl1.xaml.cs:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Net;

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Documents;

    using System.Windows.Input;

    using System.Windows.Media;

    using System.Windows.Media.Animation;

    using System.Windows.Shapes;

    using TranslateService.Web;


    namespace TranslateService

    {

        public partial class SilverlightControl1 : UserControl

        {

            public SilverlightControl1()

            {

                InitializeComponent();

                DomainService1 dom = new DomainService1();

                dom.Load(dom.GetProjectQuery());

                Project proj = new Project();

                foreach (Project p in dom.Projects)

                {

                    if (p.ProjectId == 0)

                    {

                        proj = p;

                    }

                }

                

            }

        }

    }


    SilverlightControl1.xaml is standard, i havn't changed it.

    Here is the code of DomainService1.cs:

    namespace TranslateService.Web

    {

        using System;

        using System.Collections.Generic;

        using System.ComponentModel;

        using System.ComponentModel.DataAnnotations;

        using System.Data;

        using System.Linq;

        using System.ServiceModel.DomainServices.EntityFramework;

        using System.ServiceModel.DomainServices.Hosting;

        using System.ServiceModel.DomainServices.Server;



        // Implements application logic using the NewJavaCompileEntities context.

        // TODO: Add your application logic to these methods or in additional methods.

        // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access

        // Also consider adding roles to restrict access as appropriate.

        // [RequiresAuthentication]

        [EnableClientAccess()]

        public class DomainService1 : LinqToEntitiesDomainService<NewJavaCompileEntities>

        {


            // TODO:

            // Consider constraining the results of your query method.  If you need additional input you can

            // add parameters to this method or create additional query methods with different names.

            // To support paging you will need to add ordering to the 'Project' query.

            public IQueryable<Project> GetProject()

            {

                return this.ObjectContext.Project;

            }


            public void InsertProject(Project project)

            {

                if ((project.EntityState != EntityState.Detached))

                {

                    this.ObjectContext.ObjectStateManager.ChangeObjectState(project, EntityState.Added);

                }

                else

                {

                    this.ObjectContext.Project.AddObject(project);

                }

            }


            public void UpdateProject(Project currentProject)

            {

                this.ObjectContext.Project.AttachAsModified(currentProject, this.ChangeSet.GetOriginal(currentProject));

            }


            public void DeleteProject(Project project)

            {

                if ((project.EntityState == EntityState.Detached))

                {

                    this.ObjectContext.Project.Attach(project);

                }

                this.ObjectContext.Project.DeleteObject(project);

            }


            // TODO:

            // Consider constraining the results of your query method.  If you need additional input you can

            // add parameters to this method or create additional query methods with different names.

            // To support paging you will need to add ordering to the 'Resource' query.

            public IQueryable<Resource> GetResource()

            {

                return this.ObjectContext.Resource;

            }


            public void InsertResource(Resource resource)

            {

                if ((resource.EntityState != EntityState.Detached))

                {

                    this.ObjectContext.ObjectStateManager.ChangeObjectState(resource, EntityState.Added);

                }

                else

                {

                    this.ObjectContext.Resource.AddObject(resource);

                }

            }


            public void UpdateResource(Resource currentResource)

            {

                this.ObjectContext.Resource.AttachAsModified(currentResource, this.ChangeSet.GetOriginal(currentResource));

            }


            public void DeleteResource(Resource resource)

            {

                if ((resource.EntityState == EntityState.Detached))

                {

                    this.ObjectContext.Resource.Attach(resource);

                }

                this.ObjectContext.Resource.DeleteObject(resource);

            }


            // TODO:

            // Consider constraining the results of your query method.  If you need additional input you can

            // add parameters to this method or create additional query methods with different names.

            // To support paging you will need to add ordering to the 'User' query.

            public IQueryable<User> GetUser()

            {

                return this.ObjectContext.User;

            }


            public void InsertUser(User user)

            {

                if ((user.EntityState != EntityState.Detached))

                {

                    this.ObjectContext.ObjectStateManager.ChangeObjectState(user, EntityState.Added);

                }

                else

                {

                    this.ObjectContext.User.AddObject(user);

                }

            }


            public void UpdateUser(User currentUser)

            {

                this.ObjectContext.User.AttachAsModified(currentUser, this.ChangeSet.GetOriginal(currentUser));

            }


            public void DeleteUser(User user)

            {

                if ((user.EntityState == EntityState.Detached))

                {

                    this.ObjectContext.User.Attach(user);

                }

                this.ObjectContext.User.DeleteObject(user);

            }

        }

    }



    Friday, July 30, 2010 6:55 AM
  • Now when I try to debug my project on browser I have an error:

    An unhandled excetion ('Unhandled Error in Silverlight Application

    Code: 4004

    Category: ManegedRuntimeError

    Message:

    System.ServiceModel.DomainServices.Client.DomainOperationException:

    When I try to Install OOB Application and run the same code I have another message:

    Load operation failed for query 'GetProject'. The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

    at System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
       at System.Data.EntityClient.EntityConnection..ctor(String connectionString)
       at System.Data.Objects.ObjectContext.CreateEntityConnection(String connectionString)
       at System.Data.Objects.ObjectContext..ctor(String connectionString, String defaultContainerName)
       at TranslateService.Web.NewJavaCompileEntities..ctor() in C:\Users\Administrator\Documents\Visual Studio 2010\Projects\JavaCompillerOld\TranslateService.Web\Model1.Designer.cs:line 40

    at System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)

       at System.Data.EntityClient.EntityConnection..ctor(String connectionString)

       at System.Data.Objects.ObjectContext.CreateEntityConnection(String connectionString)

       at System.Data.Objects.ObjectContext..ctor(String connectionString, String defaultContainerName)

       at TranslateService.Web.NewJavaCompileEntities..ctor() in C:\Users\Administrator\Documents\Visual Studio 2010\Projects\JavaCompillerOld\TranslateService.Web\Model1.Designer.cs:line 40


    Best regards.

    Kola.

    Friday, July 30, 2010 7:07 AM
  • As Gioa mentions, the load is asynchronous. That means that your application moves on while the query completes. You might very well be asking for the Projects before they are loaded.

    What you can do is modify

     dom.Load(dom.GetProjectQuery());

    to

     var loadIt=dom.Load(dom.GetProjectQuery());

    loadIt.Completed+=(se,ea)=>

    {

    //do the stuff you like on the Projects

    };


    Friday, July 30, 2010 7:21 AM
  • Thank you very much, ChristianL, it is working now!

    Friday, July 30, 2010 7:40 AM
  • I made haste. I have the same errors with this code((

    Friday, July 30, 2010 7:49 AM
  • ...

    var loadIt=dom.Load(dom.GetProjectQuery());

    loadIt.Completed+=(se,ea)=>

    ...


    When I see code like this, I wonder if it isn't open to an error.  (Admittedly I don't use RIA)  What if the call returns before the Completed handler gets assigned?  I know it is unlikely, but wouldn't it be better to:

    1. create the service proxy

    2. setup the Completed handler

    3. make the async call

    Friday, July 30, 2010 7:54 AM
  • I have found that my errors said "something is wrong with your app" but didn't say what was exactly the reason of it. 

    To come to know what is the actual error I must "turn on WCF error logging or use Fiddler". But I know neither how to turn on WCF error logging nor what is Fiddler and how to use it.

    Also I'm not sure I've understood everything correctly. Can anybody explain it to me?

    Thank you.

    Friday, July 30, 2010 8:33 AM
  • Mtiede: You might be right, but I've never experienced that happening, not even with the smallest of queries.

    Kola: Could you post the Stacktrace?

    Fiddler is a lovely tool for debugging those annoying "Not Found" errors and the like when working with RIA, but it does take a while to get used to.

    Friday, July 30, 2010 8:46 AM
  • ChristianL, My Call Stack  Window is empty when i'm debugging application and getting an error.

    Friday, July 30, 2010 9:07 AM
  • That sounds odd - the solution compiles ok?

    Friday, July 30, 2010 9:10 AM
  • Yes. Without any errors. Maybe I have to configure something, change some options to see stack trace? 

    Friday, July 30, 2010 9:22 AM
  • Stack trace is not empty when i toggle a breakpoint anywhere. 


    Friday, July 30, 2010 9:34 AM
  • Here it is:

    > TranslateService!TranslateService.SilverlightControl1.SilverlightControl1() Line 20 C#

    > TranslateService!TranslateService.SilverlightControl1.SilverlightControl1() Line 20 C#

      JavaCompiller!JavaCompiller.Views.JavaIDE.Compile_Click(object sender, System.Windows.RoutedEventArgs e) Line 522 + 0x16 bytes C#

      [External Code]

    Friday, July 30, 2010 9:37 AM
  • uhm, how did Java get into the equation?

    What setup are you running, something different to the usual IIS/SQL Server...? This should not be directly caused by the changes you've just made to the loading of Projects.

    Friday, July 30, 2010 9:43 AM
  • JavaCompiller is the name of my project. I didn't do any changes to the running process or to the loading of projects.

    Friday, July 30, 2010 10:11 AM
  • Ah ok :)

    And this Compile_Click event - is that where you added the .Completed event?

    Does it work if you revert to the version prior to the change?

    Friday, July 30, 2010 11:51 AM