VB .NET
Slip26
Q.1) Write a VB.NET program to accept the details of customer (CName, Contact_No,
Email_id). Store it into the database with proper validation and display appropriate message by
using Messagebox.(Use MS Access ) [Marks 20]
Solution
Dim cn As New OleDb.OleDbConnection
Dim cmd As New OleDb.OleDbCommand
Dim dt As New DataTable
Dim dataadapter As New OleDb.OleDbDataAdapter
Dim str As String
Dim n As Integer
Private Sub cmdNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNew.Click
txtCName.Clear()
txtContact.Clear()
txtEmail.Clear()
txtCName.Focus()
End Sub
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
Dim a As Integer
Dim r As String
a = Asc(txtCName.Text)
If a < 65 And a > 122 Then
MsgBox("Name is Invalid,only characters are allowed")
Exit Sub
Else
If Not IsNumeric(txtContact.Text) Then
MsgBox("Invalid Phone Number, only numbers are allowed")
Exit Sub
Else
r = InStr(1, txtEmail.Text, "@", vbTextCompare)
If r = 0 Then
MsgBox("Invalid Email")
txtEmail.Text = ""
txtEmail.Focus()
Exit Sub
Else
r = InStr(1, txtEmail.Text, ".", vbTextCompare)
If r = 0 Then
MsgBox("Invalid Email")
txtEmail.Text = ""
txtEmail.Focus()
Exit Sub
Else
cn = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\VB.NET\slip26\customer.accdb")
cn.Open()
str = "insert into customer values('" & txtCName.Text & "','" & txtContact.Text & "','" & txtEmail.Text & "')" cmd = New OleDb.OleDbCommand(str, cn)
n = cmd.ExecuteNonQuery
If (n > 0) Then
MsgBox("All fields are validated & Record Inserted Successfully")
End If
End If
End If
End If
End If
cn.Close()
End Sub
Private Sub cmdShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdShow.Click
DataGridView1.DataSource = Nothing
cn = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\VB.NET\slip26\customer.accdb")
cn.Open()
str = "select * from customer"
cmd = New OleDb.OleDbCommand(str, cn)
dataadapter = New OleDb.OleDbDataAdapter(cmd)
dataadapter.Fill(dt)
DataGridView1.DataSource = dt
End Sub
End Class
Tags:
VB .NET