Phonebook in VB.NET
Code to create a phone book in VB.NET?
Explanation
Phonebook in VB.NET
Following is the code to create a
Phone book in vb.net
Code:
Imports System.Data.OleDb
Imports System.Console
Public Class Form1
Inherits System.Windows.Forms.Form
Dim cn As OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Dim icount As Integer
Dim str As String
Public head As New System.Data.DataTable
Public No As Integer = 0
Public headadaptor As New OleDbDataAdapter
Public headcommand As New OleDbCommandBuilder
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=C:Address.mdb;")
cn.Open()
str = "insert into Address values(" & CInt(TextBox1.Text) & ",
'"& TextBox2.Text & "','" & _ TextBox3.Text & "'," &
TextBox4.Text & ",'" & TextBox5.Text & "')"
cmd = New OleDbCommand(str, cn)
cmd.ExecuteNonQuery()
MessageBox.Show("Data Added!...")
cn.Close()
initial()
showRecords()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button2.Click
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=C:Address.mdb;")
cn.Open()
str = "update Address set Name='" & TextBox2.Text & "',
Address='" & TextBox3.Text & "',Phone=" & _TextBox4.Text
& " , Email='" & TextBox5.Text & "' where id="
& TextBox1.Text
cmd = New OleDbCommand(str, cn)
cmd.ExecuteNonQuery()
MessageBox.Show("Data Updated!...")
initial()
showRecords()
cn.Close()
End Sub
Sub showRecords()
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=C:Address.mdb;")
cn.Open()
cmd = New OleDbCommand("select * from Address", cn)
dr = cmd.ExecuteReader
While dr.Read()
TextBox1.Text = dr(0)
TextBox2.Text = dr(1)
TextBox3.Text = dr(2)
TextBox4.Text = dr(3)
TextBox5.Text = dr(4)
End While
dr.Close()
cn.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=C:Address.mdb;")
cn.Open()
initial()
End Sub
Sub initial()
headadaptor = New OleDbDataAdapter("select * from
Address", cn)
headcommand = New OleDbCommandBuilder(headadaptor)
headadaptor.Fill(head)
display()
End Sub
Sub display()
If (No <= head.Rows.Count - 1) Then
TextBox1.Text = head.Rows(No)("ID").ToString
TextBox2.Text = head.Rows(No)("Name").ToString
TextBox3.Text = head.Rows(No)("Address").ToString
TextBox4.Text = head.Rows(No)("Phone").ToString
TextBox5.Text = head.Rows(No)("Email").ToString
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button3.Click
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=C:Address.mdb;")
cn.Open()
initial()
str = "delete from Address where id=" & TextBox1.Text
cmd = New OleDbCommand(str, cn)
cmd.ExecuteNonQuery()
MessageBox.Show("Data Deleted!...")
head = New DataTable
headadaptor.Fill(head)
No = No - 1
showRecords()
initial()
cn.Close()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button4.Click
No = 0
display()
End Sub
Private Sub Button6_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button6.Click
If (No = head.Rows.Count - 1) Then
MsgBox("this is last")
Else
No = No + 1
display()
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button5.Click
If (No = 0) Then
MsgBox("this is first")
Else
No = No - 1
display()
End If
End Sub
Private Sub Button7_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button7.Click
No = head.Rows.Count - 1
display()
End Sub
End Class
Description:
- In the above phone book example,three buttons are used to "ADD", "EDIT", "DELETE" to modify the phonebook.
- The records of the phonebook are stored in 'Address.mdb' database.
- Four buttons are used to traverse to First, Last, Next and Previous Records.
- While updating a record the "ID" has to a unique ID to avoid duplication.