0% found this document useful (0 votes)
3 views12 pages

Dotnet Unit 3

The document provides an overview of GUI application development using the .NET framework, detailing components such as Windows Forms, WPF, and various controls like menus, toolbars, and tree views. It also introduces ADO.NET for database programming, covering key concepts like data providers, connections, commands, and data manipulation. The document outlines practical steps for creating GUI elements and managing data connections in .NET applications.

Uploaded by

lonelysb4mi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

Dotnet Unit 3

The document provides an overview of GUI application development using the .NET framework, detailing components such as Windows Forms, WPF, and various controls like menus, toolbars, and tree views. It also introduces ADO.NET for database programming, covering key concepts like data providers, connections, commands, and data manipulation. The document outlines practical steps for creating GUI elements and managing data connections in .NET applications.

Uploaded by

lonelysb4mi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

UNIT-III

GUI Applications Development, Windows forms and controls , Creating Menus ,Toolbars, Image list , Tree View,
List view Data Base Programming , ADO.NET Introduction , Data Providers in .NET , Connection, Command , Data
reader , DataAdapter , Data Set, Command Builder , Data Relations , ADO.NET and XML

GUI Applications Development:

GUI in .NET stands for Graphical User Interface, which refers to the visual elements and controls used
to build software applications using the .NET framework. The .NETframework provides a rich set of graphical
user interface components, such as buttons,menus, text boxes, and dialog boxes, that developers can use to
create Windows applications. Developers can use several programming languages such as C#, Visual Basic
.NET, and F# tocreate GUI-based applications using .NET. The GUI is typically created using a visualdesigner
tool, such as Visual Studio, which allows developers to drag and drop GUI components onto a form and
configure their properties using a graphical interface. The .NET framework provides a rich set of libraries and
APIs for building GUI-based applications, including Windows Forms, WPF (Windows Presentation
Foundation), and ASP.NET (ActiveServer Pages.NET) for web-based applications. These frameworks provide
a wide range of features and functionality to create responsive and visually appealing user interfaces for desktop
and web applications.

The design elements of GUI in .NET refer to the visual components and controls that can be used to create a
user interface for a software application. Some of the key design elements of GUI in .NET include:

Windows Forms: Windows Forms is a graphical user interface framework that provides a setof controls for
building Windows-based applications. Windows Forms controls include buttons, labels, text boxes, menus,
and many others.

WPF (Windows Presentation Foundation): WPF is a graphical user interface framework that provides a rich
set of controls for building Windows applications. It includes advanced controls such as 3D graphics,
animation, and multimedia.

Dialog Boxes: Dialog boxes are windows that appear on top of an application's main window to prompt the
user for input or to display information. Dialog boxes can be used to perform tasks such as file selection,
message display, and error reporting.

Toolbars and Menus: Toolbars and menus are graphical elements that provide access to application
commands and functionality. Toolbars typically contain buttons for frequently used commands, while menus
provide access to a broader range of commands organized into hierarchical menus.

Status Bar: A status bar is a horizontal bar at the bottom of an application window that displays information
about the current status of the application or the task being performed. Status bars can be used to display
information such as progress indicators, status messages, and help text.

Tree View and List View: Tree view and list view controls provide a hierarchical or flat view of data,
respectively. They can be used to display and navigate through large amounts ofdata in a structured way.

Tab Control: A tab control is a graphical element that allows the user to switch between different views or
modes of an application. Tab controls are typically used to organize related functionality or data into separate
tabs.
These design elements can be combined and customized to create a visually appealing and functional GUI for
a .NET application.
Windows forms and controls:
• Windows Forms is a graphical user interface (GUI) framework for developing desktop
applications in the Microsoft .NET framework using the Visual Basic .NET (VB.NET)
• programming language. Windows Forms provide a set of classes, controls, and componentsthat
allow developers to create visually appealing and responsive user interfaces.
• Controls are the building blocks of Windows Forms. They are UI elements such as buttons,labels, text
boxes, drop-down lists, and more. Controls can be added to a form and then manipulated
programmatically to create a functional application.
• In VB.NET, controls are represented by classes that inherit from the Control class. Controls
• can be customized by setting various properties such as color, font, and size. They can also be
programmed to respond to user events such as mouse clicks, key presses, and form resizing.
• Developers can use the drag-and-drop interface of the Visual Studio Integrated Development
Environment (IDE) to create Windows Forms applications quickly. The IDE provides a toolbox with
all the controls and components needed for building Windows Forms
• applications. Developers can simply drag and drop controls onto the form, and then use the
Properties window to set their properties.
• In summary, Windows Forms and controls in VB.NET provide a powerful platform for building
desktop applications with a rich user interface that can respond to user events andinteract with data.

