0% found this document useful (0 votes)
41 views5 pages

Code For Lesson 6 Pharmacy

The document contains code for loading, saving, updating, deleting and displaying user data from a database table. It includes: 1. Functions for loading all users from the database table into a datagridview, getting the maximum user ID, and clearing controls to accept new data. 2. Procedures for saving new users to the database, updating and deleting existing user records, and displaying selected user data in controls for editing or deleting. 3. Code checks if the user record already exists before saving or updating, and displays messages confirming actions. It loads the table after saves and deletions to refresh the view.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views5 pages

Code For Lesson 6 Pharmacy

The document contains code for loading, saving, updating, deleting and displaying user data from a database table. It includes: 1. Functions for loading all users from the database table into a datagridview, getting the maximum user ID, and clearing controls to accept new data. 2. Procedures for saving new users to the database, updating and deleting existing user records, and displaying selected user data in controls for editing or deleting. 3. Code checks if the user record already exists before saving or updating, and displays messages confirming actions. It loads the table after saves and deletions to refresh the view.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

' ‫كود التحميل الخاص بالجدول‬

Public Sub LoadAllTable_Users_InDirect(ByVal dgv As DataGridView)


Dim dt As New DataTable
Dim da As New SqlDataAdapter
dt.Clear()
da = New SqlDataAdapter("select * from Users", con)
da.Fill(dt)
dgv.AutoGenerateColumns = False
dgv.DataSource = dt
End Sub

Public Sub LoadAllTable_Users_InDirect()


Dim dt As New DataTable
Dim da As New SqlDataAdapter
dt.Clear()
da = New SqlDataAdapter("select * from Users", con)
da.Fill(dt)
End Sub
' ‫كود ايجاد اكبر مسلسل داخل الجدول‬
Public Function GetMaxUserID_Users_InDirect()
Dim dt As New DataTable
Dim da As New SqlDataAdapter
dt.Clear()
da = New SqlDataAdapter("select MAX(UserID) from Users",con)
da.Fill(dt)

Dim MyNewUserID_Users As Double

If IsDBNull(dt(0)(0)) = True Then


MyNewUserID_Users = 1
Else
MyNewUserID_Users = dt(0)(0) + 1
End If

Return MyNewUserID_Users

End Function

' ‫كود تفريغ االدوات الستقبال بيانات جديدة‬


Public Sub ClearControls_Users()

UserID.Text = GetMaxUserID_Users_InDirect()
UserFullName.Text = Nothing
UserPhone.Text = Nothing
UserMobile.Text = Nothing
UserAddress.Text = Nothing
UserGender.Text = Nothing
UserJob.Text = Nothing
UserForProgramm.Checked = False
UserName.Text = Nothing
UserPassword.Text = Nothing
UserDOB.Value = Now.Date
UserCardID.Text = Nothing
UserDate.Value = Now.Date
UserTime.Value = Now
UserCurrentUser.Text = 0

