2. Introduction
VB.Net is developed by Microsoft
It is Visual Basic for .Net Platform
Ancestor of VB.Net is BASIC
In 1991, Microsoft added visual components to BASIC
and created Visual Basic
After the development of .Net, VB was added with more
set of controls and components and thus evolved a new
language VB.Net
Prof. Prachi Sasankar. TY-BCA-Visual Database Programming
3. Topics
Introduction to VB .Net
Creating VB .Net Project in VS 2005
Data types and Operators in VB .Net
String Functions
Conditions (If Else, Select Case)
Loops (Do While, Do Until, For)
Classes, Objects
Sub routines and Functions
Constructor and Destructor
Windows Application
Collections
File Handling
Exception Handling
Data base connectivity
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
4. Pros and Cons
Pros
Faster development of programs
Rich set of controls
Object Orientation of language enhances
modularity, readability and maintainability
Cons
Debugging larger programs is difficult, due to the
absence of an effective debugger
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
5. Features of VB .Net
Object Oriented Language
We can drag controls from the tool bar and drop
them on the form and write code for the controls
Runs on the CLR (Common Language Runtime)
Release of unused objects taken care by the CLR
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
6. Creating VB .Net Project in VS 2005
Open Visual Studio 2005
Click File -> New -> Project
Choose Language as VB .Net
Choose Type (Either Console Application or what ever
we want)
Give the path under which the project need to be saved
Click OK
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
7. Input and Output
Under the namespace System there is a class called Console
Console has 2 methods
ReadLine() – Reads a line from the user
WriteLine() – writes as line to the screen
Name spaces can be included in our code using the keyword
imports
Eg:
Imports System.Console
EX:
Dim I as Integer
I = console.ReadLine() ‘ Gets the value from user and stores it in
variable I
Console.WriteLine(I) ‘Displays the value of I
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
8. Data types and Operators in VB
.Net
Data types
Integer, string, single, double, boolean, char
Operators
Arithmetic (+,-,*,/,,Mod)
Logical (Or, And)
Relational (=,<>,<,<=,>,>=)
Prof.Prachi Sasankar. TY-
BCA-Visual Database Programming
9. String Functions
Len(str) – Length of the string str
Str.Replace(“old”,”New”) – Replaces Old with New in
the string Str
Str.Insert(pos,”string”) – Inserts the string specified at
the position pos in the string Str
Trim(str) – Removes the leading and trailing spaces in
the string str
Prof.Prachi Sasankar. TY-
BCA-Visual Database Programming
10. If Else
If (Condition)
Then
Statements executed if condition is true
Else
Statements executed if condition is false
EndIf
We can also have Else If block in If Else statements
If (a=0)
Then
Console.Writeline(“Zero”)
Else If(a<0)
Then
Console.Writeline(“Negative”)
Else
Console.Writeline(“Positive”)
EndIf
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
11. Select Case
Select Case var
Case 1
stmt1 // executed if var = 1
Case 2
stmt2 // executed if var = 2
Case Else
stmt3 // executed if var is other than 1 and 2
End Select
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
12. For Loop
For <<var>> = start To end Step <<val>>
Statements
Next
Eg
For I = 1 To 10 Step 2
Console.WriteLine(I)
Next
Here the values printed will be 1,3,5,7,9
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
13. Do While Loop
1. Do While(a<>0)
Console.Writeline(a)
a = a – 1
Loop
2. Do
Console.Writeline(a)
a = a – 1
Loop While(a<>0)
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
14. Do Until Loop
1. Do Until(a=0)
Console.Writeline(a)
a = a – 1
Loop
2. Do
Console.Writeline(a)
a = a – 1
Loop Until(a=0)
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
15. Class
Software structure that is important in Object
Oriented Programming
Has data members and methods
Blue print or proto type for an object
Contains the common properties and methods of an
object
Few examples are Car, Person, Animal
Class Person
1. Properties: Name, Height, Weight etc
2. Methods: Run, Walk, Speak
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
16. Object
Instance of a class
Gives Life to a class
Ram is an object belonging to the class Person
Ford is an object belonging to the class Car
Jimmy is an object belonging to the class Animal
Public Class Test
Dim a As Integer
Sub Print()
Console.WriteLine(“Hello”)
End Sub
Public Function add(ByVal a as Integer, ByVal b as Integer) as Integer
Return (a+b)
End Function
End Class
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
17. Subroutines and Functions
Subroutines
Does not return any value
Can have zero or more parameters
Functions
Always Returns some value
Can have zero or more parameters
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
18. Constructor
Used for initializing private members of a class
Name of the constructor should be New() always
Can have zero or more parameters
Does not return any value. So they are sub routines
Need not be invoked explicitly. They are invoked
automatically when an object is created
A class can have more than one constructor
Every constructor should differ from the other by
means of number of parameters or data types of
parameters
Constructors can be inherited
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
19. Destructor
Used for releasing memory consumed by objects
Name of the destructor should be Finalize() and the
method is already defined in CLR. So we can only
override it.
Does not return any value. Hence it is a subroutine
Does not accept any parameters
Only one destructor for a class
Destructors are not inherited
No need of explicit invocation. They are called when
the execution is about to finish.
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
20. Collections
Array List
Sorted List
Hash Table
Stack
Queue
These can be accessed via the System.Collections
namespace
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
21. Array List
Array List is a dynamic array
Its size grows with the addition of element
Searching is based on the index
Sequential search is performed on elements
Hence it is slow
Occupies less space
Dim Arr as New ArrayList()
Arr.Add(1)
Arr.Add(2)
The above code adds two elements 1 and 2 to the array list Arr
To access individual element, Arr.Item(1) – fetches 2
It is a zero based collection
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
22. Sorted List
Elements are stored as key value pairs
They are sorted according to the key
Search is based on index as well as on keys
Hence faster than Array List
Occupies Medium amount of space
Keys cannot be duplicate or null
Values can be duplicated and also can be null
Dim Sl as New SortedList()
Sl.Add(1,”One”)
Sl.Add(2,”Two”)
Sl.Item(1) // fetches 1
Sl.GetKey(1) //fetches the 1st Key,I.e. 1
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
23. Hash Table
Hash table stores data as a key value pair
Here the keys can be duplicated
Search is based on the hash of key values
Very fast way of searching elements
Occupies large space
Dim ht As New Hashtable()
ht.Add(1, “Ram”)
ht.Add(2,”Raj”)
ht.Item(2) // fetches Raj
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
24. Stack
Stack is a data structure where elements are retrieved in the
order opposite to which they are added
It implements LIFO (Last In First Out) algorithm
Its method called Push() is for adding elements
Its method Pop() is for removing elements
Dim stack As New Stack(5)
Dim i As Integer
For i = 1 To 5
stack.Push(i)
Next
For i = 1 To stack.Count
WriteLine(stack.Pop())
Next
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
25. Queue
Queue is a data structure where elements are retrieved in
the same order in which they are added
It implements FIFO (First In First Out) algorithm
Its method called Enqueue() is for adding elements
Its method called Dequeue() is for removing the elements
Dim queue As New Queue(5)
Dim i As Integer
For i = 1 To 5
queue.Enqueue(i)
Next
For i = 1 To queue.Count
WriteLine(queue.Dequeue())
Next
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
26. Windows Application
File -> New -> Project
Select Language as VB .Net
Select .Net template as Windows Application
Give the path where the application need to be saved
A application opens up with a Form
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
27. Forms and other controls
Form is a container for other controls
We can place the following in a form
Label
Text Box
Check Box
Radio Button
Button
Date Picker
And more…
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
28. Properties and Events
Every Control has a set of properties, methods and events
associated with it
Eg Form
Properties
Name (program will use this name to refer)
Text (title of the form)
Methods
Show
Hide
Close
Events
Load
UnLoad
If we hide a form, it is still in the memory. It can be shown at any time.
If we close a form, it is erased out of the memory. Same as destroying an
instance of the form. We need to recreate it if needed to see it again.
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
29. Exception Handling
Exception is Run time error
It is not known at the compile time
Examples of exceptions
Divide by zero
Accessing non existing portion of memory
Memory overflow due to looping
In VB .Net, exception is handled via Try Catch
Mechanism
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
30. Try Catch Finally
Try
Suspicious code that may throw an exception to be written
here
Catch ex As Exception
What to be done if exception occurs to be written here
Finally
Optional portion which will be executed irrespective of the
happening of exception to be written here. Can contain
code for releasing unnecessary space
Catch block with Specific exception to be given first and then
the generic type Exception
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
31. File Handling
In the System Namespace, the IO class has several
subclasses for file handling, text processing etc
Classes File, Directory are used for creating, deleting
file or folders and also has methods to play with them
Class FileStream is used for creating and manipulating
files
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
32. Reader and Writer
BinaryReader
This class has methods to read binary or raw data
BinaryWriter
This class has methods to write binary or raw data
StreamReader
This class has methods to read stream of data
StreamWriter
This class has methods to write stream of data
Stream is a channel through which an object (file
in this case) is accessed
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
33. File Handling
Open the file using FileStream class. This makes use of
FileMode and FileAccess enumeration to specify how to
open a file (for creating a new file or opening an existing
file) and whether the file is read only or write only or read
write
Associate a reader for the stream
Read the file and do the manipulations
Open a file stream for writing
Associate a writer
Write contents to file
Close the stream, so that it will be released back to the memory pool
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
35. Database Connectivity
Following are important to get connected to the database
and perform operations
Connection Object
Command Object
Operation on the Command Object
Using Dataset and Data adapter
Using Data Reader
If we use data adapter, it is called as disconnected
architecture
If we use data reader, it is called as connected architecture
Connected Architecture – An active connection to the database is
required. Connection need to be opened and closed explicitly
Disconnected Architecture – Connection management is done internally.
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
36. Connection Object
Dim con as New OdbcConnection(connectionstring)
Where connectionstring is a string that contains
details about the server where the database is located,
the name of the database, user id and password
required for getting connected and the driver details
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
37. Command Object
Dim cmd as New OdbcCommand(strCmd,con)
strCmd – is the select/insert/update/delete statement or exec
<<storedProc>> command
Con is the connection object created in the first step
Properties – cmd.CommandType
This can be either Text or StoredProcedure
Command Methods
Cmd.ExecuteReader() – Returns one or more table row(s) –
for select statement
Cmd.ExecuteScalar() – Returns a single value – for select
statement with aggregate function
Cmd.ExecuteNonQuery() – Used for executing stored
procedure or insert/update/delete statements
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
38. Data Reader and Data Set
Data Reader
Dim dr as OdbcDataReader
This dr holds the result set present in datareader object
Data Set
Dim ds as New DataSet()
ds holds the result of select statement
Data adapter is used to fill the data set
Data Adapter
Data adapter fills in the data set with the result of the select query
Dim da as New OdbcDataAdapter(cmd)
Cmd is the command object created
da.Fill(ds) fills the dataset
The data set can be set as a data source for various controls in the
web form or windows form
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
39. Connectivity:
For MS SQL server we have corresponding Connection, Command, datareader and data adapter object
They are:
SqlDataReader
SqlDataAdapter
SqlConnection
SqlCommand
For My SQL we have corresponding Connection, Command, datareader and data adapter object
They are:
OdbcDataReader
OdbcDataAdapter
OdbcConnection
OdbcCommand
For Oracle server we have corresponding Connection, Command, datareader and data adapter object
They are:
OracleDataReader
OracleDataAdapter
OracleConnection
OracleCommand
For MS Access/ Excel we have corresponding Connection, Command, datareader and data adapter object
They are:
OledbDataReader
OledbDataAdapter
OledbConnection
OledbCommand
Prof.Prachi Sasankar. TY-BCA-Visual Database Programming
Editor's Notes
VB .net is having the latest version 3.0. Gelled with ASP.net.
Comes with Intellisense. Debugging is allowed of single program at a time.