Creating Menus:
To create a menu in a Windows Forms application using Visual Studio, follow these steps:

• Open Visual Studio and create a new Windows Forms project.


• From the Toolbox, drag a MenuStrip control onto the form.
• Click on the MenuStrip control to select it, and then click the "Edit Items" button in the Properties
window. Alternatively, you can double-click the MenuStrip control to open the editor.
• In the editor, you can add new menu items and submenus by clicking the "Add" buttonand selecting the
appropriate option. You can also modify the text, images, and other properties of each menu item.
• To add an event handler for a menu item, select the item in the editor and then click thelightning bolt
icon in the Properties window. This will display a list of events that you can handle, such as Click or
CheckedChanged. Double-click the event to create a new event handler method in your code.
• In the event handler method, you can add the code to perform the desired action when themenu item is
clicked.
Here is an example of creating a simple menu with two items:

• Drag a MenuStrip control onto the form.

• Select the MenuStrip control and click the "Edit Items" button.

• Click the "Add" button and select "ToolStripMenuItem" to add a new menu item.

• Set the "Text" property of the menu item to "File".

• Repeat steps 3 and 4 to add another menu item with the text "Exit".

• Double-click the "Exit" menu item to create a new event handler method.

Add the following code to the event handler method to close the application:
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles
ExitToolStripMenuItem.Click Application.Exit()End Sub

Create a Tool Bar:


To create a toolbar in a Windows Forms application, you can use the ToolStrip control. Hereare the steps to
create a basic toolbar:

• Open your Windows Forms project in Visual Studio.

• Open the Toolbox window by clicking on the "View" menu, then "Toolbox".

• Locate the "ToolStrip" control in the Toolbox, and drag it onto your form.

• By default, the ToolStrip will contain a few standard buttons. You can customize thebuttons by
selecting the ToolStrip and opening the Properties window (press F4).

• In the Properties window, you can add new buttons by clicking the "..." button next to the"Items"
property. This will open the ToolStripItems Collection Editor.

• In the Collection Editor, you can add, remove, and modify the buttons on the toolbar. Youcan also change
the order of the buttons by dragging them up or down in the list.

• Once you have added all the buttons you need, you can customize their appearance andbehavior by
selecting them in the designer and changing their properties in the Properties window.

The code part to configure buttons is given below

private void myButton_Click(object sender, EventArgs e)

