You are on page 1of 31

UNIVERSITY OF GONDAR

COLLEGE OF INFORMATICS
DEPARTMENT OF INFORMATION
TECHNOLOGY./

EVENT

MY NOTE
CHAPTER 3
VB.NET is an object-oriented language and is designed to be easy to learn and use. It is
commonly used for building Windows desktop applications, web applications, and mobile
apps.

In OO Programming methodology, a program consists of various objects that interact


with each other by means of actions.
In object-oriented programming, an object is an instance of a class. A class is a blueprint or
template for creating objects. It defines a set of properties and methods that are common to
all objects of a certain kind.

A method is a function that is associated with a class or object. It defines a behavior or action
that can be performed on an object. Methods are used to implement the functionality of a
class and can be called on an object to perform a specific task.
CHAPTER 3

When we say that an object is an instance of a class, we mean that the object is a specific
example or occurrence of the class. The class is like a blueprint or template that defines the
properties and methods that an object of that class will have. An object is created by using the
class as a template and specifying values for its properties.

For example, consider a class called "Person". This class might have properties such as
"name", "age", and "address", and methods such as "speak" and "walk". An object of the
Person class would represent a specific person, with their own values for the name, age, and
address properties.

EXAMPLE
CHAPTER 3
Here is an example of a simple Person class in VB.NET:

Public Class Person


Public Property Name As String
Public Property Age As Integer
Public Property Address As String

Public Sub Speak()


' code to make the person speak
End Sub

Public Sub Walk()


' code to make the person walk
End Sub
End Class
CHAPTER 3

To create an object of the Person class, you could use code like this:

Dim myPerson As New Person()


myPerson.Name = "John Doe"
myPerson.Age = 30
myPerson.Address = "123 Main St"

This would create a new Person object and set its properties to specific values. The object is
an instance of the Person class, meaning that it is a specific example of the class with its own
unique property values. You could then call the Speak and Walk methods on the object to
perform those
CHAPTER 3

In programming, an identifier is a name given to a variable, function, class, or other program


element. Identifiers are used to identify and refer to these elements in the code.

NAMESPACE

In programming, a namespace is a container for a set of related program elements, such as


classes, interfaces, and enumerations. Namespaces are used to organize and group these
elements in a logical way, and to prevent naming conflicts between elements with the same
name.

For example, consider a program that contains two classes called "Customer". Without
namespaces, it would be impossible to have both classes in the same program, as they would
have the same name and the compiler would not be able to distinguish between them.
However, if the classes were placed in different namespaces, they could coexist in the same
program without any problems
CHAPTER 3

DATATYPE in a programming language describes that what type of data a variable can
hold .

 Syntax : Dim VariableName AS DataType


 VariableName : the variable we declare for hold the values.
 DataType : The type of data that the variable can hold

In Visual Basic (VB), data types can be divided into two main categories: value types
and reference types.

Value types are data types that store their values directly in memory. When you declare
a variable of a value type, a block of memory is allocated to store the value of the
variable. Value types include the following types:
 give full access to object-oriented features,
 but they impose some memory and speed overhead for managing and accessing objects
Boolea Byte Char Date Decimal Double Integer Long Short Single
CHAPTER 3
 Reference give full access to object-oriented features,
 but they impose some memory and speed overhead for managing and accessing objects
 are data types that store a reference to an object in memory. When you declare a variable
of a reference type, a block of memory is allocated to store the reference to the object, not
the object itself. Reference types include the following types:
Object String Array Class

Variables are places where information can be stored while a program is running.
 Each variable in VB.NET has a specific type,
 which determines the size and
 layout of the variable's memory;
 the range of values that can be stored within that memory.
Variable Naming Rules:
 Must begin with a letter.
 Can't contain an embedded period or embedded type-declaration character.
 Must not exceed 255 characters.
 Must be unique within the same scope
CHAPTER 3

