Create WPF Application Project Using SQL Database From A Class Library

 1. Create a SQL Database Project using Class Library in Visual Studio 2012

On Visual Studio 2012, Click on “New Project”

Capture01

Create a new “Class Library” Project on “visual C#” –> “Windows” and name it “MyClassLibrary” 

 Capture02

 Now, create a Database by a right click on “MyClassLibrary” à “add” –> “New Item”. on “Visual C# Items” –> “Data”, click on “Service-based Database” and name it “Database.mdf”

Capture03

Capture04

Capture05

Capture06

Note: when the Data Source Configuration Wizard window pops up, click on “Cancel”. The Data model will be created later on.

On Server Explorer (VIEW à Server Explorer), click on “Data Connections” –> “Database.mdf”, Right Click “Tables” –> “Add New Table”

Capture07 Capture08

Create the Table Companies like Shown in the figure below and click on “Update” to validate and “Update Database” to save it the changes.

  Capture09 Capture10 Capture11

2. Create a simple data access layer using Repository Pattern with Entity Framework

Create a data model by Right Click on “MyClassLibrary” –> “add” –> “New Item”. on “Visual C# Items” –> “Data”, click on “ADO.NET Entity Data Model” and name it “Model.edmx”

 Capture12

On the Entity data model Wizard, Click on “Generate from database” and “Next”.

 Capture13

On the window(Choose Your Data connection), Save entity connection settings in App.config As “DatabaseContext” and Click “Next”

 Capture14

On the window (Choose Your Database Objects and Settings) Tick on the “Tables” –> “dbo” –> “companies” Created in earlier steps and click on “Finish”.

 Capture15

Create an Interface for the repository by Right Click on “MyClassLibrary” –> “add” –> “New Item”. on “Visual C# Items” –> “Code”, click on “Interface” and name it “IRepository.cs”

  Capture16Capture17 Capture18

Capture19

Write the code for the “IRepository.cs” Interface file to declare generic interface function (REFER TO  SOURCE_CODE File Attached Below)

Create the Repository Class by Right Click on “MyClassLibrary” –> “add” –> “New Item”. on “Visual C# Items” –> “Code”, click on “Class” and name it “Repository.cs”

 Attach code for the “Repository.cs” class file to implement generic function we declared in “IRepository.cs” Interface file (REFER TO  SOURCE_CODE File Attached Below)

Capture20 Capture21

Note: some of the Data type from the codes might be identified as error because their reference assembly has not been add or their namespace is not used. One way of solving it is to Right click on the unidentified data type –> resolve and click on the appropriate namespace.

3. Create a simple data access layer Unit of Work Pattern with Entity Framework

 At this stage of the project building, a “Database.cs” Class file is created by Right Click on “MyClassLibrary” –> “add” –> “New Item”. on “Visual C# Items” –> “Code”, click on “Interface” and name it “Database.cs”

–        Attach the code for the “Database.cs” class file to implement the Unit work, that make the database access easier (REFER TO  SOURCE_CODE File Attached Below).

Capture22Capture23  

 

4. Create a WPF Application

–        On Visual Studio 2012, Click on “New Project”

–        Create a new “Class Library” Project on “visual C#” –> “Windows” and name it “MyWpfApplication”

Capture24

–        Add the “MyClassLibrary” project created earlier by a Right Click on “Solution ‘MyWpfApplication’ (1 project)” –> “add” –> “Existing Project”. On the browser window, go on the “MyClassLibrary” –> “MyClassLibrary” click on the “MyClassLibrary” c# file.

Capture25Capture26

Capture27 Capture28

–        Add the “MyClassLibrary” reference solution by Right Click on the “MyWpfApplication” –> “Add Reference”. On “Solution” –> “Projects”, Tick on “MyClassLibrary”.

Capture29 Capture30

–          In “MyClassLibrary” –> “AppConfig”, copy the connection string code as below and attach it to the “MyWpfApplication” –> “AppConfig” file in between < configuration > </configuration> (REFER TO  SOURCE_CODE File Attached Below).

Capture31Capture32  

We are good to go; we already configure our class library into the WPF application. All we need to do now is to is to create our application main window interface and its implementation

