Sunteți pe pagina 1din 43

Practical

File
Of
DOT NET FRAMEWORK

Submitted by: Under the guidance of:

1
Aim 1: Microsoft Visual Studio 2008
Installation

This Windows 7 tutorial is going to show how to install Visual Studio 2008 on Windows 7.
I'll install Microsoft Visual Studio 2008 Team Suite Edition on Microsoft Windows 7 Ultimate.
I had a clean install of Windows 7 on my new computer and I tried to install Visual Studio 2008
on Windows 7. I managed installing VS successfully with a simple and plain installation process.
I have an .iso file of the VSTS2008 Team Suite and extracted it on a folder. Within the extracted
folder the installation file is setup.exe install file.
When I double click to run the setup.exe file, the following User Account Control (UAC) screen
is asking me if I allow the setup.exe program to run.
Click "Yes" to continue with Visual Studio 2008 installation further on Windows 7.

2
The first screen in Visual Studio 2008 Setup is the below setup dialog screen.

Click the "Install Visual Studio 2008" link in order to start VS2008 on Windows 7 installation
process.

Microsoft Visual Studio Team System 2008 Setup wizard begins with the confirmation if you
want to help improve setup of Visual Studio 2008.

3
If you agree to submit anonymous information about your Visual Studio setup experiences to
Microsoft, mark the checkbox "Yes, send information about my setup experiences to Microsoft
Corporation."

The next screen in the VS 2008 setup wizard requests you to confirm that you have read and
accept the license terms.

You should also supply the Microsoft Visual Studio 2008 product key.

4
This step is which you select the Visual Studio 2008 features to setup. You have two quick
options first the default installation and the full installation.

5
Default installation will install common and recommended VS2008 features on the other hand
full installation will install all the features of MS Visual Studio 2008.

You can configure VS2008 installation in more detail by selecting the third option, Custom
installation.

The VS 2008 installation path is default "C:\Program Files\Microsoft\Visual Studio 9.0\" and
cannot be altered.

You can see the required disk space for each install option on the right bottom part of the setup
wizard.

6
The Microsoft VSTS 2008 installation process starts as seen in the below screenshot.

7
And the installation finished within one screenshot as you see here.

8
After the VS 2008 installation is completed successfully on Microsoft Windows 7 Ultimate
operating system, it is time for an update or installing the product documentation like help
documents, tutorials and samples, etc.

9
I will install MSDN documentation for VS2008 after installing service pack for VS2008
installation. I want to show you the below screenshot that I have installed the Visual Studio 2008
on my Windows 7 computer with its all features selected without any problem.

10
And here is the start page of the Microsoft Visual Studio Team System 2008 IDE with general
development settings is selected running successfully on Windows 7.

11
AIM2 : An Introduction to the .net framework?
The .NET Framework is an integral Microsoft Windows component designed to support next-
generation applications and services. Many fundamentals of the .NET Framework will be
familiar to developers who have worked in other object-oriented development environments.

Microsoft's .NET framework introduces a whole new way of programming the Microsoft
platform. Microsoft developers are primarily concerned with threads and memory (that's
basically the API programming model). This model carried over to all areas of development,
including Web development, placing a heavy burden upon programmers. .NET is built upon the
notion of managed types. Developers writing classic Windows code (and Web code) wrote
classes using C++ or Visual Basic. In many ways, types are similar to the notion of the C++ class
in that types are units of state with functionality attached to them. However, the similarity ends
there. Whereas it was incumbent upon the developer to manage instances of classes, types are
managed completely by the .NET runtime servicesthe Common Language Runtime (CLR).
Because the CLR takes over managing memory and threads, developers are much more at liberty
to concentrate on the actual application (rather than chasing down errant pointers, memory leaks,
and unexplained crashes).

ASP.NET introduces runtime services and a well-engineered class library for greatly enhancing
Web development. In a way, classic ASP was sort of taped onto the IIS/ISAPI architecture
without any real organic thought as to how early design decisions would affect developers later
on. Well, now it's later on and classic ASP.NET's warts have become fairly obvious.

ASP.NET 1.0 and 1.1 provided a significant number of features, including:

An object-oriented framework for defining applications

Separation of user interface declarations (HTML) and application logic

Compiled code for executing application logic Configurable session state management

Built-in data caching

Built-in content caching

A well-defined UI componentization architecture

