0% found this document useful (0 votes)
14 views

Module 3

Uploaded by

mr.arjunb27
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Module 3

Uploaded by

mr.arjunb27
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT

Department of MCA
Advanced Programming– 21MCA3041
Module 3: Delegates, Events, Exception Handling
and ADO.NET
Programming
Handling By
Nirupama K
Asst. Prof. Dept. of MCA
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Delegates
• Delegates is a special type of object that contains the details of a method rather than data
• Delegate is a class type object which is used to invoke a method that has been encapsulated into
it at the time of its creation
• Delegates can be used to hold the reference to a method of any class
Creating and using delegates
• Declaring delegate
• Defining delegate method
• Creating delegate object
• Invoking delegate object
• Declaring delegate
[access modifier] delegate return-type delegate-name (parameters-list);
public delegate int MyDelegate (string s);

04/04/2024 2
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Defining delegate method
static void MethodA(string message)
{
Console.WriteLine("Called ClassA.MethodA() with parameter: " + message);
}
Creating delegate object
delegate-name Object-name = new delegate-name(expression)
Invoking Delegate
Delegate-object(arguments-list)
Multicaste Delegates
• So far we have seen delegate object that have created to hold reference of single method
• We can also invoke multiple delegate that is known as multicaste delegate

04/04/2024 3
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Event
• Events are user actions such as key press, clicks, mouse movements, etc., or some occurrence
such as system generated notifications.
• Applications need to respond to events when they occur. For example, interrupts. Events are used
for inter-process communication.
• The events are declared and raised in a class and associated with the event handlers using
delegates within the same class or some other class
• An event is a notification sent by an object to signal the occurrence of an action. Events in .NET
follow the observer design pattern.
• Syntax of event
access modifier event-type event-name;
Class-object.event-name += new class-object .delegate-name(method-name);
Class-object.event-name -= new class-object .delegate-name(method-name);
this.MyEvent += new MyDel(this.WelcomeUser);

04/04/2024 5
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Exception handling
An exception is a problem that arises during the execution of a program. A C# exception is a
response to an exceptional circumstance that arises while a program is running, such as an attempt
to divide by zero.
Exceptions provide a way to transfer control from one part of a program to another. C# exception
handling is built upon four keywords: try, catch, finally, and throw.
try − A try block identifies a block of code for which particular exceptions is activated. It is followed
by one or more catch blocks.
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.
finally − The finally block is used to execute a given set of statements, whether an exception is
thrown or not thrown. For example, if you open a file, it must be closed whether an exception is
raised or not.
throw − A program throws an exception when a problem shows up. This is done using a throw
keyword.
04/04/2024 6
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Control flow statement
if (i > 0)
Console.WriteLine("The number {0} is positive", i);
//if-then-else selection
if (i > 0)
Console.WriteLine("The number {0} is positive", i);
else
Console.WriteLine("The number {0} is not positive", i);
//multicase selection
if (i == 0)
Console.WriteLine("The number is zero");
else if (i > 0)
Console.WriteLine("The number {0} is positive", i);
else
Console.WriteLine("The number {0} is negative", i);
04/04/2024 7
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
switch (strVal1)
{ case "reason1":
goto case "reason2"; // this is a jump to mimic fall-through
case "reason2":
intOption = 2;
break;
case "reason 3":
intOption = 3;
break;
case "reason 4":
intOption = 4;
break;
default:
intOption = 9;
break;
} 04/04/2024 8
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
//find out the number of digits in a given number
int i = 123;
int count = 0;
int n = i;

//while loop may execute zero times


while (i > 0)
{
++count;
i = i / 10;
}
Console.WriteLine("Number {0} contains {1} digits.", n, count);

04/04/2024 9
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
//find out the number of digits in a given number
int i = 0;
int count = 0;
int n = i;
do
{
++count;
i = i / 10;
} while (i > 0);
Console.WriteLine("Number {0} contains {1} digits.", n, count);

04/04/2024 10
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
//For loop with break and continue statements
for (int i = 0; i < 20; ++i)
{
if (i == 10)
break;
if (i == 5)
continue;
Console.WriteLine(i);
}
//foreach loop
string[] a = { "Chirag", "Bhargav", "Tejas" };
foreach (string b in a)
Console.WriteLine(b);

04/04/2024 11
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
For each
Syntax:
foreach(data_type var_name in collection_variable)
{
// statements to be executed
}
• It is necessary to enclose the statements of foreach loop in curly braces {}.
• Instead of declaring and initializing a loop counter variable, you declare a variable that is the same
type as the base type of the array, followed by a colon, which is then followed by the array name.
• In the loop body, you can use the loop variable you created rather than using an indexed array
element.
string[] cars = {"Volvo", "BMW", "Ford", "MazdaRX800"};
foreach (string i in cars)
{
Console.WriteLine(i);
} 04/04/2024 12
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Understanding of ADO .Net
• ADO .Net is the data access and manipulation protocol used by C#
• It is a disconnected architecture
• ADO.NET leverages the power of XML to provide disconnected access to data. ADO.NET is made
of a set of classes that are used for connecting to a database, providing access to relational data,
XML, and application data, and retrieving results.
• Disconnected architecture refers to the mode of architecture in Ado.net where the connectivity
between the database and application is not maintained for the full time.
• ADO .Net provides set of components to create distributed applications
• ADO .Net classes found in System.Data.dll
• LINQ to DataSet- LINQ to SQL-
• Connection, dataadapter object
• Dataset object
• Datatable object
• A DataAdapter is used to retrieve data from a data source and populate tables within a DataSet.
04/04/2024 13
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
• Use the CommandText method to assign a SQL statement to the command and then execute the
SQL by calling the ExecuteNonQuery method.

04/04/2024 14
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Describing the architecture of ADO .Net

04/04/2024 15
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Object of Data Provider
• Connection
• Command
• DataReader
• DataAdapter
Data Provider in ADO .Net
• .Net Framework Data Provider for SQL server
• .Net Framework Data Provider for OLE DB
• .Net Framework Data Provider for ODBC
• .Net Framework Data Provider for Oracle
Components of Data set
• Data Table
• Data View
• Data Column
• Data Row
• Data Relation
04/04/2024 16
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Creating connection strings
Data Source=C:\Users\Sai\Documents\Lab2.sdf
Syntax: connetionString="Data Source=ServerName;Initial Catalog=Databasename;
User ID=UserName;Password=Password“
Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Sai\Documents\Lab1.mdf;Integrated
Security=True;Connect Timeout=30;User Instance=True

04/04/2024 17
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
• Creating a command object
SqlDataAdapter SDA = new SqlDataAdapter("insert into Course(course_id,course_name) values ('"
+textBoxcid.Text + "','" + textBoxcn.Text + "')", con);
• Working with Data Adapter
The Data Adapter is the interface between the Data Set and the database itself
Means data adapter is responsible for reading data from database
. OLE DB Data Adapter
. ODBC Data Adapter
. SQL Data Adapter
. Oracle Data Adapter
• Creating dataset from data adapter
• Updating with data adapter
• Creating data view

04/04/2024 18
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT

04/04/2024 19

You might also like