5. SIMPLE METHOD OF BINDING DATABASE DATA FROM A CLASS LIBRARY TO A WPF GRID INTERFACE

–        Go on “MyWpfApplication” –> “Mainwindow.xaml”. On “PROJECT” at the upper side the window, Click on “Add New Data Source”.

Capture33

–        On the Data source configuration Wizard window to choose a data Source Type, Select “Object” and click on “Next”.

Capture34

–        On the window to Select the Data Objects, tick on “MyClassLibrary” à “MyclassLibrary” à “Company” and Click on “Finish”.

Capture35

–        Now the Connection appears on the left side of your window, Hold-Click on “Company” and Drag it into your interface designer.

Capture36

–                    From here you can design whatever you want, knowing the data from the database are already bind in your interface through the MainWindow.xaml code lines highlighted in red on the source_code file (REFER TO  SOURCE_CODE File Attached Below).

–        On “MyWpfApplication” –> “Mainwindow.xaml” –> “Mainwindow.xaml.cs”, attach the code below to implement and exploit the interface the  interface created, knowing that the Mainwindow.xaml.cs code lines highlighted in red were generated at the grid creation.

Note:

The Private function “Window_loaded_1” is used load data from the database to the grid

On “MyWpfApplication” –> “Add Reference”, search for System.configuration and System.Data.Entity  in the “Assembly” –> “Framework” and Tick on them .

On “Solution MyWpfApplication (2 Project)” –> “Manage NuGet Packages…”, search for Entity framework version 5.0.0 online and install it.

Be aware that the application and the interface integrity control are not NOT HANDLE HERE

Therefore:

To ADD new company, fill up Following Colon: Name, Telephone, Email, GPS

To UPDATE a company row, fill up following colon: Id, Name, Telephone, Email, GPS

To DELETE a company row, only fill up Id colon

RUN YOUR APPLICATION AND ENJOY!!!!!

Source_Code

Advertisements

14 comments

  1. Hey, I think your website might be having browser
    compatibility issues. When I look at your blog site
    in Firefox, it looks fine but when opening in Internet Explorer,
    it has some overlapping. I just wanted to give you a quick heads
    up! Other then that, great blog!

    Like

  2. Thanks on your marvelous posting! I quite enjoyed reading it, you
    may be a great author. I will be sure to bookmark your blog and may come back in the
    future. I want to encourage yourself to continue your
    great work, have a nice weekend!

    Like

  3. I am really enjoying the theme/design of your website.

    Do you ever run into any browser compatibility issues? A handful of my
    blog readers have complained about my site not operating correctly in Explorer but looks great in Safari.
    Do you have any solutions to help fix this problem?

    Like

  4. What i do not realize is in fact how you’re now not really a lot more neatly-appreciated than you may
    be right now. You are so intelligent. You know therefore
    significantly with regards to this topic, made me in my view consider it from numerous numerous angles.

    Its like women and men are not involved unless it is one thing to accomplish with Lady gaga!
    Your personal stuffs excellent. At all times maintain it up!

    Like

  5. May I just say what a relief to find an individual
    who really understands what they’re discussing on the net.

    You certainly know how to bring a problem to light and make it important.
    A lot more people have to check this out and understand this side of the story.
    I was surprised you’re not more popular since you surely
    possess the gift.

    Like

  6. I was recommended this website via my cousin. I’m now not
    positive whether or not this put up is written by way of
    him as no one else recognise such specific approximately
    my difficulty. You are amazing! Thank you!

    Like

  7. I’ve been surfing on-line greater than 3 hours today, but I never
    found any interesting article like yours.
    It is lovely value sufficient for me. In my view, if all webmasters and bloggers made good content as you probably did, the internet will likely
    be much more helpful than ever before.

    Like

  8. Hello there I am so delighted I found your weblog, I really found you
    by error, while I was browsing on Bing for something else, Anyways I am here now and would just like
    to say kudos for a tremendous post and a all round entertaining blog (I
    also love the theme/design), I don’t have time to read it all at the moment but
    I have book-marked it and also included your RSS feeds, so when I have
    time I will be back to read much more, Please do keep up the awesome work.

    Like


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s