Building Applications with DLinq Designer and Visual Studio 2005
page 2 of 3
by Satheesh Kumar
Feedback
Average Rating: 
Views (Total / Last 10 Days): 19257/ 64

Step-by-Step Demonstration

Open Visual Studio 2005 and select File -> New -> Project and select the option LINQ Windows Application.  Name the Project and select OK.

Figure 1

For creating the windows application, I have created the following database with two tables as Project and Requirement with the fields listed below.

Figure 2

Now select the project, right click and select Add -> New Item.  Then select DLinqObjects from the List of Items available.  The DLinqObject gets added to the project and the designer surface opens.

Figure 3

 

Expand the Server Explorer, open the database and expand the tables.  Select the table "Project" from the list and then drag and drop it on the surface of the DLinq designer.  Build the project after dropping the object on the design surface.  Now if you open the DLinqObjects1.cs file, you can see the following code in it.

Listing 1

public partial class @__SQLEXPRESSDataContext: System.Data.DLinq.DataContext
{
 
  public System.Data.DLinq.Table < Project > Projects;
 
  public System.Data.DLinq.Table < Requirement > Requirements;
 
  [System.Diagnostics.DebuggerNonUserCodeAttribute()]
  public@__SQLEXPRESSDataContext(): base(new
    System.Data.SqlClient.SqlConnection(global: :
    LINQWindowsApplication4.Properties.Settings.Default.DataConnection)){}
}
 
[System.Data.DLinq.Table(Name = "Project")]
public partial class Project: System.Data.DLinq.INotifyPropertyChanging,
  System.ComponentModel.INotifyPropertyChanged
{
 
  private int _ProjectID;
 
  private string _ProjectName;
 
  private string _ProjectDesc;
 
  private System.DateTime _StartDate;
 
  private System.DateTime _EndDate;
 
  private int _GroupID;
 
  private System.Nullable < bool > _ActiveFlag;
 
  private System.Data.DLinq.EntitySet < Requirement > _Requirements;
 
  [System.Diagnostics.DebuggerNonUserCodeAttribute()]
  public Project()
  {
    this._Requirements = new System.Data.DLinq.EntitySet < Requirement > (new
      System.Data.DLinq.Notification < Requirement > (this.Attach_Requirements),
      new System.Data.DLinq.Notification < Requirement >
      (this.Attach_Requirements));
  }
 
  [System.Diagnostics.DebuggerNonUserCodeAttribute()][System.Data.DLinq.Column
    (Name = "ProjectID", Storage = "_ProjectID", DBType =
    "int NOT NULL IDENTITY", Id = true, AutoGen = true)]
  public virtual int ProjectID
  {
    get
    {
      return this._ProjectID;
    }
    set
    {
      if ((this._ProjectID != value))
      {
        this.OnPropertyChanging("ProjectID");
        this._ProjectID = value;
        this.OnPropertyChanged("ProjectID");
      }
    }
  }

The @__SQLEXPRESSDataContext is the connection object for the database connection.  I used SQLExpress for the example.   You can see the code for the entity table "Project" and the columns added to the designer.  I have not given the code for the other columns as it is similar to the field "ProjectID."

Select the menu Data -> Add New Data Sources in Visual Studio.  This will open the wizard for adding data sources.  The first step of the wizard will ask you to select the type of the source that will provide data to the application.  Select Object from the options.   

The next step will list the available objects for binding the controls.  As we have the Projects entity class on the designer, this will get listed under the project name.  Select the Object "Project," select "next" and then finish the wizard.

Figure 4

Now you can see the data source added to the project under the Data Sources folder.  You can also view the properties of the data source by selecting the menu Data -> Show DataSources as shown below.

Figure 5

 

 

Now we are ready to Design the windows form.  Open the form design surface.  On the Data Sources explorer select the Project Datasource.  You can see the following options when you select the drop down next to the Data Source "Project."  This is to ease the design of the user Interface.  You can choose how you want to present the UI to the user.  For this example I have selected Details View.  

Figure 6

 

 

Now drag and drop the Project Data Source on the form design surface.  The Visual Studio places the corresponding data controls on the form according to the data type of the column/properties of the Table/Entity class.  We do not need to select the controls and bind it manually.  It is all done by the Visual Studio.  You can also see a navigator on top of the form.  This gives the flexibility of navigation on the records and also provides the editing controls in the navigation bar.  This helps us reduce the design time.

Figure 7

 

 

Add the following code to the form file to provide the necessary connections to the DataSource and then bind it.  The con is the connection object created using the "@__SQLEXPRESSDataContext" connection that we saw earlier.

Listing 2

namespace LINQWindowsApplication4
{
  public partial class Form1: Form
  {
    private@__SQLEXPRESSDataContext con = new@__SQLEXPRESSDataContext();
 
    public Form1()
    {
      InitializeComponent();
    }
 
    private void Form1_Load(object sender, EventArgs e)
    {
      projectBindingSource.DataSource = con.Projects.ToBindingList();
    }
 
  }
}

Now save the project, build it and execute it.  You can see the below UI with the navigation feature also in it.  The Save button is disabled as we have not enabled it nor have we added the code for it.  Before going for the editing feature, we will add another data source and a detail control for it.  Let us make this application as Master Detail application.

Figure 8

Now close the application and go to the design to open the DLinqObject Design Surface.  Now drag and drop the second table "Requirement" from the server explorer onto the Designer surface.  You can see the following on the design surface.  The Designer creates the Association relationship between the two entities.

Add data source for the "Requirement" Object just as we did previously for the project entity.  Use the Data Sources wizard for this.

Figure 9

Now open the Data sources explorer and expand it.  You can see Requirements as one of the properties under the Project Data Source.  This means that the Requirement is one of the details of the project and the Requirement itself is an object with its own properties.

Figure 10

 

Figure 11

Figure 12

Using the drop down next to the "Requirements" property, set the view as "DataGridView."  This allows us to see the details in a grid when we navigate through the project records.

Drag and drop the Requirements object from the Data Sources explorer onto the form design surface.  Select the Save button in the navigator and open the properties window.  Using the properties window, enable the save button.  Add the following code for the click event of the Save button.

Listing 3

private void projectBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
  con.SubmitChanges();
}

Save the project and execute it.   Now try navigating through the records; try editing and saving the changes.

Figure 13

The next step we can take is to create the search option on the Project name.  This is very simple to achieve by using the Linq query.  Let us see how we can do it.

Add a button next to the project name text box.  Name it whatever you would like.  Here I named it as "Find."  Add the following code to the Click event of the Find button.

Figure 14

Listing 4

private void FindButton_Click(object sender, EventArgs e)
{
  var projs = from projects in con.Projects where projects.ProjectName ==
    projectNameTextBox.Text select projects;
  projectBindingSource.DataSource = projs.ToBindingList();
 
}

Save and execute the project.  Now enter a valid project name in the project name text box and then click the "Find" button.  This will bring you the details of the project name entered and also the corresponding Requirement details in the grid.  


View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-09-15 6:43:54 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search