The Dim statement is used for variable declaration and storage allocation for one or more
variables. The Dim statement is used at module, class, structure, and procedure or block level

 ach variable in the variable list has the following syntax and parts:
variablename[ ( [ boundslist ] ) ] [ As [ New ] datatype ] [ = initializer ]
 variablename: is the name of the variable
 boundslist: optional. It provides list of bounds of each dimension of an array variable.
 New: optional. It creates a new instance of the class when the Dim statement runs.
 datatype: Required if Option Strict is On. It specifies the data type of the variable.
 initializer: Optional if New is not specified
CHAPTER 3
Declaration statement is a statement that declares a variable, constant, or other program
element and specifies its data type and other characteristics. Declaration statements are
used to define the variables and other elements that a program uses, and they must be
written before the elements can be used in the code.

An executable statement, on the other hand, is a statement that performs an action or


set of actions when the program is running. Executable statements are the building
blocks of a program and are used to express the logic and flow of the program.
These statements can call a method or function, loop or branch through blocks of code or
assign values or expression to a variable or constant.

Branching Statements
These can be either conditional statements (such as If or Select Case) or unconditional
(such as Call and Exit).
CHAPTER 3
HANDLING MOUSE EVENTS

MouseEnter: This event is raised when the mouse pointer enters the bounds of a control. It
is typically used to perform actions such as changing the cursor or highlighting the control.
MouseMove: This event is raised when the mouse pointer moves within the bounds of a
control. It is typically used to perform actions such as updating the position of a tooltip or
tracking the mouse pointer.
MouseHover: This event is raised when the mouse pointer hovers over a control for a
specific amount of time. It is typically used to perform actions such as displaying a tooltip or
showing a context menu.
MouseLeave: This event is raised when the mouse pointer leaves the bounds of a control.
It is typically used to perform actions such as resetting the cursor or turning off highlighting.

HANDLING KEYBORD EVENTS


 Handled - indicates if the KeyPress event is handled
 KeyChar - stores the character corresponding to the key pressed
CHAPTER 4 exception handling
An exception is a problem that arises during the execution of a program.
 In the .Net Framework, exceptions are represented by classes.
 The exception classes in .Net Framework are mainly directly or indirectly
derived from the System.Exception class.
 Exceptions are thrown when an error occurs, and they can be caught and
handled by the program to prevent it from crashing or behaving unexpectedly.
Syntax Errors
 Syntax errors are those that appear while you write code.
 Visual Basic checks your code as you type it in the Code Editor window and
alerts you if you make a mistake
Run-Time Errors
 Run-time errors are those that appear only after you compile and run your code.
 These involve code that may appear to be correct in that it has no syntax errors,
but that will not execute.
 For example, you might correctly write a line of code to open a file. But if the
file is corrupted, the application cannot carry out the open function, and it stops
running
CHAPTER 4
Logic Errors
 Logic errors are those that appear once the application is in use.
 They are most often unexpected results in response to user actions.
 For example, a mistyped key or other outside influence might cause your application to
stop working within expected parameters, or all together.
 Logic errors are generally the hardest type to fix, since it is not always clear where they
originate.
TRY CATCH
 Try: A Try block identifies a block of code for which particular exceptions will be
activated. It's followed by one or more Catch blocks.
 Try statement provides the code that is being tested for exceptions.
 Catch: A program catches an exception with an exception handler at the place in a
program where you want to handle the problem.
 The Catch keyword indicates the catching of an exception.
 Used to handled the exception.
 to handle errors that occur in the associated Try block.
 Finally: The Finally block is used to execute a given set of statements, whether an
exception is thrown or not thrown.
 It must be preceded by catch or try block.
CHAPTER 4
Exception handling allows a program to continue running and to recover
from errors, rather than crashing or behaving unexpectedly.

UNSTRUCTURED EXCEPTION
 In unstructured exception handling, an On Error statement at the