High-level components for managing data formatting (grids, lists, text boxes)

Built-in program tracing and diagnostics

Built-in user input validation

12
An easy-to-use custom authentication mechanism

Solid integration with ADO.NET (the .NET database story)

Excellent support for Web Services

Zero reliance on the Component Object Model

An extensible pipeline with many places in which a request can be intercepted

ASP.NET 1.0 set the stage for many developers both moving into Web development and
moving to the Microsoft Platform.

ASP.NET 2.0 builds upon ASP.NET 1.0 and 1.1 by providing a number of new features in
addition to what already existed with ASP.NET 1.0. These features include

Master Pages and Skins

Declarative data-binding

Provider pattern model

New cache features

Membership controls

Personalization controls

Support for Web Parts

Programmable configuration

Administration tools

New compilation model

All the features of ASP.NET 1.0/1.1 are still there. However, these new features make
ASP.NET an even more compelling platform for creating Web sites.

A Word About the .NET Runtime

ASP.NET 2.0 is built upon Microsoft's Common Language Runtime. In its earliest days,
programming Windows involved interacting with the operating system at a very intimate level.
For example, getting a Window to show up on a screen took many lines of C code. In addition,
Windows included a rudimentary component technologyraw Dynamic Link Libraries.

13
Dynamic Link Libraries (DLLs) represent a necessary technology to enable composing systems
dynamicallythat is, to assemble applications from several disparate binary components.
However, DLLs by themselves are not sufficient for composing systems reliablyprimarily
because it's very difficult to manage multiple versions of a component (a DLL).

During the mid-90's, the Component Object Model (COM) emerged as a way to help manage
multiple versions of a component. By stipulating strict rules about how clients and components
may interact, COM represented a technology sufficient for composing applications from
different binary components. However, COM faced a few dead ends which became apparent as
developers began building larger systems.

First, COM relied on humans following rules to get things to interoperate. For example, COM
stipulates a rule that once a programmatic interface is published, it must never change. Changing
a published COM interface after clients begin coding against it will almost certainly bring a
system to its knees. In addition, COM relied on sometimes obtuse rules as far as managing
resources. However, the coup de grace for COM was probably the disparate type systems
involved. That is, COM represented many data types differently for three separate classes of
developers: C++ developers, Visual Basic developers, and scripting developers. The different
data type systems made it extremely inconvenient to build systems built from different
languages. It could be done, but developers had to be very wary when making calls across such
component boundaries.

.NET and the Common Language Runtime (the CLR) were developed to solve the dead ends
appearing in COM near the end of the last century. When you choose to buy into the .NET
runtime, it's like putting your code in a nice hotel room when it runs. For example, the .NET
runtime loads and manages your code as it runs. Pure memory leaks are a thing of the past
because the runtime collects garbage when necessary. The problem of overrunning array
boundaries disappears because the .NET runtime keeps careful watch over memory and knows
when anything is out of place. In addition, the .NET runtime includes a new security model
making it more difficult to hack into .NET-based software. Finally, the .NET runtime introduces
a new packaging and deployment model, .NET Assemblies, which helps enforce versioning
components.

14
Figure 1: .net Framework

The top layer represents user and program interfaces, and includes Windows Forms, Web Forms,
Web Services, and Application Services. Windows Forms provide a Win32/64-based user
interface. Web Forms provide a Web-based user interface. Web Services are the most
revolutionary program interfaces because they allow programs to communicate over the Internet.
The Internet-based program interfaces, which include Web Forms and Web Services, are
implemented by ASP.NET, which is a built-in component of the .NET Framework.

The middle layer represents the .NET Framework classes, which are universally available across
multiple languages. The usage of these classes is consistent across all languages included in
the .NET Framework.

The base layer represents the common execution platform called the Common Language
Runtime (CLR). This is the most important component of the .NET Framework. The CLR
provides support for multiple languages and allows cross-language inheritance. For example, you
can inherit a class written in Visual Basic from a class written in Visual C++. Thus, with .NET,
the choice of a programming language simply depends on the user's choice. With .NET, it is
possible to create applications by using multiple languages. The multiple-language support is
possible because the CLR provides a common system of data types. In addition, the CLR
performs the memory management and monitors the complete life cycle of objects, while it
tracks objects and handles garbage collection.

