Imports System.Data.
OleDb
Public Class Form8
Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\Users\nilka\Documents\Visual Studio 2012\Projects\nk pharmacy system
2012\nk pharmacy system 2012\Database3.accdb"
Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadDataIntoListView()
End Sub
Private Sub LoadDataIntoListView()
' Configure the ListView
ListView1.Clear()
ListView1.View = View.Details
ListView1.Columns.Add("Name", 150, HorizontalAlignment.Left)
ListView1.Columns.Add("user id", 100, HorizontalAlignment.Right)
ListView1.Columns.Add("password", 100, HorizontalAlignment.Right)
ListView1.Columns.Add("role", 100, HorizontalAlignment.Right)
Try
Using connection As New OleDbConnection(connectionString)
connection.Open()
Dim query As String = "SELECT firstname,userid,pass,role FROM
table1"
Using command As New OleDbCommand(query, connection)
Using reader As OleDbDataReader = command.ExecuteReader()
While reader.Read()
Dim item As New
ListViewItem(reader("firstname").ToString())
item.SubItems.Add(reader("userid").ToString())
item.SubItems.Add(reader("pass").ToString())
item.SubItems.Add(reader("role").ToString())
ListView1.Items.Add(item)
End While
End Using
End Using
End Using
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
End Try
End Sub
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs)
Handles ListView1.SelectedIndexChanged
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
Dim firstname As String = TextBox1.Text
Dim userid As String = TextBox2.Text
Dim pass As String = TextBox3.Text
Dim role As String = TextBox4.Text
If String.IsNullOrEmpty(firstname) OrElse String.IsNullOrEmpty(userid)
OrElse String.IsNullOrEmpty(pass) OrElse String.IsNullOrEmpty(role) Then
MessageBox.Show("Please fill in all fields.")
Else
' Call the function to save data to the database
SaveDataToDatabase(firstname, userid, pass, role)
End If
End Sub
Private Sub SaveDataToDatabase(firstname As String, userid As String, pass As
String, role As String)
Dim query As String = "INSERT INTO table1 (firstname,userid,pass,mobile)
VALUES (@name,@id,@pass,@role)"
Using connection As New OleDbConnection(connectionString)
Using command As New OleDbCommand(query, connection)
command.Parameters.AddWithValue("@name", TextBox1.Text)
command.Parameters.AddWithValue("@id", TextBox2.Text)
command.Parameters.AddWithValue("@pass", TextBox3.Text)
command.Parameters.AddWithValue("@role", TextBox4.Text)
Try
connection.Open()
command.ExecuteNonQuery()
MessageBox.Show("Data saved successfully.")
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
Finally
connection.Close()
End Try
End Using
End Using
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs)
End Sub
Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles
Button2.Click
If ListView1.SelectedItems.Count = 0 Then
MessageBox.Show("Please select a row to edit.", "Information",
MessageBoxButtons.OK, MessageBoxIcon.Information)
Return
End If
Dim selectedItem As ListViewItem = ListView1.SelectedItems(0)
Dim data As New Dictionary(Of String, String) From {
{"firstname", selectedItem.SubItems(0).Text},
{"userid", selectedItem.SubItems(1).Text},
{"pass", selectedItem.SubItems(2).Text},
{"mobile", selectedItem.SubItems(3).Text}
}
Dim editForm As New Form11(Data)
editForm.ShowDialog()
' Refresh the ListView after editing
LoadDataIntoListView()
End Sub
End Class