{
// Code to handle button click goes here

Image list:
sIn a Windows Forms application, you can use an ImageList control to store and manage a collection of
images that can be used by other controls such as a ListView, aTreeView, or a ToolStrip. Here are the
steps to create an ImageList control:

• Open your Windows Forms project in Visual Studio.

• Open the Toolbox window by clicking on the "View" menu, then "Toolbox".

• Locate the "ImageList" control in the Toolbox, and drag it onto your form.

• In the Properties window (press F4), you can configure the ImageList control bysetting the properties
such as ColorDepth, ImageSize, and TransparentColor.

• To add images to the ImageList, click the "..." button next to the "Images"property in the
Properties window. This will open the ImageCollectionEditor.

• In the ImageCollectionEditor, you can add, remove, and modify the images inthe ImageList. You
can also import images from a file by clicking the "Import" button.

• Once you have added all the images you need, you can use them in other
• controls that support the ImageList control. For example, you can set the ImageList property of a
ListView control to the ImageList control you just created, and then
• set the ImageIndex property of each item in the ListView to the index of the imageyou want to display.

Example of code to set the ImageList property of a ListView control:

// Create an ImageList control

ImageList imageList1 = new ImageList(); imageList1.Images.Add(Image.FromFile("image1.png"));

imageList1.Images.Add(Image.FromFile("image2.png"));

// Set the ImageList property of the ListView control

myListView.SmallImageList = imageList1;

// Set the ImageIndex property of each item in the ListView

myListView.Items[0].ImageIndex = 0;

myListView.Items[1].ImageIndex = 1;

Tree view:
The tree view control enables a hierarchical list with expanding and collapsing nodes that contain nested items.
It can be used to illustrate a folder structure or nested relationships in your UI.

The tree view uses a combination of indentation and icons to represent the nested relationship between parent
nodes and child nodes. Collapsed nodes use a chevron pointing to the right, and expanded nodes use a chevron
pointing down.

You can include an icon in the tree view item data template to represent nodes. For example, if you show a file
system hierarchy, you could use folder icons for the parent notes and file icons for the leaf nodes.
Creating tree view:

Here's an example of a simple tree view declared in XAML. You typically add the nodes in code, but we show
the XAML hierarchy here because it can be helpful for visualizing how the hierarchy of nodes is created.

XAMLCopy
<muxc:TreeView>
<muxc:TreeView.RootNodes>
<muxc:TreeViewNode Content="Flavors"
IsExpanded="True">
<muxc:TreeViewNode.Children>
<muxc:TreeViewNode Content="Vanilla"/>
<muxc:TreeViewNode Content="Strawberry"/>
<muxc:TreeViewNode Content="Chocolate"/>
</muxc:TreeViewNode.Children>
</muxc:TreeViewNode>
</muxc:TreeView.RootNodes>
</muxc:TreeView>

List View
The ListView control displays data stacked vertically in a single column. ListView works better for items that
have text as a focal point, and for collections that are meant to be read top to bottom (for example,
alphabetically ordered). A few common use cases for ListView include lists of messages and search results. If
you need to display collections in multiple columns or in a table-like format, you should not use ListView.
ADO.NET Introduction:
ADO(ActiveX Data Object) .NET is a module of .Net Framework which is used to establish connection between
application and data sources. Data sources can be such as SQL Server and XML. ADO.NET consists of classes
that can be used to connect, retrieve, insert and delete data.

All the ADO.NET classes are located into System.Data.dll and integrated with XML classes located
into System.Xml.dll.

ADO.NET has two main components that are used for accessing and manipulating data are the .NET Framework
data provider and the DataSet.

These are the components that are designed for data manipulation and fast access to data. It provides various
objects such as Connection, Command, DataReader and DataAdapter that are used to perform database
operations.

ADO.NET Framework Data Providers:


Data provider is used to connect to the database, execute commands and retrieve the record. It is lightweight
component with better performance. It also allows us to place the data into DataSet to use it further in our
application.

The .NET Framework provides the following data providers that we can use in our application.

.NET Framework data provider Description


.NET Framework Data Provider for SQL It provides data access for Microsoft SQL Server. It requires
Server the System.Data.SqlClient namespace.
.NET Framework Data Provider for OLE It is used to connect with OLE DB. It requires
DB the System.Data.OleDb namespace.
.NET Framework Data Provider for It is used to connect to data sources by using ODBC. It requires
ODBC the System.Data.Odbc namespace.
.NET Framework Data Provider for It is used for Oracle data sources. It uses
Oracle the System.Data.OracleClient namespace.
EntityClient Provider It provides data access for Entity Data Model applications. It requires
the System.Data.EntityClient namespace.
.NET Framework Data Provider for SQL It provides data access for Microsoft SQL Server Compact 4.0. It
Server Compact 4.0. requires the System.Data.SqlServerCe namespace.

ADO.NET Connection
It is used to establish an open connection to the SQL Server database. It is a sealed class so that cannot be
inherited. SqlConnection class uses SqlDataAdapter and SqlCommand classes together to increase performance
when connecting to a Microsoft SQL Server database.

Connection does not close explicitly even it goes out of scope. Therefore, you must explicitly close the connection
by calling Close() method.

SqlConnection Signature
public sealed class SqlConnection : System.Data.Common.DbConnection, ICloneable, IDisposable

SqlConnection Constructors

Constructors Description
SqlConnection() It is used to initializes a new instance of the SqlConnection class.
SqlConnection(String)0 It is used to initialize a new instance of the SqlConnection class and takes
connection string as an argument.
SqlConnection(String, It is used to initialize a new instance of the SqlConnection class that takes
SqlCredential) two parameters. First is connection string and second is sql credentials.

SqlConnection Methods

Method Description
BeginTransaction() It is used to start a database transaction.
ChangeDatabase(String) It is used to change the current database for an open SqlConnection.
ChangePassword(String, String) It changes the SQL Server password for the user indicated in the
connection string.
Close() It is used to close the connection to the database.
CreateCommand() It enlists in the specified transaction as a distributed transaction.
GetSchema() It returns schema information for the data source of this SqlConnection.
Open() It is used to open a database connection.
ResetStatistics() It resets all values if statistics gathering is enabled.

SqlConnection Example
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
}

