DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 Basic LinQ to SQL Queries and Setup ASP.NET 4.0

This tutorial will show you how to create a LinQ to SQL Class and attach it to a Gridview. Then we will learn some basic LinQ queries to help you sort through a Database.
 

Basic LINQ to SQL Queries and Setup ASP.NET 4.0
 
This tutorial will cover how to setup a LINQ to SQL connection from one of your created SQL databases.
It will then show you how to Query through that Database using LINQ queries in C#. This Application will
be created in Web Forms although it is also possible to use LINQ in an MVC Application.

 
Overview
 
1.	Create the Application and the Database

2.	Add the GridView Control and the LINQtoSQL Class

3.	Add and setup a Filter Button




We are using Server Intellect and have found that by far, they are the most friendly,
responsive, and knowledgeable support team we've ever dealt with!
 
Create the Application / Database
 
1.	Start an Empty Web Application in Visual Web Developer 2010

2.	Create a new Web Form and call it Default.aspx

3.	Create a New SQL Database and call it EmployeeDB.mdf

4.	Inside the database add a new Table called with the rows ID, Name, and Email

5.	Make the ID tab the Primary Key also make it an identity that auto increments and make sure that
	none of the columns can be null then save it as Employees It Should look like this:
	
	db1-1

6.	Now Enter Data into the Database so that it looks like this:

	db1-2

 
Add a GridView Control and a LINQ to SQL class
 
1.	Add a GridView Control to the Form and name it GridView1

2.	Right click on the project and select add -> new item

3.	Select LINQ to SQL Class and name it EMPDB.dbml

4.	When this open it will be blank, we need to drag out the Employees Table onto it and this will appear

	db1-3
	
5.	Now add a Button to the bottom of the Web form name it btnFilter and label it Filter

6.	Now Build the project

7.	The next step is to add a LINQDatasource to the form from the Data section of the Toolbox and name it LinqDataSource1

8.	Now in the design view of the form click the smart tag on the LINQDatasource and click Configure data source.  

9.	In the wizard you should be able to select a DataContext named (“ProjectName”.EMPDBDataContext)

	db1-4

10.	Finish running through the wizard and the datasource should be setup

11.	Now we can click the smart tag on the gridview control and select LinqDataSource1 as the datasource

12.	Now Run the project and it should display your table


We chose Server Intellect for its dedicated servers,for our web hosting.
They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

Add the Filter LINQ Query
1.	Double click the Filter Button we made to open up the c# code behind

2.	Inside the function paste this code

	EMPDBDataContext Context = new EMPDBDataContext();
            var customers = from c in Context.Employees
                        where (c.Name=="Jared Pittman")
                        select c;
            GridView1.DataSourceID = "";
            GridView1.DataSource = customers;
            GridView1.DataBind(); 
	
    
3. This code will filter through the Database and find all of the entries where the Name Column is equal to “Jared Pittman” and select that line for a full code breakdown click here 4. Now run the project and click the button it should filter the gridview to only show the one line in which my name is in the Name slot. Before Click: db1-5 After Click: db1-6 Conclusion In this tutorial we have created a SQL Database and a LINQ to it allowing for easy manipulation inside the c# code behind. We also used LINQ commands in order to filter out some results for our gridview. Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

Download the source(179KB)