You should save the database in the "Bin" folder.
Here are the codes:
CODES FOR MODULE1
===================================
Imports System.Data.OleDb
________________________________________________________________
Module Module1
Dim Provider As String
Public OleCn As New OleDbConnection()
Public OleDa As New OleDbDataAdapter()
________________________________________________________________
Public Function OledbConnectionString() As String
Provider = "provider=microsoft.jet.oledb.4.0; data source=..\Database1.mdb"
Return Provider
End Function
________________________________________________________________
Public Sub opencon()
If OleCn.State <> ConnectionState.Open Then
OleCn.ConnectionString = OledbConnectionString()
OleCn.Open()
End If
End Sub
________________________________________________________________
Public Sub Initialized()
OleDa.SelectCommand = New OleDbCommand
OleDa.SelectCommand.CommandText = "SELECT * FROM tblemp"
OleDa.SelectCommand.Connection = OleCn
End Sub
________________________________________________________________
Public Sub closecon()
OleCn.Close()
End Sub
End Module
CODES FOR FORM1
============================
Imports System.Data.OleDb
_________________________________________________________________
Public Class Form1
Dim OleDr As OleDbDataReader
Dim Item As New ListViewItem
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call opencon()
Call LoadLv()
Call closecon()
End Sub
_________________________________________________________________
Public Sub LoadLv()
ListView1.Items.Clear()
Call Initialized()
OleDr = OleDa.SelectCommand.ExecuteReader()
Do While OleDr.Read()
Item = ListView1.Items.Add(OleDr("EmployeeID").ToString())
Item.SubItems.Add(OleDr("FirstName").ToString())
Item.SubItems.Add(OleDr("LastName").ToString())
Item.SubItems.Add(OleDr("Gender").ToString())
Loop
OleDr.Close()
End Sub
_________________________________________________________________
Private Sub cmdadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdadd.Click
Form2.ShowDialog()
End Sub
End Class
CODES FOR FORM2
================================
Imports System.Data.OleDb
_________________________________________________________________
Public Class Form2
Private Sub cmdsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsave.Click
If txtid.Text = "" Or txtfn.Text = "" Or txtfn.Text = "" Or cmbgender.Text = "" Then
MsgBox("Please do not leave a blank textfield!", MsgBoxStyle.Critical)
Exit Sub
End If
Try
Call opencon()
OleDa.InsertCommand = New OleDbCommand()
OleDa.InsertCommand.CommandText = "INSERT INTO tblemp (EmployeeID , FirstName , LastName , Gender) " & _
"VALUES (@EmployeeID, @FirstName, @LastName, @Gender)"
OleDa.InsertCommand.Connection = OleCn
OleDa.InsertCommand.Parameters.Add("@EmployeeID", OleDbType.VarWChar, 50, "EmployeeID").Value = txtid.Text
OleDa.InsertCommand.Parameters.Add("@FirstName", OleDbType.VarWChar, 50, "FirstName").Value = txtfn.Text
OleDa.InsertCommand.Parameters.Add("@LastName", OleDbType.VarWChar, 50, "LastName").Value = txtln.Text
OleDa.InsertCommand.Parameters.Add("@Gender", OleDbType.VarWChar, 50, "Gender").Value = cmbgender.Text
OleDa.InsertCommand.ExecuteNonQuery()
Call Form1.LoadLv()
Call closecon()
MsgBox("Record has been saved.", MsgBoxStyle.Information)
Me.Close()
Catch ex As Exception
MsgBox("Cannot be save. Existing EmployeeID", MsgBoxStyle.Critical)
Call closecon()
txtid.Focus()
txtid.SelectAll()
End Try
End Sub
________________________________________________________________
Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Call cleartext()
txtid.Focus()
Form1.ListView1.SelectedItems.Clear()
End Sub
________________________________________________________________
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub cleartext()
Me.txtid.Clear()
Me.txtfn.Clear()
Me.txtln.Clear()
End Sub
________________________________________________________________
Private Sub cmdcancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdcancel.Click
Me.Close()
End Sub
End Class
To avoid some errors, you should do the following:
-Check the name of your database.
-Check the name of your table in your database.
-The vb.net project and database file should be on the same folder.
-Check the variables if you typed them correctly.
If you have any questions, feel free to post a comment below.
No comments:
Post a Comment