ADO.NET Command
This class is used to store and execute SQL statement for SQL Server database. It is a sealed class so that cannot
be inherited.

SqlCommand Signature
1. public sealed class SqlCommand : System.Data.Common.DbCommand, ICloneable, IDisposable

Constructors

This class provides the following constructors.

Constructor Description
SqlCommand() It is used to initialize a new instance of the
SqlCommand class.
SqlCommand(String) It is used to initialize a new instance of the
SqlCommand class with a string parameter.
SqlCommand(String, SqlConnection) It is used to initialize a new instance of the
SqlCommand class. It takes two parameters,
first is query string and second is connection
string.
SqlCommand(String, SqlConnection, SqlTransaction) It is used to initialize a new instance of the
SqlCommand class. It takes three parameters
query, connection and transaction string
respectively.
SqlCommand(String, SqlConnection, SqlTransaction, It Initializes a new instance of the SqlCommand
SqlCommandColumnEncryptionSetting) class with specified command text, connection,
transaction, and encryption setting.
Methods

Method Description
BeginExecuteNonQuery() It is used to Initiate the asynchronous execution of the SQL statement described
by this SqlCommand.
Cancel() It tries to cancel the execution of a SqlCommand.
Clone() It creates a new SqlCommand object that is a copy of the current instance.
CreateParameter() It creates a new instance of a SqlParameter object.
ExecuteReader() It is used to send the CommandText to the Connection and builds a SqlDataReader.
ExecuteXmlReader() It is used to send the CommandText to the Connection and builds an XmlReader
object.
ExecuteScalar() It executes the query and returns the first column of the first row in the result set.
Additional columns or rows are ignored.
Prepare() It is used to create a prepared version of the command by using the instance of
SQL Server.
ResetCommandTimeout() It is used to reset the CommandTimeout property to its default value.

ADO.NET Data Reader


This class is used to read data from SQL Server database. It reads data in forward-only stream of rows from a
SQL Server database. it is sealed class so that cannot be inherited. It inherits DbDataReader class and implements
IDisposable interface.

SqlDataReader Signature
public class SqlDataReader : System.Data.Common.DbDataReader, IDisposable

SqlDataReader Properties

Property Description
Connection It is used to get the SqlConnection associated with the SqlDataReader.
Depth It is used to get a value that indicates the depth of nesting for the current row.
FieldCount It is used to get the number of columns in the current row.
HasRows It is used to get a value that indicates whether the SqlDataReader contains one or more rows.
IsClosed It is used to retrieve a boolean value that indicates whether the specified SqlDataReader
instance has been closed.
Item[String] It is used to get the value of the specified column in its native format given the column
name.
Item[Int32] It is used to get the value of the specified column in its native format given the column
ordinal.
RecordsAffected It is used to get the number of rows changed, inserted or deleted by execution of the
Transact-SQL statement.
VisibleFieldCount It is used to get the number of fields in the SqlDataReader that are not hidden.

Methods

