5. Application Development using C#



TOPICS: Application Development using C#: Understand object oriented concepts in C#.NET, Creation of UI and event handling, web page creation using ASP.NET, ADO.NET architecture, implementation of data seta, using ADO.NET in console application, using ADO.NET in web application.

5.1 UNDERSTAND OBJECT ORIENTED CONCEPTS IN C#.NET

  • A class is a collection of objects and represents description of objects that share same attributes and actions. It contains characteristics of the objects such as objects attributes, actions or behaviors.
  • An object is a real-world entity that keeps together property states and behaviors.
For example, A “Bike” usually has common elements such as bike color, engine, mileage etc. In OOP terminology these would be called as a Class Properties or Attributes of a Bike object.

  • Encapsulation is the process of keeping or enclosing one or more items within a single physical or logical package. In object oriented programming methodology it prevents access to implementation details.
  • Encapsulation is implemented by using access specifiers. An access specifier defines the scope and visibility of a class member. Available access specifiers are public, private, protected, internal etc.
  • Abstraction is the process of providing only essential information to the outside real world and hiding overall background details to present an object. It relies on the separation of interface and implementation.

For example, we continue with “Bike” as an example, we have no access to the piston directly, we can use start button to run the piston. Just imagine if a bike manufacturer allows direct access to piston, it would be very difficult to control actions on the piston. That’s the reason why a bike provider separates its internal implementation from its external interface.
  • Inheritance in OOP allows us to create a new class using an existing one meaning extending one class to another.
  • This concept can also be related with the real world example. Let’s take a Bike example again. A Bike manufacturer uses same mechanism of existing version of the bike while launching a new version with some additional added functionality. This allows manufacturer to save their time and efforts both.
  • The main advantage of extending classes is that it provides a convenient way to reuse existing fully tested code in different context thereby saving lots of time with existing coding and its model style.


5.2 CREATION OF UI AND EVENT HANDLING

C# has all the features of any powerful, modern language. In C#, the most rapid and convenient way to create your user interface is to do so visually, using the Windows Forms Designer and Toolbox. Windows Forms controls are reusable components that encapsulate user interface functionality and are used in client side Windows based applications.
Open your Visual Studio Environment and Click File->New Project


Then you will get a New Project Dialogue Box asking in which language you want to create a new project.



 Select Visual C# from the list, then you will get the followinug screen.



Now you can add controls in your Form Control.

How to add controls to Form ?          
               
In the left side of the Visual Studio Environment you can see the Tool Box. There are lots of controls grouping there in the Tool Box according to their functionalities. Just click the + sign before each group then you can see the controls inside the group. You can select basic controls from Common Controls group. You can place the control in your Form by drag and drop the control from your toolbox to Form control.


How to drag and drop controls ?
In the above picture we drag and drop the Button control from Toolbox - Common control to Form control.
Now you can start write codes on each control to create your programs.

Event Handling :
No GUI application is complete without enabling actions. Even though arranging components is a significant issue, applying actions is also equally important. These actions are what instructs the program to act when something happens.  For example, Mouse clicks, Keyboard presses, etc. Before beginning our discussion let's review how different API’s handle events.
Microsoft Foundation Classes (MFC): These classes are based upon Microsoft's Win32 API. Normally development work is done through Visual C++.  
Java API: Here, the majority of work is done by employing AWT and Swing packages. Actions are enabled by applying Interfaces. The main difficulty is that you should learn and remember all methods in the corresponding interfaces.  Otherwise, you receive compile time errors.
As compared to the above, Event handling in C# is much more simplified. It's possible to handle various Mouse and Key related events quickly and in a more efficient manner. Learning will be easier, if you understand the basic principles behind handling events, which are elaborated below 
  1. Invoke the related event by supplying a custom method or event handler using the += operator as shown here:
b1.Click += new EventHandler(OnClick);
  1. Apply the event handler as described below. It must be in conformance with a delegate of the class System.EventHandler:
          public delegate void EventHandler(object sender, Event args)
The first argument indicates the object sending the event and the second argument contains information for the current event. You can use this argument object to handle functions associated with the related event.

5.3 WEB PAGE CREATION USING ASP.NET
  1. Open Visual Studio or Microsoft Visual Web Developer Express.
  2. On the File menu, click New Web Site.
The New Web Site dialog box appears, as shown in the following illustration:


  1. Under Installed Templates, click Visual Basic or C# and then select ASP.NET Web Site.
When you create a Web site project, you specify a template. Each template creates a Web project that contains different files and folders. In this walkthrough, you are creating a Web site based on the ASP.NET Web Site template, which creates files and folders that are typically used in ASP.NET Web sites.
  1. In the Web Location box, select File System, and then enter the name of the folder where you want to keep the pages of your Web site.
For example, type the folder name C:\BasicWebSite.
  1. Click OK.
Visual Studio creates a Web project that includes prebuilt functionality for layout (a master page, the Default.aspx and About.aspx content pages, and a cascading style sheet), for Ajax (client script files), and for authentication (ASP.NET membership). When a new page is created, by default Visual Studio displays the page in Source view, where you can see the page's HTML elements. The following illustration shows 


5.4 ADO.NET ARCHITECTURE


ADO.NET consist of a set of Objects that expose data access services to the .NET environment. It is a data access technology from Microsoft .Net Framework , which provides communication between relational and non relational systems through a common set of components .
System.Data namespace is the core of ADO.NET and it contains classes used by all data providers. ADO.NET is designed to be easy to use, and Visual Studio provides several wizards and other features that you can use to generate ADO.NET data access code.


Data Providers and DataSet




The two key components of ADO.NET are Data Providers and DataSet . The Data Provider classes are meant to work with different kinds of data sources. They are used to perform all data-management operations on specific databases. DataSet class provides mechanisms for managing data when it is disconnected from the data source.