beginning of the code handles all exceptions.
 an On Error statement at the beginning of the code handles all exceptions.

In unstructured exception handling, an On Error statement at the beginning


of the code handles all exceptions that occur within the scope of the
procedure. This means that any exceptions that are thrown within the
procedure will be handled by the specified error-handling routine, regardless
of where they occur.
CHAPTER 4
There are two main types of exception handling: structured exception handling and
unstructured exception handling.

Structured exception handling is a mechanism that uses specific language constructs, such as
Try, Catch, and Finally blocks, to handle exceptions.
The Try block contains code that might throw an exception. If an exception is thrown, the
runtime environment looks for a matching Catch block to handle the exception. If a matching
Catch block is found, the code in the block is executed to handle the exception. If no
matching Catch block is found, the exception is propagated up the call stack until it is
handled or the program crashes.

The Finally block contains code that is executed regardless of whether an exception was
thrown. This can be useful for cleaning up resources or performing other tasks that should
always be executed, even if an exception occurs.

Unstructured exception handling, on the other hand, is a mechanism that uses general-
purpose language constructs, such as If statements and Goto statements, to handle exceptions.
Unstructured exception handling is typically used in older programming languages that do
not have specific language constructs for exception handling.
CHAPTER 4
Breakpoints—F9
 Breakpoint is used to notify debugger where and when to pause the execution of
program.
 You can put a breakpoint in code by just pressing F9 at the front of the line.
Step Over--F10
 Step Over will execute the entire method/hole function at a time.
 You cannot see how the method was executed.
Step Into--F11
 similar to Step Over.
 if the current highlighted section is any methods call, the debugger will go
inside the method.
 you can see how the method is executing line by line till it returns
 See the execution in detail manner.
Step Out --Shift - F11
 Using Shift + F11 Return from called function to calling function
CHAPTER 6
ADO is a rich set of classes, interfaces, structures, and enumerated types that
manage data access from various types of data stores such as elational databases,
XML documents, and web services. It supports both connected and disconnected
data access, allowing you to work with data in a variety of ways.
There were many data access technologies available prior to ADO.NET, primarily
the following:
 Open Database Connectivity (ODBC)
 Data Access Objects (DAO)
 Remote Data Objects (RDO)
 Active X Data Objects (ADO)

NNNNBBB: ODBC uses a connected data access model, in which a connection to


the data source is maintained for the duration of the data access operation.
ADO.NET, on the other hand, supports both connected and disconnected data
access, allowing you to work with data in a variety of ways.
CHAPTER 6
ADO.NET CONNECTION STRING
• Example:
 "Data Source=DESKTOP-UJEVO9U\SQLEXPRESS;Initial Catalog=IT;Integrated
Security=True"
 Connection String is a normal String representation
 which contains Database connection information to establish the connection between
Database and the Application.
Connection String includes parameters such as:-
 Name of the driver,
 Server name and Database name ,
 as well as security information

 ADO.NET provides mainly the following two types of architectures:


1. Connected Architecture
2. Disconnected Architecture
CHAPTER 6
CONNECTED
 In the connected architecture, connection with a data source is kept open constantly for
data access as well as data manipulation operations.
 The ADO.NET Connected architecture considers mainly three types of objects.
 SqlConnection con;
 SqlCommand cmd;
 SqlDataReader dr;

DISCONNECTED : main feature of asp.net


 The .NET application does not always stay connected with the database. The classes are
designed in a way that they automatically open and close the connection.
 The data is stored client-side and is updated in the database whenever required.
 The ADO.NET Disconnected architecture considers primarily the following types of
objects:
 DataSet ds;
 SqlDataAdapter da;
 SqlConnection con;
 SqlCommandBuilder bldr;
CHAPTER 6
 The two key components of ADO.NET are
 Data Providers and DataSet.

