Use of Microsoft Application Blocks

Step 1: First of all you will have to download MSBlocks then install to them in your system.

For what are msblocks go on this link

http://www.code-magazine.com/Article.aspx?QuickID=0411061

and you want to download to these go on this link for demo application

http://www.codeproject.com/KB/database/MS_Application_Blocks.aspx

MsBlocks are nothing just a application which composed into Asp.net and will add his refrence into your web application then can use to his method into Your web site

For example

Make a new web site and Write into page source these lines of code

<%@ Page Language=”C#” AutoEventWireup=”true”  CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<head runat=”server”>
<title></title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:DataGrid ID=”DataGrid1″ runat=”server” Height=”100%” Width=”100%” HeaderStyle-BackColor=”Black” HeaderStyle-BorderStyle=”Groove” HeaderStyle-BorderWidth=”1″ HeaderStyle-Font-Bold=”true” HorizontalAlign=”Center”
AlternatingItemStyle-BackColor=”Red” AllowSorting=”true” AlternatingItemStyle-ForeColor=”White”
BackColor=”GrayText” ForeColor=”Yellow”>
<Columns >
</Columns>
</asp:DataGrid>
</div>
</form>
</body>
</html>

Step 2: now write these lines of code into code file of your page

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

using Microsoft.ApplicationBlocks.Data;

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

try

{

DataGridBind();

}

catch(Exception ex)

{

throw ex;

}

}

private void DataGridBind()

{

//Database Connection Information

String con = (string)(ConfigurationSettings.AppSettings[“cn”]);

//Sql Statement

String qry = “select top 5 *from TbEmp”;

//Create the Connection Objec

SqlConnection sqlcon = new SqlConnection(con);

//Create Employee Dataset

DataSet Empds = SqlHelper.ExecuteDataset(con, CommandType.Text, qry);

DataGrid1.DataSource = Empds.Tables[0];

DataGrid1.DataBind();

}

}

Remember to make a tbemp table into mssqlserver
TbEmp
———-
For more code of it’s example read out to this

Introduction

Summary:

Microsoft has developed and released Application Blocks for Data Access and Exception Management for using in .NET applications. These application blocks provide the .NET developer not only the ready to use code inside the application, but also the code which was built by encapsulating the best practices by Microsoft.

In this discussion we are going to examine the following:

  • What are the advantages of Data Access Application Block?
  • How to install the Data Access Application Block?
  • Create a sample .NET application using the Data Access Application Block.

Advantages of Data Access Application Block

Microsoft has developed this wrapper on top of the ADO.NET layer by encapsulating the performance and memory management related best practices for Microsoft SQL Server. So we need not worry about the data access performance and memory leaks.

This comes as a single assembly with a class consisting of useful methods which serve all kinds data access requirements. By using these simple calls in our code, we can reduce the amount of custom code we actually write in our conventional applications. Moreover we can also skip the over head of testing and maintenance of our custom code.

Microsoft provides the source code files, along with the Quick start samples and documentation. We can use the source code as it is or we can even customize as per our application needs.

Installing the Data Access Application Block

This is a free software and you can download it from here.

The current version of this block is 2.0. And we need to have Visual Studio. NET installed on the target machine.

Your installation should create an entry in the Start > Programs > menu called �Microsoft Application Blocks for .NET�, where it will show links to source code and samples.

Sample screenshot

Fig 1: After installing the Application Block.

Select the Data Access Application Block option by navigating to the path shown in the figure. It then opens the solution in the VS.NET IDE.

Sample screenshot

Fig 2: Open the Data Access Application Block solution in the IDE.

Click on the SQLHelper.cs file to look inside the code. It creates a namespace with the nameMicrosoft.ApplicationBlocks.Data with two sealed classes SqlHelper andSqlHelperParameterCache created inside that.

Now right click on the project name and click Build. Now you should see the assembly created at the following path:

C:\Program Files\Microsoft Application Blocks for .NET\Data Access v2\Code\CS\Microsoft.ApplicationBlocks.Data\bin\Debug

So we have built the Data Access block assembly, now we create a Web Application in which we refer this assembly. In our web application, we use a simple web form to connect to SQL Server 2000 and access the data from the database.

Create a sample .NET application using Data Access Block.

Step 1: Select File > New > Project from the file menu, select the ASP.NET Web Application and enter the project name and path accordingly.

Sample screenshot

Fig 3: Creating a sample web application.

Step 2: Now we add the Data Access Block to our project. To do this, right click on the references node and click Add Reference. Click on the Browse and select the Microsoft.ApplicationBlocks.Data.dll and click Ok button.

Sample screenshot

Fig 4: Adding the Data Access Block reference.

Step 3: Open the WebForm1 and drag a datagrid from the toolbox onto it.

Sample screenshot

Fig 5: Adding the datagrid to the WebForm1.

Step 4: Add the line for using the Data Access Block assembly.

Sample screenshot

Fig 6: Using Microsoft.ApplicationBlocks.Data namespace.

Step 5: Add the following code in the Page_Load event procedure.

This code creates a connection to the SQL Server database.

We have used the ExecuteDataset static method of the SqlHelper class to fetch the top 10 records from Employees table.

Sample screenshot

Fig 7: Call SqlHelper.ExecuteDataset method

In the above code, I have used my own credentials for connecting SQL Server. Please change those accordingly.

Step 6: Run the WebForm1.aspx page in the browser to see the output.

Sample screenshot

Fig 8: Result displayed in WebForm1.aspx

Be happy and enjoy in programming World!!!
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

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