Data Providers
The .Net Framework includes mainly three Data Providers for ADO.NET. They are the Microsoft SQL Server Data Provider , OLEDB Data Provider and ODBC Data Provider . SQL Server uses the SqlConnectionobject , OLEDB uses the OleDbConnection Object and ODBC uses OdbcConnection Object respectively.
ASP.NET ODBC Connection


A data provider contains Connection, Command, DataAdapter, and DataReader objects. These four objects provides the functionality of Data Providers in the ADO.NET.

Connection
The Connection Object provides physical connection to the Data Source. Connection object needs the necessary information to recognize the data source and to log on to it properly, this information is provided through a connection string.

Command
The Command Object uses to perform SQL statement or stored procedure to be executed at the Data Source. The command object provides a number of Execute methods that can be used to perform the SQL queries in a variety of fashions.

DataReader
The DataReader Object is a stream-based , forward-only, read-only retrieval of query results from the Data Source, which do not update the data. DataReader requires a live connection with the databse and provides a very intelligent way of consuming all or part of the result set.

DataAdapter
DataAdapter Object populate a Dataset Object with results from a Data Source . It is a special class whose purpose is to bridge the gap between the disconnected Dataset objects and the physical data source.

DataSet


DataSet provides a disconnected representation of result sets from the Data Source, and it is completely independent from the Data Source. DataSet provides much greater flexibility when dealing with related Result Sets.

DataSet contains rows, columns,primary keys, constraints, and relations with other DataTable objects. It consists of a collection of DataTable objects that you can relate to each other with DataRelation objects. The DataAdapter Object provides a bridge between the DataSet and the Data Source.


5.5 IMPLEMENTATION OF DATA SETS

The DataSet object is central to supporting disconnected, distributed data scenarios with ADO.NET. The DataSet is a memory-resident representation of data that provides a consistent relational programming model regardless of the data source. It can be used with multiple and differing data sources, with XML data, or to manage data local to the application. The DataSet represents a complete set of data, including related tables, constraints, and relationships among the tables. The following illustration shows the DataSet object model. 


DataSet Object Model
The methods and objects in a DataSet are consistent with those in the relational database model.
The DataSet can also persist and reload its contents as XML, and its schema as XML schema definition language (XSD) schema. For more information, see Using XML in a DataSet.

An ADO.NET DataSet contains a collection of zero or more tables represented by DataTable objects. The DataTableCollection contains all the DataTable objects in a DataSet.
A DataTable is defined in the System.Data namespace and represents a single table of memory-resident data. It contains a collection of columns represented by a DataColumnCollection, and constraints represented by a ConstraintCollection, which together define the schema of the table. A DataTable also contains a collection of rows represented by the DataRowCollection, which contains the data in the table. Along with its current state, a DataRow retains both its current and original versions to identify changes to the values stored in the row.

A DataView enables you to create different views of the data stored in a DataTable, a capability that is often used in data-binding applications. Using a DataView, you can expose the data in a table with different sort orders, and you can filter the data by row state or based on a filter expression. For more information, see DataViews.

A DataSet contains relationships in its DataRelationCollection object. A relationship, represented by the DataRelation object, associates rows in one DataTable with rows in another DataTable. A relationship is analogous to a join path that might exist between primary and foreign key columns in a relational database. A DataRelation identifies matching columns in two tables of a DataSet.
Relationships enable navigation from one table to another in a DataSet. The essential elements of a DataRelation are the name of the relationship, the name of the tables being related, and the related columns in each table. Relationships can be built with more than one column per table by specifying an array of DataColumn objects as the key columns. When you add a relationship to the DataRelationCollection, you can optionally add a UniqueKeyConstraint and a ForeignKeyConstraint to enforce integrity constraints when changes are made to related column values.

5.7 USING ADO.NET IN WEB APPLICATIONS

  • You use the ADO.NET Connection object to create a connection between your program and a database engine. You will normally keep this connection open just long enough to retrieve or update data. By quickly opening, then closing a connection, you use server resources for as little time as possible. This helps you develop scalable, fast applications that are resource-friendly. The fewer resources you use, the more users you can support on your applications at one time.
  • If you are creating a database application, you will eventually need to open a connection to that database. An ADO.NET Connection object allows you to create that connection. You will also need to retrieve and modify data in the database, and that is where you will need to use the ADO.NET Command object.
  • When connecting to SQL Server 7.0 or greater, you use the SqlConnection and SqlCommand objects in the System.Data.SqlClient namespace. When connecting to other OLE DB datasources, use the OleDbConnection and OleDbCommand in the System.Data.OleDb namespace. The rest of the examples in this document show examples using the objects from the System.Data.SqlClient namespace.

To modify data within a database
  1. Create a SqlConnection or an OleDbConnection object and give it a connection string.
  2. Open the connection to the database.
  3. Create a SqlCommand or an OleDbCommand object and assign to it the connection object you opened.
  4. Place an SQL statement into the Command object.
  5. Execute the SQL statement by invoking the ExecuteNonQuery method on the Command object.
Connection and Command Objects
In this document, you will learn to create and open a connection to a SQL Server database using a SqlConnection class. In addition, you will learn to submit an INSERT statement to the same SQL Server database using the SqlCommand object.
To create the sample form
  1. Open Microsoft Visual Studio® .NET.
  2. Click New Project.
  3. From the tree view on the left, select Visual Basic Projects.
  4. From the project templates window, select Windows Application.
  5. Set the Name to DataConnect.
  6. Click OK.
  7. Rename the form called Form1.vb to frmConnect.vb.
  8. Set the Text property on this form to SQL Tester.
  9. Create the controls for your form

No comments:

Post a Comment