DATA PROVIDER
 A data provider is used for
 Connecting to a database
 Executing commands storing it in a dataset,
 Retrieving data
 Reading the retrieved data and
 Updating the database
 To place the data into Dataset to use it further in our application.
CHAPTER 6
The data provider in ADO.NET consists of the following four objects:
1. Connection
 is used to set up a connection with a data source.
2. Command
 A command is a SQL statement or a stored procedure used to :
 Retrieve, insert, delete or modify data in a data source.
 Execute SQL statement for SQL Server database.
3. DataReader
 Datareader is used to retrieve data from a data source in
 a read-only and
 forward-only mode.
 do not update the data.
4. DataAdapter
 This is integral to the working of ADO.NET since data is transferred to and from a
database through a data adapter.
 Is a bridge between the Dataset and the Data Source. In ADO.NET, a DataSet is an in-
memory cache of data that can be manipulated and updated independently of the data
source. DataSets are used to represent and manage data in a disconnected manner,
allowing you to work with data without maintaining a constant connection to the data
CHAPTER 6
The .NET Framework provides mainly the following data providers for
ADO.NET :
1. Microsoft SQL Server--provides access to Microsoft SQL Server.
 Requires System.Data.SqlClient namespace.
2. OLEDB- provides access to data sources exposed by using OLE DB.
 Requires System.Data.OleDb namespace.
3. ODBC-provides access to data sources exposed by ODBC.
Requires System.Data.Odbc namespace.
4. .NET Framework Data Provider for Oracle
 It is used for Oracle data sources.
 It uses the System.Data.OracleClient namespace.
5. EntityClient Provider
 provides data access for Entity Data Model applications.
 requires the System.Data.EntityClient namespace.
CHAPTER 6
DATA ADAPTER

 A Data Adapter object accesses data in a disconnected mode. Its


object contains a reference to a connection object.
 It is designed in a way that implicitly opens and closes the
connection whenever required.
 It maintains the data in a Dataset object. The user can read the data
if required from the dataset and write back the changes in a single
batch to the database.
 Additionally, the Data Adapter contains a command object
reference for Select, Insert, Update, and Delete operations on the
data objects and a data source.
CHAPTER 6
A Data Adapter supports mainly the following two methods:-
Fill ()
 The Fill method populates a dataset or a data table object with data
from the database.
 It retrieves rows from the data source using the Select statement
specified by an associated select command property.
Update ()
 The Update method commits the changes back to the database.
 It also analyzes the RowState of each record in the Dataset and
calls the appropriate Insert, Update, and Delete statements.
 i.e. A Data Adapter object is formed between a disconnected
ADO.NET object and a data source.
CHAPTER 6
DATASET

 In the disconnected scenario, the data retrieved from the database is


stored in a local buffer called Dataset.
 It is explicitly designed to access data from any data source.
 A Data Set object is an in-memory representation of the data. It is
specially designed to manage data in memory and to support
disconnected operations on data.
 It is a collection of data tables that contain the data.
 Represents data retrieved from a data source
 The DataSet class is present in the System.Data namespace.
CHAPTER 6
The following table describes all the components of Dataset:
DataTableCollection
 It contains all the tables retrieved from the data source.
DataRelationCollection
 It contains relationships and the links between tables in a data set.
ExtendedProperties
 It contains additional information, like the SQL statement for
retrieving data…
DataTable
It represents a table in the DataTableCollection of a dataset.
 It consists of the DataRow and DataColumn objects.
 The DataTable objects are case-sensitive.
DataRelation, DataRowCollection, DataView, DataRow,
DataCoulmn etc…….
CHAPTER 6
Database connections do not automatically close when an ASP.NET
program ends.

 ExecuteNonQuery() method of the SqlCommand object:


executes commands against a database.
 Used for inserting, updating, or deleting rows in a SQL Server
database.
 Does not return a record set of data.
CHAPTER 6
CHAPTER 6
CHAPTER 6
CHAPTER 6

You might also like