Visual Studio .NET (VS.NET) is the first release of the products based on the .NET Framework.
It includes Visual Basic, Visual C++, and C#. VS.NET provides a common Integrated
Development Environment (IDE) for all languages. Therefore, developers always work in a
consistent environment irrespective of the language they use.

15
AIM 3: Familiar with the VS development environment.

Toolbox

Page Layout

Property window

Toolbox contains all the controls of the form. Just drag and drop the control from the
toolbox to the form.

Page Layout gives the design of the form appears at the run time.

Property window, list the basic attributes of the control.

Solution Explorer, it gives the directory structure of the project.

16
AIM 4: Features of .NET
The following are major features of .NET.

Assemblies

An assembly is either a .DLL or .EXE that forms a part of an application. It contains MSIL code
that is executed by CLR. The following are other important points related to an assembly:

1. It is the unit on which permissions are granted.


2. Every assembly contains a version
3. Assemblies contain interfaces and classes. They may also contain other resources
such as bitmaps, file etc.
4. Every assembly contains assembly metadata, which contains information about
assembly. CLR uses this information at the time of executing assembly.
5. Assemblies may be either private, which are used only by the application to which
they belong or Global assemblies, which are used by any application in the
system.
6. Two assemblies of the same name but with different versions can run side-by-side
allowing applications that depend on a specific version to use assembly of that
version.

The four parts of an assembly are:

Assembly Manifest - Contains name, version, culture, and information about referenced
assemblies.

Type metadata - Contains information about types defined in the assembly.

MSIL - MSIL code.

Resources - Files such as BMP or JPG file or any other files required by application..

Common Type System

Common Type System (CTS) specifies the rules related to data types that languages must follow.
As programs written in all languages are ultimately converted to MSIL, data types in all
languages must be convertible to certain standard data types.

CTS is a part of cross-language integration, which allows classes written in one language to be
used and extended by another language.

17
Cross-language Interoperability

.NET provides support for language interoperability. However, it doesn't mean every
program written in a language can be used by another language. To enable a program to
be used with other languages, it must be created by following a set of rules called Cross
Language Specifications (CLS).

Cross-language inheritance is the ability to create a class in C# from a class created in VB.NET.

When an exception is raised by a program written in C#, the exception can be handled by
VB.NET. This kind of exception handling is called cross-language exception handling.

.NET has brought a set of new features which are to be understood by every programmer
developing applications for Windows. There is no way any Windows programmer can
ignore.NET unless he is desperate to be outdated. Microsoft will provide.NET as part of its
operating systems in future releases. It is the platform for programmers. It is not new OS from
Microsoft or a new language. It is the environment for which you develop applications. It is rich
in terms of features.

18
AIM 5: Design a calculator with button and textbox
(with window forms)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace cal
{
publicpartialclassForm1 : Form
{
public Form1()
{
InitializeComponent();
}
string st = "";
string op = "";
float result;

privatevoid button1_Click(object sender, EventArgs e)


{
st += "1";
textBox1.Text = st;
}

privatevoid button2_Click(object sender, EventArgs e)


{
st += "2";
textBox1.Text = st;
}

privatevoid button3_Click(object sender, EventArgs e)


{
st += "3";
textBox1.Text = st;
}

privatevoid button4_Click(object sender, EventArgs e)


{
st += "4";
textBox1.Text = st;
}

privatevoid button5_Click(object sender, EventArgs e)


{
st += "5";
textBox1.Text = st;

19
}

privatevoid button6_Click(object sender, EventArgs e)


{
st += "6";
textBox1.Text = st;
}

privatevoid button7_Click(object sender, EventArgs e)


{
st += "7";
textBox1.Text = st;
}

privatevoid button8_Click(object sender, EventArgs e)


{
st += "8";
textBox1.Text = st;
}

privatevoid button9_Click(object sender, EventArgs e)


{
st += "9";
textBox1.Text = st;
}

privatevoid button10_Click(object sender, EventArgs e)


{
st += "0";
textBox1.Text = st;
}

privatevoid button11_Click(object sender, EventArgs e)


{
op = "+";
result = Convert.ToSingle(textBox1.Text);
st = "";
button16.Enabled = true;
textBox1.Clear();
}

privatevoid button12_Click(object sender, EventArgs e)


{
op = "-";
result = Convert.ToSingle(textBox1.Text);
st = "";
button16.Enabled = true;
textBox1.Clear();
}

privatevoid button13_Click(object sender, EventArgs e)


{
op = "*";
result = Convert.ToSingle(textBox1.Text);
st = "";
button16.Enabled = true;

20
textBox1.Clear();
}

privatevoid button14_Click(object sender, EventArgs e)


{
op = "/";
result = Convert.ToSingle(textBox1.Text);
st = "";
button16.Enabled = true;
textBox1.Clear();
}

privatevoid button15_Click(object sender, EventArgs e)


{
if (op == "+")
{
result = result + Convert.ToSingle(textBox1.Text);
}
elseif (op == "-")
{
result = result - Convert.ToSingle(textBox1.Text);
}
elseif (op == "*")
{
result = result * Convert.ToSingle(textBox1.Text);
}
elseif (op == "/")
{
if (Convert.ToInt32(textBox1.Text) == 0)
{
MessageBox.Show("Cannot Divide By 0");
}
else
{
result = result / Convert.ToSingle(textBox1.Text);
}
}
textBox1.Text = result.ToString();
st = "";
}

privatevoid button16_Click(object sender, EventArgs e)


{
st += ".";
textBox1.Text = st;
button16.Enabled = false;
}
}
}

