3 5
3 5
3 5
5 With C#
265.03.01
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
Index
Module 1 Module 2 Module 3 Module 4 Module 5 Module 6 Module 7 Module 8 Module 9 Module 10 Module 11 Module 12 Module 13 Module 14 : Introduction to ASP.NET 2008 : WebForm Controls : Validation Controls : ADO.NET Architechture : DataSource Controls : DataBound Controls : Navigation Controls : Themes and Master Pages : State Management : Caching : Security : Login controls and Membership API : Configuration, Administration, and Deployment : Web Services
2
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
Browser (Client)
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
IIS (Internet Information Services): It is a web server required to host ASP.NET web applications. The .NET Framework registers ASP.NET in IIS automatically if IIS already exists on the system. If not then we require a tool called as aspnet_regiis.exe to register ASP.NET in IIS. A virtual directory exists which is mapped to a physical directory Drive:\Inetput\wwwroot.
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
To create a new website in Visual Studio 2008 IDE, go to File -> New -> Web Site. The above dialog box will be displayed. We need to specify the following things: a)Location: The location option allows to choose the types of website can be created with .NET. a) File System:This is the option where built in Web Server provided by microsoft with Visual Studio 2008 IDE is used. The web application is directly created in file system folders. One need not to have IIS installed on the machine. But this will run the web site locally. It can not run it on other systems. It is meant for testing pages. b) HTTP: This option creates a website on local IIS Web Server. The pages and folders are stored in to the folder created under the default web site which is physically mapped to [drive]:\Inetpub\wwwroot. We need not to do any configuration settings for ASP.NET websites with IIS as they are automatically done by Visual Studio. One requires administrative privileges to run the website on an IIS Web Server. If we want to create our websites in the folder on a different drive then we can create a virtual directory that maps to the physical folder. c) FTP: One can create a web site on FTP server on which read / write permissions are available. b)Language: We can choose either Visual Basic or Visual C# to create a website.
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
When a web site will be created, this is the way a web page will be displayed. By default, a web page or a web form with the name Default.aspx will get created. There are three views of a web page. 1)Design View where one can drag and drop controls available from the Tool Box and design the Web Page. 2)Source View where the HTML Source of the web page is available. Whatever control is dropped on the web form, html source for the same is generated automatically in the source view. 3)Split View that divides the screen in two parts showing both the views. Html source of a web page contains a Page Directive. The following is the code for it. <%@Page Language="C# AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> Explanation for Page Directive: The @Page directive specifies the settings used by the web page. Language Attribute: Specifies the language used when compiling all the inline codes and code declaration blocks. Only one language can be used per page. We can use any .NET Framework supported language. AutoEventWireup attribute: This tells that whether page events are wired up automatically or not. By default this attribute has true value. CodeFile Attribute: This attribute refers to the code behind file associated with the page. It takes the name of code behind file as a value. It is used together with Inherits attribute. Inherits Attribute: It defines the code behind class for a page to inherit.
Design View
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
Code View
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
The code file will be carrying the name as Default.aspx.cs for Csharp and Default.aspx.vb for Visual Basic.
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
10
To run the web site either go to Debug > Start Debugging or press F5 key. When we will run the web application for the first time, we will get this dialog box asking us to enable the debugging for the web site. By default, it is disabled in the Web.Config file. To enable the first option is selected and to run the website without debugging second option is selected. We can run our website without debugging by pressing Ctrl+F5 combination key.
10
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
11
11
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
12
(Source: Module1\02SingleFileCodeModelEg.aspx) In the Single File Code model the HTML Markup, Server Control markup and code are placed into one single file. This file has the extension .aspx.
12
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
13
In this model, HTML markup and Server Control markup appears in .aspx file and the code appears in .aspx.cs or .aspx.vb file.
13
Request
Client sends a Request to the Server.
Request
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
14
14
Response
Server gives response to the clients
Response
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
15
15
ASP.NET Compilation
Multiple languages are supported. Automatic Compilation. Deployment
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
16
Compilation: ASP.NET applications are compiled into MSIL (Microsoft Intermediate Language) and files are generated with the extensions .dll. It has certain features: Multiple Language Support: To create ASP.NET applications, we can use languages like VB, C#. We can also have code written in different languages into one application. They get compiled into different assemblies. To separate the code written in different languages, we need to create different sub folders for different languages. ASP.NET gives a folder called as App_Code to store all code files for the application. Automatic Compilation: The application code and any other dependencies gets automatically compiled when user requests the page for the first time. It creates an assembly for each application directory and one for the main directory. Separate assemblies are created for the code written in different languages. Deployment: ASP.NET provides PreCompilation option to compile the website before it has been deployed or after it gets deployed but before the first user requests it. Precompilation provides certain benefits like it improves the performance as the compiled version already exists when the first user requests it. It also helps to debug the errors before it is requested which otherwise would have arrived on the first request.
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
17
Application Folders available with ASP.NET: App_Browsers: It contains browser definitions that allows users to identify different browser capabilities. App_Code: It stores the source code i.e. business objects created with either VB or C#. App_Data: It is used to store data files like .mdb for MsAccess or .mdf for SQL Server to work with SQLExpress, the in built data server. App_GlobalResources: Stores resource files such as .rsex or .resources files that are compiled in to assemblies with global scope. App_LocalResources: It stores resource files for individual pages or user controls. App_Themes: Themes are used to give consistent look to the controls on the web pages. They store .skin files which contain formatting instructions for different applications. App_WebReferences: Contains references to web services. Bin: Contains compiled assemblies for controls, components or the code that is referred into the assembly.
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
18
18
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
19
HTML Controls: Represents HTML elements in the form of a control. Used when the HTML pages are migrated. Especially used when old version ASP application is migrated to ASP.NET. All HTML control classes reside under the namespace called as System.Web.UI.HtmlControls. Web Server Controls: They are feature rich controls than HTML controls. They are used when we want to bind the data, creating templates etc. The classes for Web Server Controls are available in the namespace System.Web.UI,WebControls. User Controls: It is used to create a custom control which provides reusability.
19
IsPostBack Property
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
20
(Source Code: Module2\01IsPostBack.aspx) When the user submits the data and sends it back to the server is called as PostBack. And when the page is posted back, the page is generated again and it fires load event. The code written for this event is executed again. Due to this the data loss may happen. IsPostBack property is given by the Page class. This is used to check whether the page is loaded for the first time or because of PostBack to the server. It is a boolean property. Using this property we can decide upon whether the code will be executed only once on the first load or on every post back. Controls taken for design: Label : It is used to display a static text on the page. Button : Has the submit behavior. When user clicks the button, the page is sent to the server automatically.
Controls Label
Properties ID Text
20
21
Login Example
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
22
(Source: Module2\02LoginPageEg.aspx) Options for TextMode property: SingleLine: Default mode Multiline: Multiple lines of text can be written. Text box will appear with the scroll bar. Password: To encrypt a password by some character. This character is browser dependent.
Value lblUserName User Name lblPassword Password lblDisplay (Blank) btnLogin Login
Label
Button
22
23
List Controls
DropDownList Control Example (AutoPostBack Property)
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
24
(Source: Module2\03DropDownListEg.aspx) We have got another set of controls called as List Controls that contains: DropDownList: We can store a list of items but at a time only one item is displayed. ListBox: We can store a list of items. We can also select multiple items with the help of Shift and Ctrl key combinations. CheckedListBox: Listbox appear with check boxes. RaioButtonListBox: Listbox appear with radio buttons. By default, all list controls contain one property called as EnableAutoPostBack to automatically send the selected data to the server to get the desired output. The event that is attached with list controls is SelectedIndexChanged which gets fired on selection of an item in the list.
DropDownList
24
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
25
25
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
26
(Source: Module2\04ListBoxEg.aspx) ListBox control is used to display multiple items. These items can be selected using Shift and control key combinations.
Value lblSelectBooks Select Books lstBooks Multiple btnSelect Select Books lblDisp (Blank)
26
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
27
27
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
28
28
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
29
(Source: Module3\01ValidationControlsEg.aspx) Validation Controls: In web applications, the user inputs is accepted only when some condition is satisfied. To check whether the inputs are according to those conditions, we need to put validations. This task has become easy as Asp.Net introduces Validation Controls. Validation Controls provide simple mechanism to check some standard validations such as, checking whether user has given the value for the fields or kept them empty, the length of the password is between some range, email id is according to the standard mail address or not etc.. Let s discuss the controls in detail. 1)RequiredFieldValidator: The type of validation which is performed by this control is Required Entry. This ensures that user doesnt skip the field from providing input. The user input is compulsory. The slide shows that all the fields are compulsory. 2)CompareValidator: The type validation this control performs is Comparison To a Value. The value entered by the user for one field is compared with the value of the other field, the constant or a specific data type. In the slide, the field for Confirm Password is compared with the field for Password as retyped password need to be the same as of actual password. 3)RangeValidator: The type of validation performed by this control is Range Checking. This controls checks whether the value entered by the user is between the specified range or not. The range can be within pair of numbers or alphabets or dates. 4)RegularExpressionValidator: The type of validation performed by this control is Pattern Matching. This control matches the value entered by the user with that of the pattern specified. Patterns can be specified for email addresses, site addresses, postal codes, telephone numbers etc. 5)CusomValidator: The type of validation performed by this control is User Defined. In the above all the controls the validation logic is already given but for custom validator the code of logic need to be written.
29
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
30
(Source Code: Module3\01ValidationControlsEg.aspx.cs) Validation Summary: This control is used to display all the error messages either in the form of a list or in the form of a message box. It tracks the error message property of all the validation controls. We have two properties: Show Summary and Show Message Box.
Control RequiredFieldValidator
Property ID ControlToValidate ID ControlToValidate ControlToCompare ID ControlToValidate MaximumValue MinimumValue ID ControlToValidate ValidationExpression ID ControlToValidate ClientValidationFunction
Value rfvUserName txtAge cvConfirmPassword txtConfirmPwd txtPassword rvAge txtAge 60 18 revEmail txtEmail \w+([-+.']\w+)*@\w+([.]\w+)*\.\w+([-.]\w+)* csvPassword txtPassword Validate
CompareValidator
RangeValidator
RegularExpressionValidator
CustomValidator
30
Validation Group
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
31
(Source: Module3\02ValidationGroupEg.aspx) Validation Group: The example in the slide accepts user name and password and also an email id if the user has forgot the password. RequiredFieldValidatior is kept for all the three fields. If user forgets to provide user name or password then the validation happens not only for these two controls but also for the one that accepts mail id. Which should not be the case. To separate validations, we need logical groupings for validation. We need to set the ValidationGroup property for the textboxes, RequiredFieldValidators and button to login together to a particular group. And textbox for email id, RequiredFieldValidator and button to get the password into another group. Benefits of Validation Controls: 1.Validation controls are rendered on the Client Side. 2.One need not to write a code. The logic is provided by the controls itself.
31
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
32
32
Introduction to ADO.NET
ADO.NET is a technology that allows access to the data. It provides a set of classes to access database either in connected environment or disconnected environment. Strong XML Integration provided.
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
33
33
Connected Model
Database
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
34
Connection: It provides a connection with the database. It provides following methods and properties. Properties / Methods of Connection: Methods: Open(): To open the connection with the database to allow to perform operations with the data. Close(): To close the connection with the database. Properties: ConnectionString: We need to set this property to provide the following information. Server or DataSource Database or Initial Catelog User id and Password for SQL Authentication Integrated Security for Windows Authentication Provider Name (Optional for Sql Server Provider) Data Command: These are the commands or quries to be fired on the database to perform operations with the database. The following properties and methods are given by command classes. Properties / Methods of Data Command: Methods: ExecuteNonQuery(): To execute manipulation statements on the database. It returns an integer value. ExecuteScalar(): It is used to return a single value. When a result set is retrieved, this method returns First Row First Column value. ExecuteReader(): Used to return a Data Reader reference to get Read Only Forward Only access to the data.
34
Disconnected Model
Provider Client Application
Connection
Database
Data Adapter
DataSet
DataSet
XML
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
35
Properties of DataCommand: CommandText: To specify the query statement to be fired on the database. CommandType: To mention which type of command to be fired on the database. It takes 3 options. Text option is used to specify the query statement and it is the default. StoredProcedure option is used to specify that the command to be fired is a stored procedure and not a simple text. TableDirect is used to specify the name of the table for which select statement is generated automatically. Data Reader: It provides Read Only Forward Only access to the data. It stores only one row at a time into the memory and retrieves the data as soon as requested. It speeds up the performance. Methods: Read(): Advances the reader to the next record. Disconnected Model: This is an Offline access to the data where the client is disconnected from the database. There is no direct connection between an application and the database. Data is accessed through DataSet. Dataset is an in - memory representation of data or cached set of data. It stores the data in the form of DataTable Objects and Datatable in turn stores it in the form of DataRow and DataColumn objects. DataAdapter: It is an intermediary between Dataset and the Application. It opens the connection with the database, fetches the data by firing commands, brings the data into the dataset and closes the connection with the database. It also updates the data with corressponding changes made into the dataset.
35
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
36
Properties / Methods of DataAdapter: Methods: Fill(): This method is used to open the connection with the database, fire the command on the database, retrieve the data into the dataset and close the connection with the database. Update(): This method is used to reflect the changes made in the dataset to the Database. For this it requires corresponding commands to be fired. It uses certain properties to achieve this task. Properties: SelectCommand, InsertCommand, UpdateCommand, DeleteCommand: These properties are required to be set if we want to perform modifications to the database through data adapter.
36
Disconnected Code
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
37
(Source: Module4\01DisconnectedCode.aspx)
37
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
38
38
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
39
39
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
40
40
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
41
41
Connected Code
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
42
(Source: Module4\02ConnectedCode.aspx)
42
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
43
One can specify connection string in Web.config file. It is an XML based configuration file for web application. The benefits are: 1.We need not write it again on every page. 2.If we want to make any changes, they need to be done at one place i.e. config file only. They will be reflected automatically at every places. To refer to connection string written in config file, we need to have a reference to System.Configuration namespace in code file. The class used is ConfigurationManger which contains ConnectionStrings collection. From this collection, we can identify each connection string by the name of it.
43
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
44
44
Data Controls
Data Controls
DataBound Controls Grid View Details View Form View Repeater DataList
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
45
Data Controls are divided into two: DataBound controls: Helps to bind i.e. associate the controls with data DataSource Controls: These controls encapsulates the logic for connecting to data, fetching and manipulating data and then bound to DataBound controls.
45
46
SqlDataSource
Allows to work with SQL Server, Oracle, SQLExpress, ODBC and OleDb databases. It is a layer between the bound controls and the database.
Select() Grid View Control Insert() DataBase Update() Delete()
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
47
The SqlDataSource control uses ADO.NET classes to work with the database. It can work with any databases that are supported by ADO.NET. It takes ConnectionString information, the query statement or a stored procedure to be fired. During rin time it opens the connection automatically, fires the required statement, performs corresponding operation on the database and closes the connection. Connecting SqlDataSource to a Database: We need to set the ProviderName, ServerName, Authentication (Windows or SQL), and Database name to connect. This connection can be saved in the Web.Config file. Issuing Data Commands with the SqlDataSource Control We can set the commands such as Select, Insert, Update, Delete with SqlDataSource. We have got two options to set this. Either we select the table from the list of tables and then select the columns or we write the query by our own. The datasource control executes the commands when corresponding Select(), Insert(), Update() and Delete() method is called. When the data is bound to the control, it automatically calls the select method. Even after performing manipulation operations, it calls the select method. We can also call these method explicitly. Returning DataReader or DataSet: It can either return a datareader or a dataset for which we need to specify a property called as DataSourceMode. DataSet allows filter, sorting , paging, caching, manipulation to the data whereas datareader provides read only, forward only access to the data. Filtering with SqlDataSource control: We can filter the data by providing a where clause with the datasource control. Sorting with SqlDataSource Control: We can sort the data by providing an Order By clause with the control.
47
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
48
(Source: Module4\03SQLDataSourceControlEg.aspx) Steps to Configure SqlDataSource Control 1) Drag an SqlDataSource Control. From the smart tag, select Configure Data Source.
48
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
49
(Source: Module4\03SQLDataSourceControlEg.aspx) 2) We need to choose the data connection. Click New Connection button.
49
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
50
(Source: Module4\03SQLDataSourceControlEg.aspx)
3) We need to specify the Data Source to which we want to connect. For our example, we will select Microsoft Sql Server and .NET Framework Data Provider for SQL Server which is default.
50
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
51
(Source: Module4\03SQLDataSourceControlEg.aspx) 4) After selecting the data source, we need to give the server name to connect. We also need to select the way of loging on to the server i.e. using Windows Authentication or SQL Server Authentication. Then provide the database name and test the connection.
51
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
52
(Source: Module4\03SQLDataSourceControlEg.aspx) 5) Once the connection string is generated, we need to save the connection string to the Web.Config file for reusability purpose.
52
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
53
(Source: Module4\03SQLDataSourceControlEg.aspx) 6) We will select the table and columns of that table by using the option : Selecting columns from a table or view. On this wizard page, to retrieve Unique rows, we need to check the checkbox for Return only unique rows. We can provide Where clause to filter the data. We can order the data by columns using Order By option. By clicking on Advanced, we can generate Insert, Update and Delete queries.
53
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
54
54
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
55
(Source: Module4\03SQLDataSourceControlEg.aspx) 8) Finally the data would be bound to the control through SQLDataSource control. We will get checkboxes like Enable Paging, Enable Sorting and Enable Selection. If we have generated manipulation statements, then we will also have options like Enable Editing, Enable Deleting. This is explained in the next two slides.
55
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
56
(Source: Module4\04SQLDataSourceControlEg.aspx) Check Generate INSERT, UPDATE and DELETE statements checkbox to generate manipulation statements for the data.
56
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
57
(Source: Module4\04SQLDataSourceControlEg.aspx) The gridview control provides the smart tag where in we have the properties to enable editing, and enable deleting for performing modifications through grid view.
57
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
58
(Source: Module4\05ModifyingDataWithSQLDataSource_Code.aspx) In the above example, we are going to modify the data using SqlDataSource. We will be binding it to list controls and textbox controls. For this purpose, we need to set certain properties with SqlDataSource control such as SelectQuery, UpdateQuery, InsertQuery, and DeleteQuery.
58
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
59
(Source: Module4\05ModifyingDataWithSQLDataSource_Code.aspx) We need to set the queries parameters to corresponding control ID to which we will pass during run time.
59
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
60
(Source: Module4\05ModifyingDataWithSQLDataSource_Code.aspx) For updating the data, we need to set UpdateQuery property of SqlDataSource control. We need to set parameters to corresponding controls.
60
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
61
61
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
62
62
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
63
(Source Code: Module4\05ModifyingDataWithSQLDataSource_Code.aspx.cs) After setting the parameters, we need to call methods to fetch as well as manipulate the data. Here, in the SelectedIndexChanged event handler, Select() method on datasource. It selects the required rows from the database. It takes one parameter of type DataSourceSelectArguments and returns IEnnumerable which has been typecasted to dataview and assigned to a dataview reference. In the btnInsert_Click event handler, we have called Insert() method on datasource. It returns an integer. It inserts a new record into the products table. After inserting records, we are calling DataBind() method on the dropdownlist so as to refresh the control with newly generated productid.
63
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
64
(Source Code: Module4\05ModifyingDataWithSQLDataSource_Code.aspx.cs) In the btnUpdate_Click event handler, we are calling Update() method on datasource to update values for selected productid. In the btnDelete_Click event handler, we are calling Delete() method on datasource to delete values for selected productid. We are again calling DataBind() method on the dropdownlist so as to reflect the changes. We also have created one user defined private method that counts total number of products.
64
ObjectDataSource
It is used in N-tier approach. Communicates with objects or collection of objects.
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
65
Object Data Source is introduced to provide a Business Logic Layer to the application. With other datasource controls, we can achieve two tier architecture where presentation layer has too much information about the database. To isolate presentation layer from the data layer, we will create set of classes which will communicate with the database, bring the data to the application, perform manipulation. We are going to create a class that contains all the methods to communicate with the database. In our example, we are going to create a class that contains all the information related to the fields of the database by creating properties and another class that contains all the manipulation methods. When we create properties for the fields of the database their name should match with the names of the fields in database.
65
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
66
(Source: Module4\06ObjectDataSourceControlEg.aspx) We have taken DetailsView control to be bound to ObjectDataSource control. DetailsView control displays details of Single record at a time. It also allows paging, modifying, inserting the data. Lets create the classes. We need to keep certain things in mind before we create business objects for ObjectDataSource control. They are as follows: I.We will write four methods for fetching, inserting, updating and deleting data. II.All the methods need to be public and static. III.Method for selecting data would return a DataSet or DataReader IV.Other methods would return an integer but it is optional. We will create three classes: one for Connection, one for storing all the fields of the table i.e. Employee and the last one to store methods to work with the data i.e. EmployeeStore.
66
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
67
67
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
68
68
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
69
69
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
70
70
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
71
71
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
72
72
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
73
73
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
74
74
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
75
(Source: Module4\06ObjectDataSourceControlEg.aspx) To configure the ObjectDataSource, we need to choose the class that contains all the manipulation methods for the option Choose your business object.
75
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
76
(Source: Module4\06ObjectDataSourceControlEg.aspx) The next step is setting the methods. In the above slide we are selecting getData() method for Select operation.
76
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
77
(Source: Module4\06ObjectDataSourceControlEg.aspx) Here we are selecting updateData(Employee emp) for Update operation.
77
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
78
(Source: Module4\06ObjectDataSourceControlEg.aspx) Here we are selecting insertData(Employee emp) for Insert operation.
78
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
79
(Source: Module4\06ObjectDataSourceControlEg.aspx) Here we are selecting deleteData(Employee emp) for Delete operation. Click on fininsh and execute the code. After we are finished with the configuration, we need to bind this ObjectDataSource control to the desired control.
79
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
80
(Source: Module4\06ObjectDataSourceControlEg.aspx)
80
DataSet
DataSet is an in memory representation of data. It contains multiple tables and information about the tables such as constraints, order, relations if any. To populate the dataset a DataAdapter is used.
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
81
DataSet: It is an in memory representation or cached set of the data. It can store data from multiple sources i.e. a dataset is not provider specific. DataSet contains tables in the form of a collection which can be related by using DataRelation objects. We can also enforce integrity like UniqueConstraint or ForeignKeyConstraint. DataSet provides disconnected model. DataAdapter is used to fill the data of the database in to the dataset. It stores data in xml format.
81
DataTable
DataRelation
DataRow
DataColumn
Constraints
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
82
DataSet Object Model: DataSet stores tables and relations amongst the tables in the form of collections called as DataTableCollection and DataRelationCollection. Each table in the collection is represented by DataTable objects and each relation in the collection is represented by DataRelation objects. DataTable stores rows and columns in DataRowCollection and DataColumnCollection. To refer to a single record and a column in the collection, we have DataRow and DataColumn objects. Apart from rows and columns we can also enforce constraints like primary key, unique key, foreign key on the data.
82
Types of DataSet
DataSet
Typed DataSet
UnTyped DataSet
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
83
83
Filtering DataSet
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
84
(Source : Module4\07Filter_DataSet.aspx) Applying Filters on DataSets: Once the data is retrieved into the dataset, the connection to the database will get closed. Now, if we want to have filtered data we again are required to fire queries to the database and retrieve a different set of data every time. Instead of firing multiple queries on the database, we can apply filters on DataSet as well. We have got two methods to filter the dataset data. One is through Select method and the other is through DataView object. Select method returns an array of DataRow objects for specified criteria and sort order. DataView allows different views of the data stored in the data table. It is similar to the database views where we can apply different filter and sort criteria and can have different views of data. However, it has got certain limitations like it cant have data from joined tables, cant append computational column, cant exclude any column existing in the source table.
84
Code
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { SqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"]. ConnectionString); SqlCmd = new SqlCommand("select categoryid from categories", SqlCon); SqlCon.Open(); SqlDr = SqlCmd.ExecuteReader(); while (SqlDr.Read()) { ddlCategories.Items.Add(SqlDr["CategoryID"].ToString()); }
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
85
85
Code
} } private DataTable GetData() { SqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"]. ConnectionString); SqlCmd = new SqlCommand("select productid, productname, unitprice, categoryid from products", SqlCon); SqlCon.Open(); SqlDr = SqlCmd.ExecuteReader(); dt = new DataTable(); dt.Load(SqlDr); return dt; }
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com 86
86
87
88
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
89
(Source Code: Module4\08SqlCommandBuilder.aspx) Command Builder: Command Builder is used to generate manipulation commands automatically. To generate the commands it requires select statement which must retrieve a primary key or a unique key column. Command Builder is required in the case when the dataset is generated during run time. This dataset doesnt have any information related to the manipulation commands. If we want to manipulate such dataset and the database later through such dataset then we need to provide the statements of modifications during run time. Command Builder suffices this purpose. System.Data.SqlClient namespace provides SqlCommandBuilder class to generate the commands. The constructor of this class takes SqlDataAdapter reference as a parameter.
89
Add Columns
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
90
(Source Code: Module4\08SqlCommandBuilder.aspx) For working with command builder, we need to add bound columns as well as command fields to the gridview control. To add it, Select the grid and from the smart tag select Edit Columns option. The dialog box displayed in the slide will appear. Select a BoundField from Available Fields list and click on add button. It will get added to the selected fields list. Select the field, Property page will be displayed. We need to specify the following properties: HeaderText: The heading of the column DataField: The name of the field which will be mapped with that of database column during run time. This has to be exactly same as the database field. ReadOnly: True ( For the columns which dont take part in modifications e.g. Primary Key Column). We also need to code the events gerated by the gridview control for manipulation. In the above example, as we will be only updating the data, we have generated RowEditing, RowUpdating and RowCancelingEdit
90
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
91
91
92
93
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
94
94
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
95
(Source: Module4\09MasterDetailEg.aspx)
95
96
97
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
98
(Source: Module4\10SPIN.aspx) We can also use stored procedure to manipulate the data. A stored procedure can accept data as well as can return data. When a procedure accepts some values, they are called as input parameters. It can also return data either as output parameters or return values.
98
(Source: Module4\10SPIN.aspx.cs) Create the following procedure in SQLServer Database. create proc InsertEmployee @fname nvarchar(10), @lname nvarchar(20) as employees(firstname, lastname) values (@fname, @lname) insert into
99
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
100
(Source: Module4\10SPIN.aspx.cs)
100
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
101
(Source: Module4\10SPIN.aspx.cs)
101
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
102
(Source: Module4\10SPIN.aspx.cs)
102
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
103
(Source : Module4\11SPOUT.aspx)
103
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
104
(Source : Module4\11SPOUT.aspx.cs) Create the following procedure in SQLServer Database: create proc GetTotalPrice @catid int, @total money output as select @total=sum(unitprice) from products where categoryid=@catid
104
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
105
(Source : Module4\11SPOUT.aspx.cs)
105
(Source : Module4\11SPOUT.aspx.cs)
106
Transactions
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
107
(Source: Module4\12Transaction.aspx) Transactions: It is a set of operations, wherein either all the operations must be successful or must fail so as to maintain the consistency of the database. For e.g. if we are transferring amount from one account to the other, then the amount should be added to one and deducted from the other. If deduction is successful but addition is not then the data will go in inconsistent state. Therefore we need to put these operations inside a transaction so that if one is successful but the other is not then the entire transaction will fail. Transactions follows certain guidelines i.e. ACID properties. Atomicity: Isolation: Durability: hard disk data can All the operations in the transactions should either succeed or fail together. All operations happen independently. None of the operations affect each other. Whenever manipulation happen to the data, the modified data is stored on some storage like before the transaction is declared as successful. So, if the transaction fails then the original be brought back to the database.
When all the operations in the transactions are successful they are committed i.e. save permanently in the database. If they fail then they are rolled back i.e. discarded permanently.
107
Example
SqlConnection sqlcon; SqlCommand sqlcmd; SqlTransaction sqltran; SqlDataReader sqldr; decimal curBal = 0.0M, dbBal=0.0M; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"]. ConnectionString); sqlcmd = new SqlCommand("select accid from accounts", sqlcon);
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
108
(Source: Module4\12Transaction.aspx.cs) Create the following table for the above example: Create Table Accounts ( AccID int Identity(1,1) Primary Key, AccName nvarchar(50) NOT NULL, Balance money NOT NULL )
108
Example Contd
sqlcon.Open(); sqldr = sqlcmd.ExecuteReader(); while (sqldr.Read()) { ddlFromAccID.Items.Add(sqldr["AccID"].ToString()); ddlToAccID.Items.Add(sqldr["AccID"].ToString()); } sqldr.Close(); sqlcon.Close(); } }
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
109
(Source: Module4\12Transaction.aspx.cs)
109
Example Contd
protected void btnTransaction_Click(object sender, EventArgs e) { sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"]. ConnectionString); sqlcmd = new SqlCommand("select balance from accounts where accid="+ddlFromAccID.SelectedItem.Text ,sqlcon); sqlcon.Open(); sqldr = sqlcmd.ExecuteReader(); curBal = decimal.Parse(txtAmtTransfer.Text); sqldr.Read(); dbBal=decimal.Parse(sqldr["Balance"].ToString()); if (dbBal < curBal) {
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com 110
(Source: Module4\12Transaction.aspx.cs)
110
Example Contd
Response.Write("Insufficient balance. Cannot transfer"); sqldr.Close(); sqlcon.Close(); } else { try { //sqlcon.Open(); sqldr.Close(); sqlcmd = new SqlCommand(); sqlcmd.Connection = sqlcon; sqlcmd.CommandText = "select balance from accounts where accid=" + ddlToAccID.SelectedItem.Text;
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
111
(Source: Module4\12Transaction.aspx.cs)
111
Example Contd
sqldr = sqlcmd.ExecuteReader(); sqldr.Read(); decimal toBal = decimal.Parse(sqldr["Balance"].ToString()); sqldr.Close(); sqltran = sqlcon.BeginTransaction(); sqlcmd.Transaction = sqltran; sqlcmd.Connection = sqlcon; sqlcmd.CommandText = "Update accounts set balance="+ (dbBal - decimal.Parse (txtAmtTransfer.Text)) + " Where accid=" + ddlFromAccID.SelectedItem.Text; sqlcmd.ExecuteNonQuery(); sqlcmd.CommandText = "Update accounts set balance="+(toBal + decimal.Parse (txtAmtTransfer.Text)) + " where accid=" + ddlToAccID.SelectedItem.Text; sqlcmd.ExecuteNonQuery(); sqltran.Commit();
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
112
(Source: Module4\12Transaction.aspx.cs)
112
Example Contd
Response.Write("Transfer of funds successful"); } catch (Exception ex) { Response.Write("Error occurred: "+ex.Message); sqltran.Rollback(); } finally { sqlcon.Close(); } } }
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
113
(Source: Module4\12Transaction.aspx.cs)
113
Data Relations
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
114
(Source: Module4\13DataRelations.aspx) Data Relations: Datasets can represent relational data by using DataRelation object. Whenever a database is created manually, we need to add relations between the tables by creating an object of DataRelation class.
114
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
115
(Source: Module4\13DataRelations.aspx.cs)
115
(Source: Module4\13DataRelations.aspx.cs)
116
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
117
(Source: Module4\13DataRelations.aspx.cs)
117
Bulk Copy
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
118
(Source: Module4\14BulkCopy.aspx) Bulk Copy: It help copying data from various sources such as text files, csv files, XML, or other databases to SQL server. Whenever we copy the data, the table in which we want to copy should exist into the database. The data types of the columns in the table should match with that of the data to be copied. The class used for this purpose is SqlBulkCopy.
118
Code
SqlConnection sourcecon, destcon; SqlCommand cmd; SqlDataReader dr; string constring; protected void btnBulkCopy_Click(object sender, EventArgs e) { constring = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionStri ng; sourcecon = new SqlConnection(constring); destcon = new SqlConnection(constring);
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com 119
(Source: Module4\14BulkCopy.aspx.cs)
119
(Source: Module4\14BulkCopy.aspx.cs)
120
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
121
(Source: Module4\14BulkCopy.aspx.cs)
121
Batch Updates
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
122
(Source: Module4\15BatchUpdates.aspx)
122
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
123
(Source: Module4\15BatchUpdates.aspx.cs)
123
(Source: Module4\15BatchUpdates.aspx.cs)
124
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
125
(Source: Module4\15BatchUpdates.aspx.cs)
125
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
126
(Source: Module4\15BatchUpdates.aspx.cs)
126
(Source: Module4\15BatchUpdates.aspx.cs)
127
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
128
(Source: Module4\15BatchUpdates.aspx.cs)
128
MARS
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
129
(Source: Module4\16MARS.aspx)
129
MARS Example
SqlCommand cmd; SqlConnection con; SqlDataReader orderdr; protected void Page_Load(object sender, EventArgs e) { con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString.To String()); cmd = new SqlCommand("select customers.companyname, customers.contactname, orders.orderid, orders.orderdate, orders.requireddate, orders.shippeddate from orders,customers where orders.customerid=customers.customerid order by customers.companyname, customers.contactname", con);
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
130
(Source: Module4\16MARS.aspx.cs) MARS: It stands for Multiple Active Result Sets. For activating MARS feature, we just need to add one attribute to the connection string i.e. MultipleActiveResultSets=True. <connectionStrings> <addname="constr InitialCatalog=Northwind; connectionString="DataSource=PSPL2939\SA1; MultipleActiveResultSets=True;
130
(Source: Module4\16MARS.aspx.cs)
131
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
132
(Source: Module4\16MARS.aspx.cs)
132
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
133
(Source: Module4\16MARS.aspx.cs)
133
Provider Factories
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
134
(Source: Module4\17ProviderFactories.aspx) Provider Factories: It provides set of abstract classes that can work with multiple providers. It is used to write common code independent of any specific provider. The namespace that provides factory classes is System.Data.Common. The class DbProviderFactory is used to expose different providers. All the providers that are exposed by DbProviderFactory have their configuration information into the Machine.config file. We need to pass provider information to the instance of DbProviderFactory class, it determines the strongly typed connection object and returns it.
134
(Source: Module4\17ProviderFactories.aspx.cs)
135
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
136
(Source: Module4\17ProviderFactories.aspx.cs)
136
(Source: Module4\17ProviderFactories.aspx.cs)
137
138
(Source: Module4\17ProviderFactories.aspx.cs)
138
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
139
(Source: Module4\18XML.aspx)
139
XML File
<?xml version="1.0" encoding="utf-8" ?> <Books> <Book bookid="1"> <Name>Professional Asp.Net 2.0</Name> <Description>Learning Advance features of Asp.Net 2.0</Description> <Price>350</Price> <Author>Bipin Joshi</Author> </Book> <Book bookid="2"> <Name>Complete Reference to Java 1.5</Name> <Description>Learning Basic features of Java </Description> <Price>450</Price> <Author>Herbert Schildt</Author> </Book> </Books>
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com 140
(Source: Module4\Books.xml)
140
Code
protected void btnXMLData_Click(object sender, EventArgs e) { DataSet ds = new DataSet(); ds.ReadXml(Server.MapPath("Books.xml")); grid.DataSource = ds; grid.DataBind(); }
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
141
(Source: Module4\18XML.aspx.cs)
141
Using XMLDataSource
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
142
(Source: Module4\19XMLDataSource.aspx) XMLDataSource: Uses an XML file as data source. It is especially used in binding hierarchical data to the navigation controls but can also be used with the GridView control. Whenever we bind the xml data to the grid, by default all the attributes specified into the file are automatically treated as Columns. But if we want that elements in the file also should be considered as columns then we need to add template columns in to the grid. Add label to the Item Template and bind it to the elements of the XML file. To bind XML data, we need to use a special function called XPath(string Expression). XPath expressions are used to traverse the xml tree, filter the data of xml.
142
LINQ DataSource
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
143
(Source: Module4\20LinqDataSourceEg.aspx) .NET Framework 3.5 introduces a new feature called LINQ (Language Integrated Query) which gives a query syntax for retrieving data from various sources such as an array, collection, dataset, object, xml.
143
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
144
144
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
145
145
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
146
146
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
147
147
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
148
148
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
149
149
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
150
150
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
151
151
GridView Control
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
152
GridView Control: Displays more than one columns data. Can be bound to SqlDataSource and ObjectDataSource controls. It allows us to add various types of fields like ButtonField, BoundField, TemplateField etc. It fires events whenever any operation is performed on data through the control.
152
Column Types
Types of Columns available with the GridView:BoundField ButtonField ImageField HyperLinkField CheckBoxField CommandField TemplateField
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
153
153
Bound Field
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
154
(Source: Module5\01BoundFieldsEg.aspx) Bound Field is used to bind one column data to one column of the grid view control. For this purpose we need to add a BoundField from the available fields. It will then appear into the SelectedFields. The properties we need to set are: DataField > the field name as specified into the database and HeaderText > Name of the field for display. We can also set ReadOnly to true if the field is a primary key.
154
Template Field
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
155
(Source: Module5\02TemplateFieldEg.aspx) Template Fields are used to present customized data like displaying computed data or combined data. When the field is added, it appears as a container. It contains multiple templates where in we can put other controls and bound them to the data. Various templates are discussed in the next slide.
155
Types Of Templates
Header Template Item Template Alternating Item Template EditItem Template Pager Template Footer Template
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
156
(Source: Module5\02TemplateFieldEg.aspx) The different templates are: Header Template: It is the template where in we display the column names. HeaderText is the property which need to be set for displaying names on the header template. Item Template: It is the template where actual data is displayed. Data is bound to the controls put in it is with the method Eval(string expression) which is a one way binding. Alternating Item Template: It is the same as Item Template. It is just used to display data in different format for alternating template. EditItemTemplate: This template is produced only when we want to manipulate the data and we have generated manipulation statements with the datasource. Here the Bind(string Expression) method is used to bind the controls kept in it. This produces two way data binding. Pager Template: Used for paging. Footer Template: Used to display data like Grand Total or summary on footer. ShowFooter is the property with the GridView control that need to be set to display footer.
156
Template Field
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
157
(Source: Module5\02TemplateFieldEg.aspx)
157
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
158
(Source: Module5\02TemplateFieldEg.aspx)
158
decimal.Parse(Eval("UnitPrice").ToString())*int.Parse(Eval("UnitsInStock").ToString())
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
159
(Source: Module5\02TemplateFieldEg.aspx)
159
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
160
(Source: Module5\03CommandFieldEg.aspx)
160
Enabling Manipulation
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
161
(Source: Module5\03CommandFieldEg.aspx)
161
DetailsView Control
Can be bound to any Data Source Displays single record details at a time. Allows paging and manipulation to the data.
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
162
(Source: Module5\04DetailsViewEg.aspx)
162
FormView
It is a Template based control. Its view is similar to the Details view. It also allows paging and manipulation to the data.
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
163
(Source: Module5\05FormViewControlEg.aspx)
163
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
164
(Source: Module5\05FormViewControlEg.aspx)
164
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
165
(Source: Module5\05FormViewControlEg.aspx)
165
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
166
(Source: Module5\05FormViewControlEg.aspx)
166
DataList Control
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
167
(Source: Module5\06DataListEg.aspx)
167
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
168
(Source: Module5\06DataListEg.aspx)
168
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
169
(Source: Module5\06DataListEg.aspx)
169
Repeater Control
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
170
(Source: Module5\07RepeaterEg.aspx)
170
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
171
(Source: Module5\07RepeaterEg.aspx)
171
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
172
(Source: Module5\07RepeaterEg.aspx)
172
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
173
(Source: Module5\07RepeaterEg.aspx)
173
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
174
(Source: Module5\07RepeaterEg.aspx)
174
Query
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
175
(Source: Module5\07RepeaterEg.aspx)
175
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
176
(Source: Module5\08ProductsInfo.aspx)
176
Output
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
177
177
ListView Control
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
178
(Source: Module5\09ListViewControl.aspx) ListView Control: It is a new control introduced by .NET 3.5. It is a template based control. We can bind data to this control either through DataSourceID property or DataSource property. DataSourceID property helps bind a control to DataSourceControls. This gives all the capabilities of sorting, paging, modifying the data. DataSource property helps bind a control to DataSet, DataReader, Collections. But then to perform sorting, paging, and modifications we need to write the code.
178
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
179
(Source: Module5\09ListViewControl.aspx)
179
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
180
(Source: Module5\09ListViewControl.aspx)
180
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
181
181
About Navigation
Site navigation is used to provide the way to navigate the site. Provides tools to easily access the web pages. The controls such as TreeView, Menu and SiteMapPath is used for this purpose.
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
182
182
TreeViewControl
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
183
(Source: Module7\01TreeViewEg.aspx) TreeView control displays the data in tree structure. The first node is the root node. It has parent nodes and leaf nodes that dont contain any further nodes. We can bind the tree view control to xml datasource and sitemap datasource controls. XMLDataSource control requires XML file as data and SiteMapDataSource requires sitemap file.
183
Using XMLDataSourceControl
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
184
(Source: Module7\01TreeViewEg.aspx)
184
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
185
(Source: Module7\01TreeViewEg.aspx)
185
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
186
(Source: Module7\01TreeViewEg.aspx)
186
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
187
187
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
188
(Source: Module7\01TreeViewEg.aspx)
188
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
189
(Source: Module7\01TreeViewEg.aspx) After providing the file, the nodes will not display proper text. We need to bind it by setting properties
189
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
190
From the smart tag, we will choose Edit TreeNode DataBindings option. We will get the above screen. We will select subject and click on Add. Once it is added to the selected databindings, we will choose the property TextField. We will set it to the name attribute of the XML file.
190
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
191
(Source: Module7\01TreeViewEg.aspx) After adding subject, add course. Set TextField to the name attribute of the course element and NavigateUrlField to url attribute of the course element.
191
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
192
(Source: Module7\01TreeViewEg.aspx) Once set the properties, the treeview would look like given in the slide.
192
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
193
(Source: Module7\02TreeView_Mannual.aspx) We can also bind treenodes manually. For this, Select TreeView control > Select Smart Tags > click Edit Nodes.
193
Adding Nodes
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
194
(Source: Module7\02TreeView_Mannual.aspx)
194
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
195
(Source: Module7\02TreeView_Mannual.aspx)
195
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
196
(Source: Module7\02TreeView_Mannual.aspx)
196
Showing Checkboxes
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
197
(Source: Module7\03TreeView_CheckBox.aspx) To display checkboxes to the treeview, set the following property:
Control TreeView
197
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
198
(Source: Module7\03TreeView_CheckBox.aspx.cs)
198
Menu Control
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
199
(Source: Module7\04MenuEg.aspx)
199
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
200
(Source: Module7\04MenuEg.aspx)
200
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
201
(Source: Module7\05SiteMapEg.aspx)
201
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
202
(Source: Module7\05SiteMapEg.aspx) To work with SiteMapPath control and SiteMapDataSource control, we need to add Web.sitemap file to the application. It is an xml based file. It contains the node hierarchy. We need to set the attributes given in the file to the required values.
202
Web.SiteMap file
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
203
(Source: Module7\web.sitemap) This slide shows the structure of sitemap file. It contains siteMapNode element and has three attributes namely url, title and description. The url attribute need to be set to the url of the file, title attribute to the text to be displayed to the user, and description will appear as a tool tip when the mouse pointer will be moved on the controls text.
203
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
204
(Source: Module7\05SiteMapEg.aspx) After creating the file,We need not to bind it explicitly to either SiteMapDataSource control or SiteMapPath control. When we just add any one of these, the file would be bound automatically. After adding SiteMapPath DataSource control, we need to drag the control on each file that we have set to the value of url attribute. As we want to create home, about us, contact us and branches pages, to the corresponding pages we will add the SiteMapPath control. It is shown in the next slide. This slide shows the home page.
204
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
205
205
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
206
206
Themes
Adding a Skin File
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
207
(Source Code: Module8/01UsingThemeProperty.aspx) Themes are used to provide consistent look for the controls of the web pages. They can be set for individual pages or as global to all the pages in the Web.config file. Themes are stored into the special folders called as App_Theme. App_Theme is a special folder that stores all the themes. To store themes, we require skin files. Skin file stores the formats that we want to apply to the individual controls. We can either create individual skin files for individual controls or create one file for all the controls. Here we are creating Button.skin file to the application. It will add App_Themes.
207
Skin File
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
208
(Source Code: Module8/01UsingThemeProperty.aspx) The slide shows the skin file where we have stored the format for Button control. This format will be applied to all the buttons.
208
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
209
(Source Code: Module8/01UsingThemeProperty.aspx) Theme can be applied at page level or at application level. To apply it at page level, we need to add Theme property to the page directive of the individual page. It is to be set to the name of the folder which contains the skin file for the controls.
209
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
210
(Source Code: Module8/01UsingThemeProperty.aspx) After setting the Theme property of the page, the control will not display the format during design. It will be applied during run time.
210
Default Skin
Named Skin
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
211
(Source Code: Module8/02UsingSkinID.aspx) We can add multiple format instructions for a single control. When we add multiple formats for the same control, we need to bifurcate them by keeping one format instruction as default skin and other as a named skin. To define a named skin, we need to provide skin id. If we dont then we will get an error as default skin already exist.
211
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
212
212
Once applied, controls on all the pages will have the same format.
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
213
213
Disabling Theme
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
214
(Source Code: Module8/04EnableThemingPropertyEg.aspx) By default, themes are enabled as a property EnableThemining property is set to true. If we set it to false, the control will not be getting the format specified into the theme.
214
Master Pages
Master pages act as a template to the web pages. It provides consistent layout to the web pages. Contains placeholder to hold the content page data. It is a solution to User Controls.
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
215
215
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
216
216
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
217
217
Content Page
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
218
218
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
219
219
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
220
220
State Management
State Management Techniques
Client - Side
Server - Side
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
221
What is State Management? The standard transfer protocol (HTTP) is a stateless protocol. Web pages are regenerated when they are sent to the server. So, the information associated with the page is lost during round trip. To maintain this State Management techniques are provided by .NET Framework.
221
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
222
222
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
223
Due to the stateless nature of the standard protocol, the information need to be maintained between round trips. So, whatever information is set to the controls during run time need to be retained. For this, we have View State that is enabled on almost all the controls to retain the value. To enable view state for a control, there is a property EnableViewState which need to be set to true. It maintains this information in a hidden field called as _VIEWSTATE. This hidden field stores control information in hashed, encrypted format. When the page is posted back to the server, the information stored in view state string is checked and restored to the controls.
223
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
224
(Source Code: Module9/01ViewStateEg.aspx) Property to be set for the Label for view state: EnableViewState = True
224
(Source Code: Module9/01ViewStateEg.aspx.cs) Run the code. Make the EnableViewState property to false and try running the program. Disadvantages of ViewState: It affects the performance: As it is stored within the page it self, if large values are stored then it may slow down the load of the page as values stored in view state need to be loaded. They are not much secured: Though data stored in view state is in hashed format, it can be tempered with. They can be viewed directly leads to security issue.
225
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
226
(Source Code: Module9/02QueryStringEg.aspx) (Source Code: Module9/02_QueryString_Testing.aspx) Query String: It is the string appended with the URL of the page. It is used to pass information from one page to another. This is another way of maintaining state on client side. The information is passed from one page to another page with the URL. For e.g. www.shoppingmall.com\cart.aspx?productid=1. It is simple to implement. No server resources are required to maintain it. It requires HTTP Get method to submit the page.
226
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
227
227
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
228
228
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
229
(Source Code: Module9/02QueryStringEg.aspx) (Source Code: Module9/02_QueryString_Testing.aspx) Limitations of Query String: 1.Visible in the browser: It is easily visible in the address bar of the browser, so anybody can easily temper with it. A user can bookmark the url and can send it to another user along with the query string. 2.Limited capacity: Limited data can be sent through query string. Some browsers may allow only 255 characters of data to be passed through query string. 3.Advantage of query string can not be availed if the page is passed using HTTP POST method.
229
Cookies
Text files used to store user information. It is light weight, stores data in key/value pair. Information is stored with the request to the server. Can be temporary or permanent.
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
230
(Source Code: Module9/03Cookies.aspx) Cookies: Cookies are used to store user information. They are light weight and stores information in key and value pair. Whenever user accesses the web page of a site for the first time, some user specific information is stored in a text file and sent to the client. Next time when user visits the same page, the information is picked up from the text file and used to render the pages. Cookies can be temporary that remains on client machine till the user session is active. Cookies can be permanent that remains on the client machine for a specific duration.
230
Cookie Example
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
231
231
Cookie Example
Creating Cookie
protected void btnWriteCookie_Click(object sender, EventArgs e) { HttpCookie cookie = new HttpCookie("MyCookie"); cookie.Expires = System.Convert.ToDateTime("10/31/2005"); cookie.Value = System.DateTime.Now.ToString(); Response.Cookies.Add(cookie); Response.Write("Cookie Written"); }
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
232
232
Cookie Example
Retrieving a Cookie.
protected void btnReadCookie_Click(object sender, EventArgs e) { HttpCookie cookie = Request.Cookies.Get("MyCookie"); if (cookie != null) { Response.Write(cookie.Value); } else { Response.Write("Cookie doesnot exist"); } }
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
233
(Source Code: Module9/03Cookies.aspx.cs) Limitations of Cookies: 1.Size limitations: Limited amount of data can be stored. Around 4096 bytes data can be stored. 2.Restrictions on client browsers: Client may disable the cookies then this functionality will not be available. Client may delete the cookie. 3.Security risk: Cookies can be tempered with as it is visible to the users.
233
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
234
234
Application State
Stores the information that is available to all the pages of the website. The information is stored with the application state variables in the form of key and value pair. Data is shared by multiple sessions.
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
235
(Source Code: Module9/04ApplicationState.aspx) (Global.asax) Application State is an instance of HTTPApplicationState class. It stores data in key / value pair. Application state is stored on server memory and is faster to retrieve data. Application state is applicable to all users. Global.asax file: It is a Global Application Class file that stores server events that are fired for application and sessions. The events available are: Application_Start, Application_End, Application_Error, Session_Start, Session_End. Application_Start event is fired when application is hit for the first time. Happens only once. Session_Start event is fired when each user session is started. Session_End is fired either with session timeout or on the call of Abandon() method of session. Application_End evnt is fired on server crash or server shut down.
235
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
236
236
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
237
237
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
238
(Source Code: Module9/04ApplicationState.aspx) Limitation of Application State: Requires server resources and there by occupying space on the server and affecting the application scalability. It is volatile. It will be lost if the server process is distroyed.
238
Session State
Allows to store each user session specific value. Values are stored on the server.
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
239
239
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
240
(Source Code: Module9/05SessionState.aspx) (Source Code: Module9/05Session_State_Testing.aspx) Limitations of Session State are: Affects Performance: Session state can store single values as well as large amount of data like dataset. It stays in the server memory till it is removed or replaced. This slows down the performance of the server.
240
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
241
241
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
242
242
What is Caching?
Storing data which is accessed frequently and takes significant amount of processing time to improve performance. We can cache either the output displayed on the page or data retrieved from the database or some complex logic. Used to reduce server hits there by reducing the load on the server.
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
243
243
Types of Caching
Page Output Caching
Page Caching Fragment Caching Post Cache Substitution
Data Caching
Invalidating Cache by using Expiration Policies
Absolute Expiration Sliding Expiration
Dependencies
File Dependency Sql Cache Dependency
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
244
Types of Caching: 1.Page Output Caching: 1. Page Caching: Here the entire page content remains into the memory for a specific duration. When the duration elapses, the cache is refreshed with the new data i.e. page content. For that specific duration, the page is retrieved from the cache there by avoiding recreation of page every time. 2. Fragment Caching: Only a part of the page is cached for specific duration. Rest of the page is rendered from the server. Web user control is used to achieve this. 3. Post Cache Substitution: It is an opposite of Fragment Caching. Here, only a part of the page is refreshed every time when page is retrieved. Rest of the page remains cached for specific duration. Substitution control is used to achieve this. 2.Data Caching: Asp.Net allows data to be cached in to the memory. Data can either be retrieved from the file or database. To save frequent database access, we cache the data. There is an inbuilt object available called as Cache that stores the data. To invalidate cache items we can either use Expiration Policies or Dependencies. 1. Expiration Policies: Cache can expire on the basis of Time Spans. There are two expiration policies available: 1. Absolute Expiration: For fixed duration the data will remain in the cache. Once that duration is elapsed, the cache will get invalidated, new data will be fetched and added to the cache. 2. Sliding Expiration: That is for how long the data will remain into the cache after it is last accessed. It is achieved through TimeSpan values. 2. Dependencies: The cache is dependent on some resource like database or a file. If any changes happen to these resources then the cache is refreshed. 1. File Dependency: The data can be cached based on file like XML file. Whenever changes are made to the file they are retrieved into the cache. 2. Sql Cache Dependency: The data in the cache is dependent on the database.
244
Page Caching
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
245
(Source Code: Module10/OutputCaching/01PageCaching.aspx) For page output caching, we need to add a directive as follows: <%@ OutputCache Duration="10" VaryByParam="none"%> Duration Attribute: Specifies the time span for which the page output will remain in the cache. VaryByParam Attribute: Specifies that whether cache will vary by a parameter passed to the page. In this page, it is set to none.
245
Page Caching
protected void Page_Load(object sender, EventArgs e) { lblTime.Text = System.DateTime.Now.ToString(); }
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
246
246
VaryByParam Example
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
247
247
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
248
Put this line of code in the html source: <%@ OutputCache Duration="10" VaryByParam="city"%>
248
Code
Passing Parameter:
protected void btnSubmit_Click(object sender, EventArgs e) { Response.Redirect("~/02_VaryByParamEg_Testing.aspx?city=" + txtCity.Text); }
Retrieving Parameter:
protected void Page_Load(object sender, EventArgs e) { lblTime.Text = System.DateTime.Now.ToString(); lblCity.Text ="City Entered: "+ Request.QueryString.Get("city"); }
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
249
249
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
250
250
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
251
(Source Code: Module10/OutputCaching/03VaryByControl.aspx) The directive for this example would be as follows: <%@ OutputCache Duration="10" VaryByControl="txtColor" %> VaryByControl Attribute: The cache varies according to the changes made to the control. It takes control ID as a value.
251
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
252
252
Fragment Caching
We need to use a web user control for fragment caching.
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
253
253
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
254
(Source Code: Module10/OutputCaching/01WebCntPartialPageCaching.ascx) (Source Code: Module10/OutputCaching/04FragmentCaching.aspx) Add OutputCache directive to the control. The control is saved with the extension .ascx.
254
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
255
255
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
256
(Source Code: Module10/OutputCaching/01WebCntPartialPageCaching.ascx) (Source Code: Module10/OutputCaching/04FragmentCaching.aspx) After creating the control, we need to add it on the page. For adding a control, the following directive is used. <%@ Register Src="~/01WebCntPartialPageCaching.ascx" TagName="WebCntPartialPageCaching" TagPrefix="ucl" %>
256
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
257
257
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
258
258
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
259
259
Data Caching
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
260
260
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
261
261
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
262
262
Absolute Expiration
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
263
(Source Code: Module10/DataCaching/02AbsoluteExpiration.aspx) Absolute Expiration: The cache will expire after a set period of time, irrespective of whether it is accessed by users or not.
263
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
264
264
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
265
265
Sliding Expiration
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
266
(Source Code: Module10/DataCaching/03SlidingExpiration.aspx) Sliding Expiration: It is to set that after what duration the cache will expire after it is last accessed.
266
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
267
267
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
268
268
File Dependency
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
269
269
Courses.xml File
<?xml version="1.0" encoding="utf-8" ?> <Courses> <course> <name>ASP.NET 2.0</name> <duration>80Hrs</duration> </course> <course> <name>VB.NET 2.0</name> <duration>120Hrs</duration> </course> <course> <name>VB.NET 2.0</name> <duration>120Hrs</duration> </course> </Courses>
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com 270
270
271
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
272
272
SQL Dependency
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
273
273
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
274
(Source Code: Module10/DataCaching/05SQLCacheDependency.aspx) In order to work with SQL Cache Dependency, we need to enable the database as well as the table for dependency. Asp.Net provides a command line utility for this purpose. On the visual studio command prompt we need to write the following line of code: To enable SQL Cache Dependency on the Database: >aspnet_regsql S PSPL2939\SA1 E d Northwind ed To enable SQL Cache Dependency on the Table: >aspnet_regsql S PSPL2939\SA1 E d Northwind Explanation for switches provided by the utility: -S : Provide server name -E : Trusted Connection -U : User Name -P : Password -d : Database -ed : Enable dependency on Database -t : Table -et : Enable dependency on Table. -lt : List tables -t Products et
274
Configuring Web.config
<caching> <sqlCacheDependency enabled="true"> <databases> <add name="Northwind" connectionStringName ="constr" pollTime=500"/> </databases> </sqlCacheDependency> </caching>
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
275
275
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
276
276
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
277
277
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
278
278
Authentication
Three Authentication Modes are available:
Windows Authentication Forms Authentication Passport Authentication
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
279
Three Modes of Authentication: 1.Windows Authentication: It considers the user identity supplied by Internet Information Services for Authentication. It is the default authentication mode. 2.Forms Authentication: It allows user created credentials to be checked for authentication purpose. In this type of authentication, we need to create a login page to accept credentials. Whenever user requests any page of the web site, he/she will be taken to login page for authentication. After authentication he/she will be redirected to the requested page. 3.Passport Authentication: It is a centralized authentication service provided by Microsoft that helps users to use single logon for member sites.
279
Windows Authentication
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
280
(Source Code: Module11/WindowsAuthentication/01WindowsAuthenticationEg.aspx) We need to configure Web.config file. The following elements need to be added. <authentication mode="Windows"/> <authorization> <deny users="?"/> </authorization> By default authentication mode is Windows. Authorization helps to check whether user has got rights to access the page or not. <deny users=?/> here ? means anonymous access which can be allowed or denied by using element <allow> or <deny>
280
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
281
281
Forms Authentication
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
282
282
Configurations in Web.config
<authentication mode="Forms"> <forms name="Login" loginUrl=Login.aspx" defaultUrl=Index.aspx"/> </authentication> <authorization> <deny users="?"/> </authorization>
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
283
283
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
284
284
285
Index Page
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
286
286
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
287
287
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
288
Web Site Configuration: The site configuration is generally stored in Web.Config file. The administration tool allows to configure the settings through an Administration Tool without having to do it manually. This tool creates a configuration file, if it does not exist with the application. It also generates the required databases for memberships, roles, profiles etc. in App_Data folder. It is a tab based tool. It has the following tabs: Security Tab: Helps to manage authentication, membership, roles related to users. Application Tab: We can do application settings, take application offline, can do SMTP settings, debugging and tracing. Provider Tab: Allows to choose providers for membership, roles, profiles.
288
Security Tab
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
289
289
Create User
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
290
290
Manage Users
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
291
291
Application Tab
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
292
292
Provider Tab
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
293
293
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
294
294
Membership API
Membership deals with managing the users. It allows creation, validation of users. It simplifies the management of users as it provides built in methods to manipulate users.
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
295
Membership provides following facilities: 1.Creation of a new user. 2.Authenticating a user by validating user credentails. 3.Storing user, membership related information in database. 4.Changing password. 5.Deleting users. 6.Retrieving password. It uses membership provider with the name AspNetSqlProvider to store all membership and user related data.
295
Membership Provider
<membership> <providers> <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="LocalSqlServer" enablePasswordRetrieval="false enablePasswordReset="true" requiresQuestionAndAnswer="true applicationName="/" requiresUniqueEmail="false passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression="" /> </providers> </membership>
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
296
Membership provides various features that can be configured in web.config file. The above slide shows the membership provider and the settings that can be applied to membership provider. These default settings are provided by machine.config file which can be overridden in web.config file.
296
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
297
This set up wizard is used to create a database with the name aspnetdb that contains all the information related to membership, roles, users, profiles etc. For this purpose one need to run a command line utility aspnet_regsql.exe
297
Select Option
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
298
298
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
299
299
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
300
300
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
301
301
Configuration Settings
<connectionStrings> <remove name="LocalSqlServer"/> <add name="LocalSqlServer" connectionString="server=pspl2939\sa1;database=aspnetdb;integrated security=true" providerName="System.Data.SqlClient"/> </connectionStrings> <authentication mode="Forms"> <forms name="Login" loginUrl=Login.aspx" defaultUrl=Index.aspx"/> </authentication> <authorization> <deny users="?"/> </authorization>
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
302
302
Login Controls
Login Control
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
303
303
LoginName LoginStatus
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
304
304
Module 13: Configuration, Administration and Deployment of the Web Application Overview
Asp.Net Configuration System Structure of web.config Administrating Asp.Net web application Storing Database connection string in config file Web Site Administration Tool Deploying Asp.Net application
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
305
305
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
306
306
Machine.config
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
307
Located at Drive>\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config
307
Web.config
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
308
Web.config file has an inheritance behavior. It inherits the settings from Machine.config file. There can be one or more web.config files in one or more folders of the application. By default one web.config gets added per application. But a file kept in a sub folder takes precedence over the default web.config.
308
IIS Manager
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
309
309
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
310
Instead of configuring settings mannually into web.config file, we can use a tool provided to make these changes.
310
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
311
The General Tab helps manage the connection strings and application settings for the application.
311
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
312
312
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
313
313
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
314
314
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
315
(Source Code: Module13\WebSetup_Test) The file system editor shows Web Application Folder that shows what is to be installed on the target machine.
315
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
316
(Source Code: Module13\WebSetup_Test) Before we add any project to the web setup, we need to set compilation mode debug to false.
316
Add Output
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
317
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
318
(Source Code: Module13\WebSetup_Test) After adding the project output, build the web setup project. Set the compilation mode to Release and not to Debug.
318
Start Installer
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
319
319
Select Path
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
320
320
Confirm Installation
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
321
321
Installation Complete
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
322
322
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
323
323
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
324
324
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
325
A web service exposes a number of methods that provide functionality that can be used by one or more applications, regardless of the programming languages, operating systems and hardware platforms used to develop them. The methods that provide such functionality are called web methods. Some of the use of XML Web Service Authentication Services Weather Reports Exchange Rate Stock Quotes Etc.
325
Client Application
Web Service
Describes
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
326
Web service is comprised of five fundamental components Delivery Medium Internet Delivery Protocol HTTP Message Formatting SOAP (Simple Access Object Protocol) Service Description WSDL (Web Service Description Language) Web Service Publication UDDI (Universal Description, Discovery and Integration)
326
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
327
327
WSDL
WSDL (Web Services Description Language) is used to locate web services. It describes the web service by containing links to the resources that define the web service.
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
328
328
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
329
329
WebServiceHello Example
Service Class
[WebService(Namespace = "http://tempuri.org/")] public class ServiceHello : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } }
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
330
330
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
331
331
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
332
332
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
333
333
Add Reference
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
334
334
Test Code
using TestService; public partial class _01ServiceHelloTesting : System.Web.UI.Page { ServiceHello hello; protected void Page_Load(object sender, EventArgs e) { hello = new ServiceHello(); Response.Write(hello.HelloWorld()); } }
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
335
335
Test Service
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
336
336
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
337
337
Service Class
public class ServiceExchangeRate : System.Web.Services.WebService { [WebMethod] public decimal GetExchangeRate(string country) { DataSet ds = new DataSet(); decimal rate; ds.ReadXml(Server.MapPath("~/ExchangeRate.xml")); DataRow[] row = ds.Tables[0].Select("Country='" + country + "'"); if (row != null) { rate = decimal.Parse(row[0]["Rate"].ToString()); return rate; }
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com 338
338
Service Class
else { rate = 0; return rate; } } [WebMethod] public string[] GetCountries() { DataSet ds = new DataSet(); List<string> countryList = new List<string>(); ds.ReadXml(Server.MapPath("~/ExchangeRate.xml")); foreach (DataRow dr in ds.Tables[0].Rows) { countryList.Add(dr["Country"].ToString()); } return countryList.ToArray(); }
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com 339
339
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
340
340
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
341
341
XML Output
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
342
342
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
343
343
Output
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
344
344
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
345
345
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
346
(Source Code: Module14\WebServiceExchangeRateTest\01TestExchangeRateService.aspx) Go to Project Menu > Add Web Reference Select web Services on the Local Machine
346
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
347
347
Crete Proxy
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
348
348
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
349
(Source Code: Module14\WebServiceExchangeRateTest\01TestExchangeRateService.aspx) A folder with the name App_WebReferences would be added to the application which contains the reference we have added.
349
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
350
350
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
351
351
Pragati Software Pvt. Ltd., 312, Lok Center, Marol-Maroshi Road, Marol, Andheri (East), Mumbai 400 059. www.pragatisoftware.com
352
352