End Sub
' ‫كود الحفظ‬
Public Sub Save_Users_InDirect(ByVal colName As String, ByVal colNametxt As
String)
' check if this ColumnName is already exist in database
Dim dt As New DataTable
Dim da As New SqlDataAdapter
dt.Clear()
da = New SqlDataAdapter("Select * FROM Users where " & colName & " =
'" & colNametxt & "'", con)
da.Fill(dt)
' if Column present give msgbox to alert & Dont save
If dt.Rows.Count > 0 Then
MsgBox("‫" هذا االسم موجود من قبل‬, MsgBoxStyle.Critical, "‫)"بيانات مكررة‬
Else
dt.Rows.Add()
Dim last As Integer = dt.Rows.Count - 1

dt.Rows(last).Item("UserID") = UserID.Text
dt.Rows(last).Item("UserFullName") = UserFullName.Text
dt.Rows(last).Item("UserPhone") = UserPhone.Text
dt.Rows(last).Item("UserMobile") = UserMobile.Text
dt.Rows(last).Item("UserAddress") = UserAddress.Text
dt.Rows(last).Item("UserGender") = UserGender.Text
dt.Rows(last).Item("UserJob") = UserJob.Text
dt.Rows(last).Item("UserForProgramm") = UserForProgramm.Checked
dt.Rows(last).Item("UserName") = UserName.Text
dt.Rows(last).Item("UserPassword") = UserPassword.Text
dt.Rows(last).Item("UserDOB") = UserDOB.Value
dt.Rows(last).Item("UserCardID") = UserCardID.Text
dt.Rows(last).Item("UserDate") = UserDate.Value
dt.Rows(last).Item("UserTime") = UserTime.Value
dt.Rows(last).Item("UserCurrentUser") = UserCurrentUser.Text

Dim Save As New SqlCommandBuilder(da)


da.Update(dt)
dt.AcceptChanges()

'give msgbox to sure save


MsgBox("‫" تم الحفظ بنجاح‬, MsgBoxStyle.Information, "‫)"تاكيد الحفظ‬
' load table to refresh data
LoadAllTable_Users_InDirect(dgv)
End If
End Sub
' ‫كود التعديل‬
Public Sub Update_Users_InDirect(ByVal colName As String, ByVal colNametxt As
String)
' check if this ColumnName is already exist in database
Dim DT As New DataTable
Dim da As New SqlDataAdapter
DT.Clear()
DA = New SqlDataAdapter("Select * FROM Users where " & colName & " = " &
colNametxt & "", con)
DA.Fill(DT)
' if Column present give msgbox to alert & Dont save
If DT.Rows.Count = 0 Then
MsgBox("‫" هذا االسم غير موجود في قاعدة البيانات‬, MsgBoxStyle.Critical, " ‫بيانات غير‬
‫)"مسجلة‬
Else
Dim pos As Integer = BindingContext(DT).Position

'DT.Rows(pos).Item("UserID") = UserID.Text
DT.Rows(pos).Item("UserFullName") = UserFullName.Text
DT.Rows(pos).Item("UserPhone") = UserPhone.Text
DT.Rows(pos).Item("UserMobile") = UserMobile.Text
DT.Rows(pos).Item("UserAddress") = UserAddress.Text
DT.Rows(pos).Item("UserGender") = UserGender.Text
DT.Rows(pos).Item("UserJob") = UserJob.Text
DT.Rows(pos).Item("UserForProgramm") = UserForProgramm.Checked
DT.Rows(pos).Item("UserName") = UserName.Text
DT.Rows(pos).Item("UserPassword") = UserPassword.Text
DT.Rows(pos).Item("UserDOB") = UserDOB.Value
DT.Rows(pos).Item("UserCardID") = UserCardID.Text
DT.Rows(pos).Item("UserDate") = UserDate.Value
DT.Rows(pos).Item("UserTime") = UserTime.Value
DT.Rows(pos).Item("UserCurrentUser") = UserCurrentUser.Text

Dim Save As New SqlCommandBuilder(DA)


DA.Update(DT)
DT.AcceptChanges()
'give msgbox to sure save
MsgBox("‫" تم التعديل) بنجاح‬, MsgBoxStyle.Information, ")‫)"تاكيد التعديل‬
' load table to refresh data
LoadAllTable_Users_Indirect(dgv)
End If
End Sub

' ‫كود الحذف‬


Public Sub Delete_Users_InDirect(ByVal DeletecolID As String, ByVal
DeletecolIDtxt As String)
' check if this ColumnName is already exist in database
Dim DT As New DataTable
Dim da As New SqlDataAdapter
DT.Clear()
DA = New SqlDataAdapter("Select * FROM Users where " & DeletecolID & " =
" & DeletecolIDtxt & "", con)
DA.Fill(DT)
'if row present Delete the selected row
If DT.Rows.Count > 0 Then
Dim pos As Integer = BindingContext(DT).Position
DT.Rows(pos).Delete()

Dim Save As New SqlCommandBuilder(DA)


DA.Update(DT)
DT.AcceptChanges()

'give msgbox to sure Delete


MsgBox("‫" تم الحذف بنجاح‬, MsgBoxStyle.Information, "‫)"تاكيد الحذف‬

' load table to refresh data


LoadAllTable_Users_Indirect(dgv)
Else
MsgBox("‫"ال توجد بيانات لحذفها‬, MsgBoxStyle.Critical, "‫)"بيانات مفقودة‬
End If
End Sub

' ‫كود عرض البيانات علي االدوات للتعديل او الحذف‬


Public Sub ShowRowsFromDGV_Users_InDirect(ByVal dgv As DataGridView, ByVal
Colindx As Integer)
Try
' check if this Row in dgv is already exist in database
Dim DT As New DataTable
Dim da As New SqlDataAdapter
DT.Clear()
DA = New SqlDataAdapter("Select * FROM Users where UserID = " &
dgv(Colindx, dgv.CurrentRow.Index).Value & "", con)
DA.Fill(DT)
' if Row present
If DT.Rows.Count > 0 Then
' show selected row data in table form
UserID.Text = DT.Rows(0).Item("UserID").ToString()
UserFullName.Text = DT.Rows(0).Item("UserFullName").ToString()
UserPhone.Text = DT.Rows(0).Item("UserPhone").ToString()
UserMobile.Text = DT.Rows(0).Item("UserMobile").ToString()
UserAddress.Text = DT.Rows(0).Item("UserAddress").ToString()
UserGender.Text = DT.Rows(0).Item("UserGender").ToString()
UserJob.Text = DT.Rows(0).Item("UserJob").ToString()
UserForProgramm.Checked =
DT.Rows(0).Item("UserForProgramm").ToString()
UserName.Text = DT.Rows(0).Item("UserName").ToString()
UserPassword.Text = DT.Rows(0).Item("UserPassword").ToString()
UserDOB.Value = DT.Rows(0).Item("UserDOB").ToString()
UserCardID.Text = DT.Rows(0).Item("UserCardID").ToString()
UserDate.Value = DT.Rows(0).Item("UserDate").ToString()
UserTime.Value = DT.Rows(0).Item("UserTime").ToString()
UserCurrentUser.Text = DT.Rows(0).Item("UserCurrentUser")
Else
' if Row is not present
MsgBox("‫" هذا السجل غير موجود‬, MsgBoxStyle.Critical, "‫)"بيانات مفقودة‬
End If
Catch ex As Exception
End Try
End Sub

You might also like