Você está na página 1de 4

Imports System.

Data
Imports System.Data.SqlClient
Imports System.IO

Public Class frmAddUsers


Dim conn As New SqlConnection
Dim imgfilepath As String
Dim UserExist As Boolean
'===========================ADD USERS========================
Private Sub Add()
If frmUsers.xmode = "ADD" Then
conn = New SqlConnection("Server=CJAY-PC\SQLEXPRESS; Database=MMA; T
rusted_Connection=True;")
conn.Open()
Dim cmd As SqlCommand = conn.CreateCommand 'create a command that as
sociates with sql conn
cmd.CommandText = String.Format("INSERT INTO tblUsers (FName, MName,
LName, Position, UserName, Passwords, Photo)" & "VALUES ('{0}', '{1}', '{2}', '
{3}', '{4}', '{5}', '{6}')", txtFName.Text,
txtMName.Text,
txtLName.Text,
txtPosition.Text,
txtUsername.Text,
txtPassword.Text,
lblFilepath.Text) 'get or set sql st
atement procedute to execute at the datasource

Dim rowsAffected As Integer = cmd.ExecuteNonQuery 'execute sql stat


ement againts the connection and return the number of rows affected
If rowsAffected > 0 Then
MsgBox("sucessfully added users!")
txtFName.Clear()
txtLName.Clear()
txtMName.Clear()
txtPosition.Clear()
txtUsername.Clear()
txtPassword.Clear()
txtPosition.Clear()
lblFilepath.Text = ""
'==============REFRESH DATADRID VIEW===================
Me.Close()
frmUsers.Show()
frmUsers.Loadusers()
Else
MsgBox("Failed to add users!")

End If
conn.Close()
End If

End Sub

Public Sub CheckUsername()


Dim conn As New SqlConnection("Server=CJAY-PC\SQLEXPRESS; Database=MMA;
Trusted_Connection=True; ")
conn.Open()
Dim cmd As SqlCommand = conn.CreateCommand
cmd.CommandText = String.Format("SELECT * FROM tblUsers WHERE Username =
'{0}'", txtUsername.Text)
Dim dr As SqlDataReader = cmd.ExecuteReader()
If dr.HasRows Then
UserExist = True

End If
dr.Close()
conn.Close()

End Sub

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.Ev


entArgs) Handles btnAdd.Click
CheckUsername()
If Len(txtFName.Text) = 0 Or Len(txtMName.Text) = 0 Or Len(txtLName.Text

) = 0 Or Len(txtUsername.Text) = 0 Or Len(txtPosition.Text) = 0 Or Len(txtPasswo


rd.Text) = 0 Then
MsgBox("Please fill all fields.")
ElseIf txtPassword.Text <> txtPassword2.Text Then
MsgBox("Password mismatch.")
ElseIf UserExist = True Then
MsgBox("Username already exist!")
txtUsername.Clear()
txtUsername.Focus()
Else
Add()
End If

End Sub
'---back button
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.E
ventArgs) Handles btnExit.Click
Me.Close()
frmUsers.Show()
End Sub

Private Sub frmAddUser_Load(ByVal sender As System.Object, ByVal e As System


.EventArgs) Handles MyBase.Load
If frmUsers.xMode = "ADD" Then
Me.Text = "ADD USER"
'MsgBox("Add mode")
btnAdd.Text = "&Add User"
ElseIf frmUsers.xMode = "EDIT" Then
'''''''''''set edit names''''''''''
Me.Text = "UPDATE USER INFORMATION"
btnAdd.Text = "&Update User"
If frmUsers.dgvUsers.SelectedRows.Count > 0 Then
Dim selectedRow = frmUsers.dgvUsers.SelectedRows(0)
frmUsers.selectedUserID = selectedRow.Cells(0).Value
txtFName.Text = selectedRow.Cells(1).Value
txtMName.Text = selectedRow.Cells(2).Value
txtLName.Text = selectedRow.Cells(3).Value
txtPosition.Text = selectedRow.Cells(4).Value
txtUsername.Enabled = False
txtUsername.Text = selectedRow.Cells(5).Value
txtPassword.Text = selectedRow.Cells(6).Value
lblFilepath.Text = selectedRow.Cells(7).Value
End If
Dim filePath As String
filePath = lblFilepath.Text

If filePath = "" Then


MsgBox("No image can be loaded. Please select image for this use
r.")
btnBrowse_Click(Nothing, Nothing)
Else
Dim filename As String = Path.GetFileName(filePath)
Dim fs As FileStream = New FileStream(filePath, FileMode.Open, F
ileAccess.Read)
Dim br As BinaryReader = New BinaryReader(fs)
Dim bytes As Byte() = br.ReadBytes(Convert.ToInt32(fs.Length))
br.Close()
fs.Close()
picUser.ImageLocation = filePath
picUser.BackgroundImage = Nothing
lblFilepath.Text = filePath
End If
End If
End Sub
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System
.EventArgs) Handles btnBrowse.Click
End Sub
End Class

Você também pode gostar