The main objective is to activate Intellisense in the dataset so that there are no errors while typing the column names and table names of SQL Server table and display the data in the GridView Control.
Creating SQL Server Database and Table
The following shows how to create the new table:
- CREATE TABLE [dbo].[Employee] (
- [Emp_ID] INT IDENTITY (1, 1) NOT NULL,
- [Name] NVARCHAR (40) NULL,
- [Designation] NVARCHAR (100) NULL,
- [Salary] INT NULL,
- PRIMARY KEY CLUSTERED ([Emp_ID] ASC)
- );
Add Some Sample Data to it using the following QueryThe following shows how to add sample data to the preceding table:
- INSERT INTO [dbo].[Employee]
- ([Name]
- ,[Designation]
- ,[Salary])
- VALUES
- (<Name, nvarchar(40),>
- ,<Designation, nvarchar(100),>
- ,<Salary, int,>)
- GO
Step 1
Create an Empty ASP.NET web application and add a Webform to it naming it Demo.aspx and drag a GridView control to it.
Step 2
Go to Add New Item in the Solution Explorer and add a dataset there naming it Employee.xsd.
Step 3
Go to View -> Server Explorer and add a data connection to your database table and drag the table onto the XSD file.
Step 4
Go to the Code Behind file of the Webform and paste the following code there.
Demo.aspx
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- EmployeeTableAdapters.EmployeeTableAdapter DA = new EmployeeTableAdapters.EmployeeTableAdapter();
-
- Employee.EmployeeDataTable employeeDataTable = new Employee.EmployeeDataTable();
- DA.Fill(employeeDataTable);
-
- Session["DATATABLE"] = employeeDataTable;
-
- GridView1.DataSource = from emp in employeeDataTable select new {
- emp.Emp_ID, emp.Name, emp.Designation, emp.Salary
- };
- GridView1.DataBind();
- }
-
- }
Explanation of preceding code:
- If it is the get request of the page or simply if it is not a Postback request, then the compiler will execute the code.
- When we add a table to the dataset like we did in Step 3, the classes are generated automatically based on the columns present in the table.
- So, here EmployeeTableAdapter is a class generated by the dataset from Step 3 and simply we made the instance of it named "DA".
- Now, we need to retrieve that table of that DataAdapter, so we used the Dataset name followed by the name of the DataTable. Don't worry, all the classes will be available automatically in the Intellisense of Visual Studio and we also created its instance named "employeeDataTable".
- We filled the data in the DataTable from the DataAdapter just like we do in our simple ADO.NET Code.
- We are saving this DataTable in a Session Variable so that we can use it in our Next Tutorial that I will post soon.
- Now we have used some LINQ Code to fetch the data from the DataTable.
- "emp" is a new variable used to fetch the properties of the DataTable. When we write the name of this variable and press (.), the column name will appear automatically. This was our objective.
- Finally we bound the data fetched to the GridView.
- Save all the changes.
Step 5
Press the F5 key to run the project and you will see how easy it is to fetch data using a Strongly Typed Dataset without making any mistakes.
I hope you like this tutorial.
Please refer to my blog for more tutorials on ASP.NET and other programming Languages. Also like my FB Page.