Method Description
Close() It is used to closes the SqlDataReader object.
GetBoolean(Int32) It is used to get the value of the specified column as a Boolean.
GetByte(Int32) It is used to get the value of the specified column as a byte.
GetChar(Int32) It is used to get the value of the specified column as a single character.
GetDateTime(Int32) It is used to get the value of the specified column as a DateTime object.
GetDecimal(Int32) It is used to get the value of the specified column as a Decimal object.
GetDouble(Int32) It is used to get the value of the specified column as a double-precision floating point
number.
GetFloat(Int32) It is used to get the value of the specified column as a single-precision floating point
number.
GetName(Int32) It is used to get the name of the specified column.
GetSchemaTable() It is used to get a DataTable that describes the column metadata of the SqlDataReader.
GetValue(Int32) It is used to get the value of the specified column in its native format.
GetValues(Object[]) It is used to populate an array of objects with the column values of the current row.
NextResult() It is used to get the next result, when reading the results of SQL statements.
Read() It is used to read record from the SQL Server database.

To create a SqlDataReader instance, we must call the ExecuteReader method of the SqlCommand object.

ADO.NET Data Adapter


The DataAdapter works as a bridge between a DataSet and a data source to retrieve data. DataAdapter is a class
that represents a set of SQL commands and a database connection. It can be used to fill the DataSet and update
the data source.

Data Adapter Class Signature


public class DataAdapter : System.ComponentModel.Component, System.Data.IDataAdapter

Data Adapter Constructors

Constructors Description
DataAdapter() It is used to initialize a new instance of a DataAdapter class.
DataAdapter(DataAdapter) It is used to initializes a new instance of a DataAdapter class from an existing object
of the same type.

Methods

Method Description
CloneInternals() It is used to create a copy of this instance of DataAdapter.
Dispose(Boolean) It is used to release the unmanaged resources used by the
DataAdapter.
Fill(DataSet) It is used to add rows in the DataSet to match those in the
data source.
FillSchema(DataSet, SchemaType, String, It is used to add a DataTable to the specified DataSet.
IDataReader)
GetFillParameters() It is used to get the parameters set by the user when
executing an SQL SELECT statement.
ResetFillLoadOption() It is used to reset FillLoadOption to its default state.
ShouldSerializeAcceptChangesDuringFill() It determines whether the AcceptChangesDuringFill
property should be persisted or not.
ShouldSerializeFillLoadOption() It determines whether the FillLoadOption property should
be persisted or not.
ShouldSerializeTableMappings() It determines whether one or more DataTableMapping
objects exist or not.
Update(DataSet) It is used to call the respective INSERT, UPDATE, or
DELETE statements.
ADO.NET Data Set
It is a collection of data tables that contain the data. It is used to fetch data without interacting with a Data Source
that's why, it also known as disconnected data access method. It is an in-memory data store that can hold more
than one table at the same time. We can use DataRelation object to relate these tables. The DataSet can also be
used to read and write data as XML document.

ADO.NET provides a DataSet class that can be used to create DataSet object. It contains constructors and
methods to perform data related operations.

Data Set Class Signature


1. public class DataSet : System.ComponentModel.MarshalByValueComponent, System.ComponentMode
l.IListSource,
2. System.ComponentModel.ISupportInitializeNotification, System.Runtime.Serialization.ISerializable,
3. System.Xml.Serialization.IXmlSerializable

Data Set Constructors

Constructor Description
DataSet() It is used to initialize a new instance of the DataSet class.
DataSet(String) It is used to initialize a new instance of a DataSet class with the
given name.
DataSet(SerializationInfo, It is used to initialize a new instance of a DataSet class that has
StreamingContext) the given serialization information and context.
DataSet(SerializationInfo, StreamingContext, It is used to initialize a new instance of the DataSet class.
Boolean)

Data Set Properties

Properties Description
CaseSensitive It is used to check whether DataTable objects are case-sensitive or not.
DataSetName It is used to get or set name of the current DataSet.
DefaultViewManager It is used to get a custom view of the data contained in the DataSet to allow filtering and
searching.
HasErrors It is used to check whether there are errors in any of the DataTable objects within this
DataSet.
IsInitialized It is used to check whether the DataSet is initialized or not.
Locale It is used to get or set the locale information used to compare strings within the table.
Namespace It is used to get or set the namespace of the DataSet.
Site It is used to get or set an ISite for the DataSet.
Tables It is used to get the collection of tables contained in the DataSet.