21
Output:

22
AIM 6:To study Basic Window Controls
Button:

Used to start ,stop or interrupt a process


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace button
{
publicpartialclassForm1 : Form
{
public Form1()
{
InitializeComponent();
}

privatevoid button1_Click(object sender, EventArgs e)


{
MessageBox.Show("U just clicked");
}
}
}
Output:

TextBox:

23
Displays text entered at design time that can be edited by user at run time or changed
programmatically.

privatevoid button1_Click(object sender, EventArgs e)


{
MessageBox.Show(textBox1.Text);
}

Combo Box:

24
Displays a drop down list of items. It is used to store multiple items but allows only
single selection.

Adding items to Combo Box:


We use Add( ) method to add items in Combo Box.

privatevoid Form1_Load(object sender, EventArgs e)


{
comboBox1.Items.Add("India");
comboBox1.Items.Add("USA");
comboBox1.Items.Add("Australia");
}

Removing items from Combo Box:


There are 2 ways to remove items from Combo Box.

1. Remove: This method is used to remove items text wise.


comboBox1.Items.Remove("USA");

2. RemoveAt: This method is used to remove items index wise i.e. from location.
comboBox1.Items.RemoveAt(1);

25
Both these methods will display the following output:

Displaying selected Item:


MessageBox.Show(comboBox1.SelectedItem);

Count:Counts the no of items.


Int32 total = comboBox1.Items.Count();

Clear:Deletes all the items.


comboBox1.Items.Clear();

RadioButton:
Radio buttons are used for single selection only.

26
privatevoid button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
MessageBox.Show(radioButton1.Text);
}
elseif(radioButton2.Checked == true)
{
MessageBox.Show(radioButton2.Text);
}
else
{
MessageBox.Show("Please select a value");
}
}

Checked List Box:


Display the scrollable list of items, each accompanied by a check box. Check boxes are used for
multiple selections.

27
Adding items to Checked List Box:
To add items in Check Box, Click on smart tag & select "Edit Items".

We can also use Add( ) method to add items.


checkedListBox1.Items.Add("Maths");

