locked
Binding database data to a VS 2012 Express control using WPF & Entity Framework RRS feed

  • Question

  • They added an additional step to VS 2012 Express for the desktop to make the Data Sources window show the database tables, and there’s an additional step needed to make the data show up in your user control.  I found this information at

    http://msdn.microsoft.com/en-us/data/jj574514

    When using this information and then dragging a table control onto the design window the JobsViewSource resource and the JobsDataGrid control were added to XAML, and the following code-behind was added to my project.

    Class MainWindow
    
        Private Sub Window_Loaded_1(sender As Object, e As RoutedEventArgs) Handles MyBase.Loaded
            Dim JobViewSource As System.Windows.Data.CollectionViewSource = CType(Me.FindResource("JobViewSource"), System.Windows.Data.CollectionViewSource)
            'Load data by setting the CollectionViewSource.Source property:
            'JobViewSource.Source = [generic data source]
    
        End Sub
    End Class

    I cannot use the loading information from the website I mentioned above, so what Entity Framework code would I use in the place where they say [generic data source] to populate a JobNumberComboBox and a JobsDataGrid?

    Robert Agans

    Monday, April 1, 2013 9:13 PM

Answers

  • 4/5/2013

    Here’s some additional information;

    The code that I was trying to use to load my table data into my controls, shown below, appears to be right only if you are using the new SQL Server Express LocalDB that comes with VS 2012.

    Class MainWindow
    
        Private db As New DbParaFabEntities
    
        Private Sub Window_Loaded_1(sender As Object, e As RoutedEventArgs) Handles MyBase.Loaded
            Dim JobViewSource As System.Windows.Data.CollectionViewSource = CType(Me.FindResource("JobViewSource"), System.Windows.Data.CollectionViewSource)
            'Load data by setting the CollectionViewSource.Source property:
            'JobViewSource.Source = [generic data source]
    	JobViewSource.Source = db.Jobs.Local
        End Sub
    End Class

    But I’m using SQL Server Express 2008 R2 to load and serve my project, so I needed to change the last word on the last line of code above to;

    JobViewSource.Source = db.Jobs.ToList
    and then one of my projects showed the database information in my controls and worked fine.  The error message that I showed in an earlier reply above seems to be something peculiar to a different project that I am working on.


    Robert Agans

    For more information regarding the new LocalDB, see http://msdn.microsoft.com/en-us/library/hh873188.aspx

    • Edited by BobOfCalif Friday, April 5, 2013 8:20 PM
    • Marked as answer by BobOfCalif Friday, April 5, 2013 8:20 PM
    Friday, April 5, 2013 8:07 PM

All replies

  • Hi Robert,

    You may try to create an instance of context, and then set Source property to query from DbSet or local data, like:

    _context.Categories.Load()
    categoryViewSource.Source = _context.Categories.Local
    

    There are also some code snippet in the article you posted.

    What is JobNumberComboBox? Do you want to also try data binding with a ComboBox?

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, April 3, 2013 7:43 AM
  • Hi Chester,

    I have a VS 2010 Express project called WpfParaFab that I am trying to duplicate in VS 2012 Express.  When I first opened it in VS 2012 it worked fine, but I noticed that the Data Sources window didn’t show my database tables, so I tried to build it from scratch and the tables still didn’t show up.  Then I saw the website above that said that VS 2012 requires you to load the Data Sources window by clicking on the Object choice instead of the Database choice when connecting to a database, which works fine but you still have to add the code as I requested.

    I tried your code, but when I try to create an instance of context, I get the following error message:

    An unhandled exception of type 'System.IO.FileNotFoundException' occurred in PresentationCore.dll

    Additional information: Could not load file or assembly 'EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.

    It seems to me that the Entity Framework Data Model of my database is not recognized by the Data Sources mechanism, they don’t work together like they did with VS 2010.  When you have to chose Object instead of Database to load a second time the information from your database, it appears to cause a disconnect.

    You asked, "What is JobNumberComboBox? Do you want to also try data binding with a ComboBox?"  To answer, whenever a person drug a control from the Data Sources window in VS 2010 onto the design window it automatically set the code in XAML and the window_loaded code-behind, and everything needed to display the information in any control for that table was automatically added and displayed.  All of this has changed with VS 2012.

    What I was trying to do was add both a JobNumberComboBox and a JobsDataGrid from my Jobs table to show in my project.

    Bob


    Robert Agans


    • Edited by BobOfCalif Thursday, April 4, 2013 2:16 AM
    Wednesday, April 3, 2013 10:08 PM
  • 4/5/2013

    Here’s some additional information;

    The code that I was trying to use to load my table data into my controls, shown below, appears to be right only if you are using the new SQL Server Express LocalDB that comes with VS 2012.

    Class MainWindow
    
        Private db As New DbParaFabEntities
    
        Private Sub Window_Loaded_1(sender As Object, e As RoutedEventArgs) Handles MyBase.Loaded
            Dim JobViewSource As System.Windows.Data.CollectionViewSource = CType(Me.FindResource("JobViewSource"), System.Windows.Data.CollectionViewSource)
            'Load data by setting the CollectionViewSource.Source property:
            'JobViewSource.Source = [generic data source]
    	JobViewSource.Source = db.Jobs.Local
        End Sub
    End Class

    But I’m using SQL Server Express 2008 R2 to load and serve my project, so I needed to change the last word on the last line of code above to;

    JobViewSource.Source = db.Jobs.ToList
    and then one of my projects showed the database information in my controls and worked fine.  The error message that I showed in an earlier reply above seems to be something peculiar to a different project that I am working on.


    Robert Agans

    For more information regarding the new LocalDB, see http://msdn.microsoft.com/en-us/library/hh873188.aspx

    • Edited by BobOfCalif Friday, April 5, 2013 8:20 PM
    • Marked as answer by BobOfCalif Friday, April 5, 2013 8:20 PM
    Friday, April 5, 2013 8:07 PM