115
.NET Technology By Akber Ahmed Khowaja

VB Dot net

Embed Size (px)

Citation preview

Page 1: VB Dot net

.NET Technology

ByAkber Ahmed Khowaja

Page 2: VB Dot net

Today’s Discussion

What We Expect From Language? What is new in .NET? Comparisons with Previous Technologies /

JAVA / VB-6 etc. First Look of VB .NET

Page 3: VB Dot net

What is in Language?

Interaction With Other Language Platform Support Programming Technique (structured ? Procedural? OOP?)

Databases Support Database Handling Technique Thread Model Component Designing?

Page 4: VB Dot net

Network Support Programming? (Socket Programming)

Internet Programming ? Future / Reliability?

Page 5: VB Dot net

What is new in .NET Technology

.NET Framework covers all the layers of software development from the operating system up

Page 6: VB Dot net

.NET Framework

ASP.NET

Web Services Web FormsASP.NET Application Service

Windows Forms

Controls DrawingWindows Application Service

.NET Framework Base Classes

ADO.NET XML Threading IONet Security Diagnoses Etc.

Common Language Runtime

Memory Management

Common Type SystemLifecycle Monitoring

Page 7: VB Dot net

Windows Forms (Winforms)

Windows Form are a new way to create Standard WIN32 Desktop Application based on the Windows Foundation Classes(WFC)

Page 8: VB Dot net

Web Forms

Web Forms provide a powerful, form based user interface for web, it also contains additional .net ASP controls which was not present in previous versions, These controls provide Sever based events facility so that our application can be develop more easily and attractive.

Page 9: VB Dot net

Web Services

Provides a mechanism for programs to communicate over the Internet using SOAP (Simple Object Access Protocol).

Web Services provide an analog of Com and DCOM for object brokering and interfacing, but based on internet technologies so that allowance is made for integration, even with non Microsoft plateforms

Page 10: VB Dot net

CLR (Common Language Runtime)

Common Type SystemData Types / etc

Intermediate Language to Native Code Compiler

Execution Support Security

Garbage Collection

Memory Management(Previous version it was done by OS)

Page 11: VB Dot net

Multiple Language Support

.Net Environment Provides Multiple language Integration Support, se that we can use multiple Languages in single program.

For Example we can C# function in ASP.NET as well as in VB.NET

Cross language Inheritance is also Possible

So Mix Language development team can also participate to develop program.

How IT is Possible ? Answer CLR

Page 12: VB Dot net

Common Type System

All Languages use same Data types, their Length, Size, Type and specificaton will be same.

For example in Previous VB-6 use 16 bit Integer (2 bytes)

In Visual C integer was 4 Bytes.

Now For all .NET Support languages use int32 Integer for 4 Bytes.

Page 13: VB Dot net

Common Types

Boolean Sbyte (Signed Byte)Byte SingleCharDate/TimeDecimalDoubleInt16Int32Int46

Page 14: VB Dot net

Execution Support

JIT (Just in Time Compilation and Execution)

JIT Compiles with three methods:

1) Standard JIT: it Convert to exe code at the time of execution

2) Pre JIT: it it convert to exe code at the time of installation.

3) Econo JIT: it create low Optimization code, for low memory resources

Page 15: VB Dot net

Programming Technique

OOP (Object Oriented Programming)

a) Encapsulation

b) Inheritance

c) Polymorphism

Page 16: VB Dot net

Form Level Inheritance

1 2

1 2

3 4

Form 1 Form2 inherited from Form1

Page 17: VB Dot net

Database Handling

It use ADO+ or ADO.NET To handle different Databases

Disconnected Databases

SQL

OLEDB Drivers

(Will be discuss in Detail)

Page 18: VB Dot net

XML Support

It Gives XML support to represent Fast Data

Page 19: VB Dot net

VB.NET Programming

By:Akber Ahmed Khowaja

Page 20: VB Dot net

Today’s Discussion

VS.NET (Visual Studio.net Environment) Working With WinForms & important Properties Working With Events in .NET Data Base Creation with MS-SQL Server Database Handling with VS.NET

Page 21: VB Dot net

VS.NET Environment

Page 22: VB Dot net
Page 23: VB Dot net
Page 24: VB Dot net

Form Properties

Page 25: VB Dot net
Page 26: VB Dot net

Database Handling With ADO.NET

Page 27: VB Dot net

Import System.Data Import System.data.ADO

Create and set SQLConnection Create and set SQL DataAdapter Create and set Dataset

Page 28: VB Dot net

Steps to Create & Set SqlConnection

1. Place SqlConnection To Form (from Data toolBox)

2. Set its name property to con13. Set its Connection Property using connection

Wizard

Page 29: VB Dot net

Steps to Create & Set DataAdapter

1. Put DataAdapter Control to form2. Right click on That Control3. Select Configure4. Follow Steps to complete Process5. Right Click on DataAdapter Control, and

select Create DATASET

Page 30: VB Dot net

Put Following Code on Form’s Load Event

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

da1.Fill(ds1, "ghufi")

End Sub

Page 31: VB Dot net

Form Design Layout

Page 32: VB Dot net

Private Sub cmdLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLast.Click

cnt = Me.ds1.Tables("ghufi").Rows.Count

Me.BindingContext(ds1, "ghufi").Position = Me.BindingContext(ds1, "ghufi").Count - 1

MsgBox(cnt.ToString)

End Sub

Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNext.Click

Me.BindingContext(ds1, "ghufi").Position = Me.BindingContext(ds1, "ghufi").Position + 1

End Sub

Page 33: VB Dot net

Private Sub cmdPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrev.Click

Me.BindingContext(ds1, "ghufi").Position = Me.BindingContext(ds1, "ghufi").Position - 1

End Sub

Private Sub cmdFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFirst.Click

Me.BindingContext(ds1, "ghufi").Position = 0

End Sub

Page 34: VB Dot net

Todays Discussion

Few Language Tips How To search Working wit DataGrid Add Records Save Records

Page 35: VB Dot net

Few Language Changes

Data Types Declaration Methods Type Casting Option keyword Changes in Array

Page 36: VB Dot net

Data Types (Integers)

Byte 8 Bits 0 to 255 Short Integer Long

Page 37: VB Dot net

Floats

Single Double Decimal 128 bits

Page 38: VB Dot net

Others

Char String Varient Object

Page 39: VB Dot net

Declaration of Variables

Dim I , j , k as integer

Page 40: VB Dot net

Option

Option Explicit On/Off Option Compare Binary/Text Option Strict On/Off (Data type conversion)

Page 41: VB Dot net

Find Records

On Load

txtfind.Text = ds1.ghufi.Columns.Count() ComboBox1.Items.Add(ds1.ghufi.Columns(0).ColumnName) ComboBox1.Items.Add(ds1.ghufi.Columns(1).ColumnName) ComboBox1.Items.Add(ds1.ghufi.Columns(2).ColumnName) ComboBox1.Items.Add(ds1.ghufi.Columns(3).ColumnName)

Page 42: VB Dot net
Page 43: VB Dot net

Write Following Code under Find

Dim fldname As Stringfldname = ComboBox1.SelectedItem()ds1.Clear()da1.SelectCommand.CommandText =

"select * from ghufi where " & fldname & "='" & txtfind.Text & "'“da1.Fill(ds1, "ghufi")

If Me.BindingContext(ds1, "ghufi").Count <= 0 ThenMsgBox("Sorry No Record for your Search")

End If

Page 44: VB Dot net

Today’s Discussion (03/11/2002)

New Control “Notify Icon” How to Navigate B/W Forms Inheritance in Forms How User Master Details Add / Save

Page 45: VB Dot net

NotifyIcon Control

Notify Icon is a new control which put a link on System Tray, it automatically creates a new panel in system tray and shows Icon.

It’s Click Event Fires when we double click on that Icon.

We may use it for new form load, or terminate Application.

Page 46: VB Dot net

Steps to use NotifyTray

Drag and drop this control on Form Set it’s Icon Property with any suitable Icon Write “End” to its click event

Page 47: VB Dot net

Form Level Inheritance

Page 48: VB Dot net

Put Form1 as Following

Page 49: VB Dot net

Insert Another Form

Page 50: VB Dot net

Its coding is

Public Class Form4

Inherits System.Windows.Forms.FormPrivate Sub Form4_Load(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles MyBase.Load

End Sub

End Class

Page 51: VB Dot net

Change this coding as

Public Class Form4

Inherits System.amin.form3Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles MyBase.Load

End Sub

End Class

Set form4 as Default For and Run Program

Page 52: VB Dot net

Come back to Database

Master Details Forms

Page 53: VB Dot net

Database Structure

Page 54: VB Dot net

Put Sample Data

Page 55: VB Dot net

Form Structure

Page 56: VB Dot net

ListBox Properties, Datagrid Prop.

Page 57: VB Dot net

Code on ListIndexChanged

ds2.Clear()

da2.SelectCommand.CommandText = "select * from ghufi_details where id=" &

CInt(sender.Text)

da2.Fill(ds2, "ghufi_details")

Page 58: VB Dot net

output

Page 59: VB Dot net

VB.Net Programming

ByAkber Ahmed Khowaja

Page 60: VB Dot net

DialogBox

Page 61: VB Dot net

FileDialogBox SaveDialogBox FontDialogBox ColorDialogBox PrintDialogBox

Page 62: VB Dot net

Screen For Dialog Boxes

Open

Save

Font

Color

Print

Page 63: VB Dot net

Today’s Discussion (18/11/2002)

Add New Record with Programming Edit Record Delete Record

Page 64: VB Dot net

OpenFileDialogBox

Imports System.IO openDialog.Filter = "Text Files (*.txt)|*.txt|All Files|*.*“ openDialog.FilterIndex = 1 openDialog.InitialDirectory = "c:\“openDialog.Title = "Demo Open Dialog“openDialog.ShowDialog()strfilename = openDialog.FileNameDim objreader As StreamReader = New StreamReader(strfilename)

TextBox1.Text = objreader.ReadToEnd

Page 65: VB Dot net

objreader.Close() objreader = Nothing Do Until objreader.Read = -1 TextBox1.Text = TextBox1.Text &

objreader.ReadLine & vbCrLf

Loop

Page 66: VB Dot net

SaveDialog & FileStreamWriter

With saveDialog .DefaultExt = "txt“ .FileName = strfilename .Filter = "Text Files(*.txt)|*.txt|All Files|*.*“

.FilterIndex = 1 .InitialDirectory = "c:\“ .OverwritePrompt = True .Title = "File Save Demo“End WithsaveDialog.ShowDialog()strfilename = saveDialog.FileName

Page 67: VB Dot net

Dim objwriter As StreamWriter = New StreamWriter(strfilename, False)

objwriter.Write(TextBox1.Text)

objwriter.Close()

objwriter = Nothing

Page 68: VB Dot net

FontDialogBox

FontDialog.ShowColor = True

FontDialog.ShowDialog()

TextBox1.Font = FontDialog.Font

Page 69: VB Dot net

Color Dialog

colorDialog.ShowDialog()

TextBox1.ForeColor = FontDialog.Color

Page 70: VB Dot net

Print Dialog

PrintDialog.AllowPrintToFile = False

PrintDialog.AllowSelection = False

PrintDialog.AllowSomePages = False

PrintDialog.ShowDialog()

Page 71: VB Dot net

Menu Designing

Page 72: VB Dot net

Steps To Design Menu

There are two Types of Menues– Main Menu– Context Menu

Page 73: VB Dot net

Main Menu

This Menu Appear Normally on the Top of the form, this is very common menu for windows base applications.

ie. File, Edit, View etc

Context MenuThis menu is Small Menu Which appears when user right click on Any Form Item

Page 74: VB Dot net

Steps To Design Main Menu

Drag and drop main menu from Toolbox Write Menu Name Use & for Access Keys Set Properties of every menu

– Name– Shortcut

Page 75: VB Dot net

Steps Context Menu

Drag and drop Context menu from ToolBox Design some menu items

Every Control has Context menu property, we bind context menu property to context menu, so that menu will appear when user right click on that menu

Page 76: VB Dot net

Screen Layout

Page 77: VB Dot net

Import

Imports System.Data

Imports System.Data.OleDb

Imports System.DBNull

Page 78: VB Dot net

DeclarationsPublic Class Form2

Inherits System.Windows.Forms.Form

Public con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0; data source=d:\LibrarySystem\library.mdb")

Public da As OleDbDataAdapterPublic ds As DataSetPublic dw As DataViewPublic cm As CurrencyManager

Page 79: VB Dot net

Initializations (call two functions)

'Add any initialization after the InitializeComponent() call

 

FillData()

bindFields()

Page 80: VB Dot net

FillData Function Private Sub FillData() dw = Nothing ds = New DataSet() con.Open() da = New OleDbDataAdapter("select * from books", con) da.Fill(ds, "books") dw = New DataView(ds.Tables("books")) cm = CType(Me.BindingContext(dw), CurrencyManager) con.Close() End Sub

Page 81: VB Dot net

DataBinding Function

Private Sub bindFields()

TextBox1.DataBindings.Clear()

TextBox2.DataBindings.Clear() 

TextBox1.DataBindings.Add("text", dw, "code") TextBox2.DataBindings.Add("text", dw, "Name")

End Sub

Page 82: VB Dot net

Add Button Code

TextBox1.Text = ""

TextBox2.Text = ""

TextBox3.Text = ""

TextBox4.Text = ""

Page 83: VB Dot net

Save Button

Dim pos As Integer pos = cm.Position Dim cmd As New OleDbCommand()

cmd.Connection = con

cmd.CommandText = "Insert into books (code, name, author) values(?,?,?)“

cmd.CommandType = CommandType.Text

con.Open()

Page 84: VB Dot net

cmd.Parameters.Add(New OleDbParameter())cmd.Parameters.Item(0).Direction = ParameterDirection.Inputcmd.Parameters.Item(0).DbType = DbType.Stringcmd.Parameters.Item(0).Size = 10cmd.Parameters.Item(0).Value = TextBox1.Text

Page 85: VB Dot net

Try cmd.ExecuteNonQuery()Catch ex As Exception MsgBox("Error:=" & ex.Message)End Try con.Close() FillData() bindFields() MsgBox("Record Added") ds = Nothing da = Nothingcmd = NothingEnd sub

Page 86: VB Dot net

Edit

cmd.CommandText = "update books set name=?,author=? where code=?"

cmd.CommandType = CommandType.Text

 

Page 87: VB Dot net

Delete

cmd.CommandText = “delete from books where code=?"

cmd.CommandType = CommandType.Text

 

Pos=1

Page 88: VB Dot net

Me.BindingContext(ds1, "ghufi").AddNew()

da1.Update(ds1, "ghufi")

da1.Fill(ds1, "ghufi")

Page 89: VB Dot net

Handling ADO With Coding

By Akber Ahmed Khowaja

Page 90: VB Dot net

Code for Import Files

Imports System.Data

Imports System.Data.OleDb

Imports System.DBNull

 

Public Class Form2 . . . . .

. . . . .

Page 91: VB Dot net

Variable Declaration

• For Connection

• For Data Adapter

• For Dataset

Page 92: VB Dot net

Few Slides Are Remaining

Page 93: VB Dot net

Today’s Discussion (Dec 02, 2002)

Concept Of OOP What is Class Simple Class Methods Constructors Properties Data Base Handling With Class

Page 94: VB Dot net

What is OOP

OOP is Programming technique in which we make programs Flexible and Reusable.

Basic Concept of OOP is Designing Classes in Program.

Page 95: VB Dot net

What is Class

Class is Collection of Data / Properties and Methods/Functions which operate on that data.

Methods and Data are Encapsulated in Class, is called Encapsulation

We may Inherit functionality of one class to other class is

called Inheritance.With class one object may work with different ways is

called Polymorphism

Page 96: VB Dot net

A Simple Class

Write Click On Project add Class to your project

Public Class Person

Public FName As String

Public LName As String

Public Age As Integer

End Class

Page 97: VB Dot net

Form for Usage of “Person” class

Page 98: VB Dot net

Create Object for “Person” Class

Dim p1 As New Person()

Page 99: VB Dot net

Code for Insert Data / Show Data Buttons

p1.FName = TextBox1.Text

p1.LName = TextBox2.Text

p1.Age = CInt(TextBox3.Text)

TextBox1.Text = p1.FName

TextBox2.Text = p1.LName

TextBox3.Text = p1.Age

Page 100: VB Dot net

Encapsulation (Private Data and Access public Methods)

Public Class Person

Private FName As String Private LName As String Private Age As Integer 'setdata Method Public Sub setData(ByVal Fnm As String, ByVal LNm As String, ByVal ag As Integer)

FName = Fnm LName = LNm Age = ag End Sub

Page 101: VB Dot net

 

Public Function getFName() As String

Return (FName)

End Function

Public Function getLName() As String

Return (LName)

End Function

Public Function getAge() As Integer

Return (Age)

End Function

 End Class

Page 102: VB Dot net

Constructor

Constructor is Special Type of method which call automatically when object creates, we may use constructor for data initialization.

Public Sub New()

FName = "FName Not Set"

LName = "LName Not Set"

Age = 0

End Sub

Page 103: VB Dot net

Array Of Objects

Dim p(10) As Person Dim cntr As Integer

Page 104: VB Dot net

Screen For Use Array of Objects

Page 105: VB Dot net

Code for Set Data Button

p(cntr) = New Person()p(cntr).setData(TextBox1.Text, TextBox2.Text, CInt(TextBox3.Text))MsgBox("Data Stored as index number : " & CStr(cntr))cntr += 1 

Page 106: VB Dot net

Code for Show Data Button

Dim xx As Integer xx = txtIndex.text

TextBox1.Text = p(xx).getFName()

TextBox2.Text = p(xx).getLName()

TextBox3.Text = p(xx).getAge()

Page 107: VB Dot net

Properties For Class Property FName() As String

Get Return (XFName) End Get Set(ByVal Value As String) XFName = Value End Set

End Property

Page 108: VB Dot net

Use Properties

dim t1 as new Teacher() ‘ teacher is class

t1.FName = TextBox1.Text

t1.LName = TextBox2.Text

t1.Age = TextBox3.Text

Page 109: VB Dot net

Data base Handling with class

Public Class DataClass

Public con As OleDbConnection

Public da As OleDbDataAdapter

Public ds As DataSet

Public dw As DataView

Public cm As CurrencyManager

Page 110: VB Dot net

Method Open Connection

Public Sub openConnection(ByVal constr As String) con = New OleDbConnection(constr) con.Open()End Sub‘’’’’’’’’’’’’’’’’’’’’’’’’’’’ Private Sub setDa(ByVal sqlstr As String, ByVal cn As OleDbConnection)

da = New OleDbDataAdapter(sqlstr, cn)

End Sub

Page 111: VB Dot net

Public Sub FillData(ByVal sqlstr As String, ByVal dbnm As String, ByVal tblnm As String)

dw = Nothing ds = New DataSet() OpenConnection("Provider=Microsoft.jet.oledb.4.0; data source=f:\Inventory_VBNET\InventorySystem\" & dbnm) setDa(sqlstr, con)

da.Fill(ds, tblnm)dw = New DataView(ds.Tables(tblnm))

con.Close()

End Sub

Page 112: VB Dot net

Move Records

Public Sub MoveRecord(ByVal bc As BindingContext, ByVal tblnm As String, ByVal symbol As String)

If symbol = ">" Then bc(ds, tblnm).Position += 1 ElseIf symbol = "<" Then bc(ds, tblnm).Position -= 1 ElseIf symbol = "<<" Then bc(ds, tblnm).Position = 0 ElseIf symbol = ">>" Then bc(ds, tblnm).Position = bc(ds, tblnm).Count – 1

End If End Sub

Page 113: VB Dot net

Usage

‘Create Object Dim obj As New DataClass()

 'Add any initialization after the InitializeComponent() call

obj.FillData("select * from items", "Inventory.mdb", "Items")

showData()

Page 114: VB Dot net

Private Sub showData()

Dim pos As Integer pos = Me.BindingContext(obj.ds, "items").Position

TextBox1.Text = obj.ds.Tables("items").Rows(pos).Item("itemName")

End Sub

Show Data Method

Page 115: VB Dot net

Code For Next Button_Click

obj.MoveRecord(Me.BindingContext, "Items", ">")showData()