`Displaying selected Items:

privatevoid button1_Click(object sender, EventArgs e)

28
{
string st="";
for (Int32 i = 0; i < checkedListBox1.CheckedItems.Count; i++)
{
st += checkedListBox1.CheckedItems[i].ToString() + " ";
}
MessageBox.Show(st);
}

Displays a list of items and allow single as well as multiple selection. For multiple selection,
change the property "Selection Mode" of List box to "MultiSimple".

Adding items to List Box:

29
Items can be added in List box in the same way as Checked List Box.
listBox1.Items.Add("Maths");

Displaying selected Items:


privatevoid button1_Click(object sender, EventArgs e)
{
string st="";
for (Int32 i = 0; i < listBox1.SelectedItems.Count; i++)
{
st += listBox1.SelectedItems[i].ToString() + " ";
}
MessageBox.Show(st);
}

AIM7: Introduction to the web form in visual studio.


Before you use any server control to design a Web Forms page, you need to create an
ASP.NET Web Application project. You can create either a Visual Basic .NET or a C# Web

30
Application project, depending on the programming language you want to use.A Web
Application project is always created on a Web server.

The steps to create an ASP.NET Web Application project are as follows:

1. Select Start >Programs >Microsoft Visual Studio 2005 >

Microsoft Visual Studio 2005 to start Visual Studio.NET.

2. Select File >New > Website to open the New dialog box.

3. Select ASP.NET Web Application in the Templates pane.

4. Specify the project name in the Name box, if necessary.

5. Specify the name of the computer where you want to create the application, in the
Location box if necessary, and click OK.A new Web Application project is displayed in the
designer window, as shown in figure below.

When you create a Web Application project, the Application Wizard creates the necessary
project files along with the page file and code behind class file as described:

31
Default.aspx: This page file consists of the user interface for the visual representation of the
Web Forms page. The file has two views, Design and HTML. The default view is Design
view.

Design view: This view represents the user interface for the Web Forms page. You
can place controls directly from the toolbox to the Web Forms page in Design view.
By default, the page layout of the Web Forms page is Grid Layout. This layout
enables you to accurately position controls on the page by using the absolute
coordinates (X,Y) of the page. In addition to Grid Layout, ASP.NET provides
another page layout, which is called Flow Layout. In Flow Layout, you can add text
to the page directly in Design mode. You can change the page layout from the default
Grid Layout to Flow Layout. To do so, right-click the page in Design view and select
Properties from the context menu. Next, in the DOCUMENT Property Pages dialog
box, from the Page Layout list box, select Flow Layout.

HTML view: This view represents the ASP.NET code for the Web Forms page. To
open HTML view, click the HTML tab in the designer. When the Web Application
project is a Visual Basic project or a C# project, the scripting language used in the
HTML page is Visual Basic or C#, respectively.

Default.aspx.cs or Default.aspx.vb: This file consists of the code to implement programming


logic in the Web Forms page. You can view the code file by using the Show All Files icon in
the Solution Explorer window. If the Web Application project is a Visual Basic project, you
use Visual Basic .NET to implement the programming logic and the code file is called the
Default.aspx.vb file. Conversely, if the Web Application project is a C# project, you use C#
to implement the programming logic and the code file is called the Default.aspx.cs file. The
code file (Default.aspx.cs) appears within the WebForm1.aspx node as shown in figure.

AIM 8: Studying the Files in a Web Forms Project

32
The Web form is only one of 11 files Visual Studio .NET generates when it creates a new
Web forms project. The purpose of each of these project files are described below.

File name Contains

AssemblyInfo.vb All of the attributes that are written into the


compiled assembly, including version,
AssemblyInfo.cs company name, GUID, and so on.

The global events that occur in your Web


application, such as when the application starts
Global.asax or ends. You can have only one Global.asax
file per project, and it exists in the root folder
of the project.

Global.asax.vb
The code used in Global.asax. This file is not
shown in Solution Explorer.
Global.asax.cs

The style definitions to use for the HTML


generated by your project. This file appears
Styles.css
only in Visual Basic .NET projects. For Visual
C# projects, you can add it manually.

The settings your Web server uses when


processing this project. These settings specify
Web.config
how errors are reported, what type of user
authentication to use, and so on.

Descriptions of the Web Services that this


project provides. This file is used for dynamic
Projectname.vsdisco discovery of Web services (.asmx files)
included in a Web application. This file is not
shown in Solution Explorer.

WebForm1.aspx The visual description of a Web form.

33
File name Contains

WebForm1.aspx.vb The code that responds to events on the Web


form. By default, this file is not shown in
WebForm1.aspx.cs Solution Explorer.

The Extensible Markup Language (XML)


WebForm1.aspx.resx resources used by the Web form. This file is
not shown in Solution Explorer.

Projectname.vbproj The project files listing the files and settings


used at design time. This file is not shown in
Projectname.csproj Solution Explorer.

Projectname.vbproj.webinfo This file tracks the root virtual folder for the
Web application. This file is not shown in
Projectname.csproj.webinfo Solution Explorer.

34
File name Contains

The primary types of files that youll add to a Web application project are:

File extension Project item Description

Each Web form constitutes an


ASP.NET Web page in your
application. Applications can
have one or many Web forms.
.aspx Web form Web forms have code files
associated with them with the
file extension .aspx.vb. Visual
C# forms have associated
.aspx.cs files.

Web pages that dont have


.htm HTML page server code can appear as
HTML pages in your project.

35
Code that defines objects in
.vb or .cs Class or module your application is stored in
classes.

User controls that are built


from other Web forms and
.ascx Web user control
server controls in Visual
Studio .NET.

Web services that expose


classes for remote execution
.asmx Web service
over a network, such as the
Internet.

Data files that store


.xml XML file information used by your
application.

Schema files that describe the


.xsd XML Schema format and constraints to
apply to stored data.

Formatting rules to apply


.xslt XML Style Sheet
when displaying XML data.

AIM 9: Creating the first web application.


At Design time:

36
Step 1:Go to the File> New Website > ASP.net website then name the website project
name.

Step2: Now drag the Textbox and Button from the toolbox to the web form.

Step3: Double click on the Button to navigate click event of the Button in coding page.

Now in write the coding as follows:


protectedvoid Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = System.DateTime.Now.ToString();

AIM 10: To make familiar with the validation control


in the Web form.

37
Working with ASP.NET validation server controls is no different from working with any
other ASP.NET server controls. Each of these controls allows you to drag and drop it onto a
design surface or to work with it directly from the code of your ASP.NET page. These
controls can also be modified so that they appear exactly as you wishensuring the visual
uniqueness that your applications might require.

The following table describes the functionality of each of the available validation server
controls.

Validation Server Control Description

RequiredFieldValidator Ensures that the user does not skip a form


entry field.

CompareValidator Allows for comparisons between the userss


input and another item using a comparison
operator (equals, greater than, less than, and
so on).

RangeValidator Checks the users input based upon a lower-


and upper-level range of numbers or
characters.

RegularExpressionValidator Checks that the users entry matches a


pattern defined by a regular expression. This
is a good control to use to check e-mail
addresses and phone numbers.

CustomValidator Checks the users entry using custom-coded


validation logic.

ValidationSummary Displays all the error messages from the


validators in one specific spot on the page.

Validation Causes:

Validation doesnt just happen; it occurs in response to an event. In most cases, it is a button
click event.The Button, LinkButton, and ImageButton server controls all have the capability
to cause a pages formvalidation to initiate. This is the default behavior. Dragging and
dropping a Button server control ontoyour form will give you the following initial result:

<asp:Button ID=Button1 Runat=server Text=Button />

If you have multiple buttons on an ASP.NET page, and you dont want each and every button
to initiatethe form validation, you can set the CausesValidation property to False for all the
buttons you wantto ignore the validation process (for example, a forms Cancel button):
38
<asp:Button ID=Button1 Runat=server Text=Cancel CausesValidation=False />

1. The RequiredFieldValidator Server Control: The RequiredFieldValidator control


simply checks to see if something was entered into the HTML form element. It is a
simple validation control, but it is one of the most frequently used. You must have a
RequiredFieldValidator control for each form element on which you wish to enforce a
value-required rule.

2. The CompareValidator Server Control: The CompareValidator control allows you


to make comparisons between two form elements as well as to compare values
contained within form elements to constants that you specify. For instance, you can
specify that a form elements value must be an integer and greater than a specified
number. You can also state that values must be strings, dates, or other data types that
are at your disposal.

3. The RangeValidator Server Control: The RangeValidator control is quite similar to


that of the CompareValidator control, but it makes sure that the end user value or
selection provided is between a specified range as opposed to being just greater than
or less than a specified constant. For an example of this, go back to the text-box
element that asks for the age of the end user and performs a validation on the value
provided.

4. The RegularExpressionValidator Server Control: One exciting control that


developers like to use is the RegularExpressionValidator control. This control offers a
lot of flexibility when you apply validation rules to your Web forms. Using the
RegularExpressionValidator control, you can check a users input based on a pattern
that you define using a regular expression.

5. The ValidationSummary Server Control: The ValidationSummary control is not a


control that performs validations on the content input into your Web forms. Instead,
this control is the reporting control, which is used by the other validation controls on
a page. You can use this validation control to consolidate error reporting for all the
validation errors that occur on a page instead of leaving this up to each and every
individual validation control.

At Design time:

Step 1: Place the text boxes and a button on the web form to implement the validation on them.

Step 2: Now drag the validation control on the web form Validation category from the toolbox.

Step 3: Set the properties of validation control as follows:

Control Name Properties of validation control

39
RequiredFieldValidator Control to validate. <Textbox1>

CompareValidator Control to validate. <Textbox 4>

Control to compare. <Textbox3>

RangeValidator Control to validate. <Textbox5>

Maximum value. <0>

Minimum value. <100>

RegularExpressionValidator Control to validate. <Textbox 2>

Validation Expression <internet mail address>

ValidationSummary Gives the list of validation errors.

Figure: Validation form example

At Run time: Debug the Web Form, Now when you press the button the validation will check
the data inputted is valid or not. If any violation of validation is there then it shows the error.

AIM 11: To study the ADO connectivity in .net


Microsoft tried reinventing data access again with OLE DB (OLE for Databases). OLE DB tied
database development directly to Microsoft's Component Object Model (COM). OLE DB is a set

40
of COM interfaces that exposes a consumer/provider model of accessing data throughout an
enterprise. OLE DB makes no distinction as to whether the data is in a desktop database (like
Access), an enterprise database (like SQL Server or Oracle), or a non-database source (like an
Excel spreadsheet). If developers wanted to expose their data to OLE DB they needed to develop
an OLE DB provider. Unfortunately, development of OLE DB providers proved to be difficult
for all but the most talented C++ developers. Developing data consumers in OLE DB was much
simpler, but tied developers directly to Visual C++, therefore losing any language independence.

In the most recent past, Microsoft developed ActiveX Data Objects (ADO) as a COM wrapper
around OLE DB. ADO was developed specifically to make OLE DB easier to use. Soon after the
release of ADO, the Internet revolution exploded. Developers craved an easy-to-use API to make
their Web sites and Web applications database driven. ADO's script ability fit perfectly into
Microsoft's Internet Information Server (IIS) and Active Server Pages (ASPs). ADO soon
became the lingua franca of Internet Web sites. For small Web sites, ADO shined; it was easy to
understand and simple to code.

ADO.NET is a set of .NET classes that allow for the connection and manipulation of data. .NET
classes are separated into namespaces to help segment their specific functionality. In ADO.NET,
namespaces provide segregation between different parts of the managed provider model.

ADO.NET Data Structures


ADO.NET supports three different ways of accessing database information directly: Commands,
Data Readers, and Data Sets.

Command classes (SqlCommand for the SQL Managed Provider and OleDbCommand for the
OLE DB Managed Provider) can be used directly to retrieve results from database queries.
Command classes always support the IDbCommand interface, which can get a scalar result (the
first column of the first row of a result set) or out parameters of a stored procedure. This sort of
data is retrieved using IDbCommand.ExecuteScalar() and IDbCommand.ExecuteNonQuery().

DataReaders (SqlDataReader and OleDbDataReader) are classes that provide something similar
to ADO's Recordset using a forward-only cursor. In ADO, we need to be careful and make sure
we call MoveNext() and check for the end-of-record indicator. The DataReader uses a less error-
prone system that just requires that you read the current record into memory and check for the
end-of-record indicator in one step. For cases where you will need to simply get a query and
spew out the results, the DataReader is the perfect vehicle. In fact, Microsoft uses DataReaders
within the managed providers' DataAdapters to fill DataSet.

The DataSet looks much like an OLE DB RowSet or an ADO Recordset. The DataSet, however,
is much more than that. It is a complex data structure that allows for an in-memory

41
representation of almost any relational model. The DataSet holds a collection of tables that can
have optional DataRelations, Constraints, or even ForeignKeyConstraints.

Example:

At Design time:

Place Gridview and access DataSource on the web form.


Click on the top right corner of the DataSource, and click on configure data source.
Follow the wizard and chose the relevant data from the table.
Now click on the top right corner on the Gridview and click on Choose data source and
select the data source configured in the second step.

Now run the program by pressing the F5 key.

(b) To show data in DetailView :


using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

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

protected void Page_Load(object sender, EventArgs e)

if(!Page.IsPostBack)

42
bind();

private void bind()

SqlDataAdapter adp = new SqlDataAdapter("select * from products",


ConfigurationManager.ConnectionStrings["cn"].ConnectionString);

DataSet ds = new DataSet();

adp.Fill(ds);

DetailsView1.DataSource=ds;

DetailsView1.DataBind();

43

S-ar putea să vă placă și