Upload
akber-khowaja
View
313
Download
1
Embed Size (px)
Citation preview
.NET Technology
ByAkber Ahmed Khowaja
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
What is in Language?
Interaction With Other Language Platform Support Programming Technique (structured ? Procedural? OOP?)
Databases Support Database Handling Technique Thread Model Component Designing?
Network Support Programming? (Socket Programming)
Internet Programming ? Future / Reliability?
What is new in .NET Technology
.NET Framework covers all the layers of software development from the operating system up
.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
Windows Forms (Winforms)
Windows Form are a new way to create Standard WIN32 Desktop Application based on the Windows Foundation Classes(WFC)
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.
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
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)
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
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.
Common Types
Boolean Sbyte (Signed Byte)Byte SingleCharDate/TimeDecimalDoubleInt16Int32Int46
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
Programming Technique
OOP (Object Oriented Programming)
a) Encapsulation
b) Inheritance
c) Polymorphism
Form Level Inheritance
1 2
1 2
3 4
Form 1 Form2 inherited from Form1
Database Handling
It use ADO+ or ADO.NET To handle different Databases
Disconnected Databases
SQL
OLEDB Drivers
(Will be discuss in Detail)
XML Support
It Gives XML support to represent Fast Data
VB.NET Programming
By:Akber Ahmed Khowaja
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
VS.NET Environment
Form Properties
Database Handling With ADO.NET
Import System.Data Import System.data.ADO
Create and set SQLConnection Create and set SQL DataAdapter Create and set Dataset
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
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
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
Form Design Layout
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
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
Todays Discussion
Few Language Tips How To search Working wit DataGrid Add Records Save Records
Few Language Changes
Data Types Declaration Methods Type Casting Option keyword Changes in Array
Data Types (Integers)
Byte 8 Bits 0 to 255 Short Integer Long
Floats
Single Double Decimal 128 bits
Others
Char String Varient Object
Declaration of Variables
Dim I , j , k as integer
Option
Option Explicit On/Off Option Compare Binary/Text Option Strict On/Off (Data type conversion)
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)
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
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
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.
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
Form Level Inheritance
Put Form1 as Following
Insert Another Form
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
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
Come back to Database
Master Details Forms
Database Structure
Put Sample Data
Form Structure
ListBox Properties, Datagrid Prop.
Code on ListIndexChanged
ds2.Clear()
da2.SelectCommand.CommandText = "select * from ghufi_details where id=" &
CInt(sender.Text)
da2.Fill(ds2, "ghufi_details")
output
VB.Net Programming
ByAkber Ahmed Khowaja
DialogBox
FileDialogBox SaveDialogBox FontDialogBox ColorDialogBox PrintDialogBox
Screen For Dialog Boxes
Open
Save
Font
Color
Today’s Discussion (18/11/2002)
Add New Record with Programming Edit Record Delete Record
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
objreader.Close() objreader = Nothing Do Until objreader.Read = -1 TextBox1.Text = TextBox1.Text &
objreader.ReadLine & vbCrLf
Loop
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
Dim objwriter As StreamWriter = New StreamWriter(strfilename, False)
objwriter.Write(TextBox1.Text)
objwriter.Close()
objwriter = Nothing
FontDialogBox
FontDialog.ShowColor = True
FontDialog.ShowDialog()
TextBox1.Font = FontDialog.Font
Color Dialog
colorDialog.ShowDialog()
TextBox1.ForeColor = FontDialog.Color
Print Dialog
PrintDialog.AllowPrintToFile = False
PrintDialog.AllowSelection = False
PrintDialog.AllowSomePages = False
PrintDialog.ShowDialog()
Menu Designing
Steps To Design Menu
There are two Types of Menues– Main Menu– Context Menu
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
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
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
Screen Layout
Import
Imports System.Data
Imports System.Data.OleDb
Imports System.DBNull
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
Initializations (call two functions)
'Add any initialization after the InitializeComponent() call
FillData()
bindFields()
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
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
Add Button Code
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
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()
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
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
Edit
cmd.CommandText = "update books set name=?,author=? where code=?"
cmd.CommandType = CommandType.Text
Delete
cmd.CommandText = “delete from books where code=?"
cmd.CommandType = CommandType.Text
Pos=1
Me.BindingContext(ds1, "ghufi").AddNew()
da1.Update(ds1, "ghufi")
da1.Fill(ds1, "ghufi")
Handling ADO With Coding
By Akber Ahmed Khowaja
Code for Import Files
Imports System.Data
Imports System.Data.OleDb
Imports System.DBNull
Public Class Form2 . . . . .
. . . . .
Variable Declaration
• For Connection
• For Data Adapter
• For Dataset
Few Slides Are Remaining
Today’s Discussion (Dec 02, 2002)
Concept Of OOP What is Class Simple Class Methods Constructors Properties Data Base Handling With Class
What is OOP
OOP is Programming technique in which we make programs Flexible and Reusable.
Basic Concept of OOP is Designing Classes in Program.
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
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
Form for Usage of “Person” class
Create Object for “Person” Class
Dim p1 As New Person()
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
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
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
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
Array Of Objects
Dim p(10) As Person Dim cntr As Integer
Screen For Use Array of Objects
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
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()
Properties For Class Property FName() As String
Get Return (XFName) End Get Set(ByVal Value As String) XFName = Value End Set
End Property
Use Properties
dim t1 as new Teacher() ‘ teacher is class
t1.FName = TextBox1.Text
t1.LName = TextBox2.Text
t1.Age = TextBox3.Text
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
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
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
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
Usage
‘Create Object Dim obj As New DataClass()
'Add any initialization after the InitializeComponent() call
obj.FillData("select * from items", "Inventory.mdb", "Items")
showData()
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
Code For Next Button_Click
obj.MoveRecord(Me.BindingContext, "Items", ">")showData()