Linq to SQL basic list/grid view and form view

Lately I've been getting more and more into using Linq to SQL and think it rocks, Linq takes care of all the hard back end database functions so that you can focus on the interesting business logic of fun design and styling. I've been doing some research on the basics of Linq and found that most tutorials only show the List/GridView. However there was not many tutorials on working with Forms. So I have put together this basic tutorial to get you started. I have to give a lot of credit to aDefWebserver for this tutorial http://www.adefwebserver.com/DotNetNukeHELP/LinqTutorial2/LinqTutorial2_3.htm which although it specific to DotNetNuke a lot of the concepts can be learned here.

I have created a very basic database called "Contacts" with a table called "MyContacts", here is a screenshot of this table.

I prefer to set up a new database user now for this specific application so that when it comes to deploying the application your ready to go.

 

Now I will create a New Web Site in Visual Web Developer 2008 Express Edition called "MyContacts"

 In the Database Explorer right button click on the Data Connections root and select Add Connection

Add a new Linq to SQL Classes item called "My_Contacts_DataClass.dbml" under the App_Code folder

Once the My_Contacts_DataClass opens drag the "MyContacts" table onto the data class from the Database Explorer

 

Add a new web form called "List_MyContacts.aspx" for the list/grid view

Add a LinqDataSource from the Data section in the Toolbox to your new List_Mycontacts page and select Configure Data Source

Select the My_contacts_DataClassDataContext object and click next

Click the "Advanced" button and select which options you want to allow from the list/grid view. In this case I only want to allow Delete

Add a GridView from the Data section in the Toolbox to your new List_Mycontacts page and select the LinqDataSource1 as the Data source. Then check Enable Deleting and Selection so that the user can either delete the record directly from the list or select it to view it in the form view mode.

Select the GridView control and open the properties and click on the lightning bolt to see the events. Double click on the SelectedIndexChanged event and enter the following line of code within this SelectedIndexChanged procedure. This code will open the update page with the selected record in the form view.

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

    {

        Response.Redirect("Update_MyContacts.aspx?ContactId=" + Convert.ToInt32(GridView1.SelectedDataKey.Value) + "");

    }

 

Add a new web form called "Update_MyContacts.aspx" for the form view

Add a LinqDataSource from the Data section in the Toolbox to your new Update_Mycontacts page and select Configure Data Source

Select the My_contacts_DataClassDataContext object and click next

Click the "Advanced" button and select which options you want to allow from the list/grid view. Here I want to allow Insert, Update and Delete

Also click on the Where button and select the Contact_Id is equal to the a parameter passed via the query string so that when a user selects an item on the list/grid view page the update form will know which item the user wants to edit. Add the where clause

 

Add a FormView from the Data section in the Toolbox to your new Update_Mycontacts page and select the LinqDataSource1 as the Data source.

That's it now you can open your browser and browse the application. Here is the first page the list/grid view

When you click on the Select link in the grid view the page redirects to the Update page to display the form view in read only mode

 

Now when the user clicks on the Edit link the form will automatically switch into update mode so they can update the data and save it

Now that all of the data access functionality has been automatically created by Linq all you need to focus on is design and logic.