Data Set Methods

The following table contains some commonly used methods of DataSet.

Method Description
BeginInit() It is used to begin the initialization of a DataSet that is used on a form.
Clear() It is used to clear the DataSet of any data by removing all rows in all tables.
Clone() It is used to copy the structure of the DataSet.
Copy() It is used to copy both the structure and data for this DataSet.
CreateDataReader(DataTable[]) It returns a DataTableReader with one result set per DataTable.
CreateDataReader() It returns a DataTableReader with one result set per DataTable.
Command Builder:
Sometimes creating SQL statements could be lengthy job when dealing with many columns in a table. A
Command Builder object reduces the burden of creating SQL statements for you. In other words, the Command
Builder helps you to generate update, delete., and insert commands on a single database table for a data adapter.
Similar to other objects, each data provider has a command builder class. The OleDb Command Builder, Sql
Common Builder, and Odbc Command Builder classes represent the Common Builder object in the OleDb, Sql,
and ODBC data providers. These classes also work in pretty similar fashion. Once you know how to use OleDb
Command Builder, you can use SqlCommand Builder and Odbc Command Builder in a similar way. I'll use
OleDb Command Builder class in this example.

Creating a Command Builder Object

Creating a Commoned Buider object is pretty simply. You pass a Data Adapter as an argument of the Command
Builder constructor. For example:

// Create a command builder object


SqlCommandBuilder builder = new SqlCommandBuilder(adapter);

Sql Command Builder Members

The Data Adapter property of a Common Builder represents the Data Provider attached to a Common Builder
object for which automatic SQL statements are generated. The Get Delete Common, Get Update Command,
and Get Insert Command methods return the delete, update, and insert commands in the form of a Command
object. The Refresh Schema method refreshes the database schema.

Data relations:
To provide data integrity and consistency, we should use relationships between two tables. We can achieve this
relationship by defining a primary key in one table and using a foreign key in the other table. Say a college has
multiple students; the student details table stores the student details, and the result table stores all the student
results details. To avoid the redundancy of data, you define the primary key of the student table as a foreign key
in the results table.
Create Database And Table

create database COLLEGE

use COLLEGE

create table Student_details


(
RollNo int constraint pk primary key,
F_Name varchar(30),
L_Name varchar(30),
City varchar(20)
);

create table Student_Results


(
RollNumber int constraint fk foreign key references AllStudent(RollNo),
Course varchar(20),
Result varchar(4)
);

ADO.NET and XML:


There are two approaches to work with XML and ADO. First, you can use ADO.NET to access XML
documents. Second, we can use XML and ADO.NET to access XML. Additionally, you can access a relational
database using ADO.NET and XML.NET.

Reading XML using Data Set

In ADO.NET, we can access the data using the Data Set class. The Data Set class implements methods and
properties to work with XML documents. The following sections discuss methods that read XML data.

The Read xml Method

Read Xml is an overloaded method; we can use it to read a data stream, Text Reader, Xml Reader, or an XML
file and to store into a Data Set object, which can later be used to display the data in a tabular format. The Read
Xml method has eight overloaded forms. It can read a text, string, stream, Text Reader, Xml Reader, and their
combination formats. In the following example, create a new Data Set object.

In the following example, create a new Data Set object and call the Data Set. Read Xml method to load the
books.xml file in a Data Set object:

//Create a DataSet object


DataSet ds = new DataSet();
// Fill with the data
ds.ReadXml("books.xml");

The Read Xml Schema method

The Read XML Schema method reads an XML schema in a Data Set object. It has four overloaded forms. We
can use a Text Reader, string, stream, and Xml Reader. The following example shows how to use a file as direct
input and call the Read Xml Schema method to read the file:

DataSet ds = new DataSet();


ds.ReadSchema(@"c:\books.xml");

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy