Vous êtes sur la page 1sur 5

Imports System.Data.

SqlClient
Public Class frmEmployees
Sub division()
Try
con = New SqlConnection(cs)
con.Open()
adp = New SqlDataAdapter()
adp.SelectCommand = New SqlCommand("SELECT distinct RTRIM(division)
FROM signatories", con)
ds = New DataSet("ds")
adp.Fill(ds)
dtable = ds.Tables(0)
cmbDivision.Items.Clear()
For Each drow As DataRow In dtable.Rows
cmbDivision.Items.Add(drow(0).ToString())
Next
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Try
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
Sub Reset()
txtDivision.Text = ""
cmbDivision.Text = ""
txtContactPerson.Text = ""
txtContactNo.Text = ""
txtSearchByContactPerson.Text = ""
txtContactPerson.Focus()
Getdata()
btnSave.Enabled = True
btnDelete.Enabled = False
btnUpdate.Enabled = False
txtContactPerson.Focus()
End Sub
Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnNew.Click
Reset()
End Sub

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


System.EventArgs) Handles btnSave.Click
If txtContactPerson.Text = "" Then
MessageBox.Show("Please enter Employee Name.", "",
MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtContactPerson.Focus()
Return
End If
If txtContactNo.Text = "" Then
MessageBox.Show("Please enter Position.", "", MessageBoxButtons.OK,
MessageBoxIcon.Warning)
txtContactNo.Focus()
Return
End If
If txtDivision.Text = "" Then
MessageBox.Show("Please enter Employee Division.", "",
MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtContactNo.Focus()
Return
End If
Try
con = New SqlConnection(cs)
con.Open()
Dim ct As String = "select name from signatories where name=@d1 "
cmd = New SqlCommand(ct)
cmd.Parameters.AddWithValue("@d1", txtContactPerson.Text)
cmd.Connection = con
rdr = cmd.ExecuteReader()

If rdr.Read() Then
MessageBox.Show("Record Already Existed!", "Error",
MessageBoxButtons.OK, MessageBoxIcon.[Error])
If (rdr IsNot Nothing) Then
rdr.Close()
End If
Return
End If

con = New SqlConnection(cs)


con.Open()

Dim cb As String = "insert into signatories(name,position,division)


VALUES (@d1,@d2,@d3)"
cmd = New SqlCommand(cb)
cmd.Parameters.AddWithValue("@d1", txtContactPerson.Text)
cmd.Parameters.AddWithValue("@d2", txtContactNo.Text)
cmd.Parameters.AddWithValue("@d3", txtDivision.Text)
cmd.Connection = con
cmd.ExecuteReader()
con.Close()
LogFunc(lblUser.Text, "added the new signatory having Employee Name '"
& txtContactPerson.Text & "' and Position '" & txtContactNo.Text & "'")
MessageBox.Show("Successfully Saved", "Record", MessageBoxButtons.OK,
MessageBoxIcon.Information)
btnSave.Enabled = False
Getdata()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.[Error])
End Try
End Sub

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


System.EventArgs) Handles btnUpdate.Click
If txtContactPerson.Text = "" Then
MessageBox.Show("Please enter Employee Name.", "",
MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtContactPerson.Focus()
Return
End If
If txtContactNo.Text = "" Then
MessageBox.Show("Please enter Position.", "", MessageBoxButtons.OK,
MessageBoxIcon.Warning)
txtContactNo.Focus()
Return
End If
Try
con = New SqlConnection(cs)
con.Open()
Dim cb As String = "Update Signatories set name=@d1, position=@d2,
division=@d3 where ID=" & txtID.Text & ""
cmd = New SqlCommand(cb)
cmd.Parameters.AddWithValue("@d1", txtContactPerson.Text)
cmd.Parameters.AddWithValue("@d2", txtContactNo.Text)
cmd.Parameters.AddWithValue("@d3", txtDivision.Text)
cmd.Connection = con
cmd.ExecuteReader()
con.Close()
LogFunc(lblUser.Text, "updated the Signaotry having Employee Name '" &
txtContactPerson.Text & "' and Position '" & txtContactNo.Text & "'")
MessageBox.Show("Successfully updated", "Record", MessageBoxButtons.OK,
MessageBoxIcon.Information)
btnUpdate.Enabled = False
Getdata()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.[Error])
End Try
End Sub
Private Sub DeleteRecord()

Try
Dim RowsAffected As Integer = 0
con = New SqlConnection(cs)
con.Open()
Dim cq As String = "delete from signatories where ID=@d1"
cmd = New SqlCommand(cq)
cmd.Parameters.AddWithValue("@d1", Val(txtID.Text))
cmd.Connection = con
RowsAffected = cmd.ExecuteNonQuery()
If RowsAffected > 0 Then
LogFunc(lblUser.Text, "deleted the signatory having Employee Name
'" & txtContactPerson.Text & "' and Position '" & txtContactNo.Text & "'")
MessageBox.Show("Successfully deleted", "Record",
MessageBoxButtons.OK, MessageBoxIcon.Information)
Getdata()
Reset()
Else
MessageBox.Show("No Record found", "Sorry", MessageBoxButtons.OK,
MessageBoxIcon.Information)
Reset()
End If
If con.State = ConnectionState.Open Then
con.Close()

End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.[Error])
End Try
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnDelete.Click
Try
If MessageBox.Show("Do you really want to delete this record?",
"Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) =
Windows.Forms.DialogResult.Yes Then
DeleteRecord()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Try
End Sub

Private Sub dgw_RowPostPaint(ByVal sender As Object, ByVal e As


System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles dgw.RowPostPaint
Dim strRowNumber As String = (e.RowIndex + 1).ToString()
Dim size As SizeF = e.Graphics.MeasureString(strRowNumber, Me.Font)
If dgw.RowHeadersWidth < Convert.ToInt32((size.Width + 20)) Then
dgw.RowHeadersWidth = Convert.ToInt32((size.Width + 20))
End If
Dim b As Brush = SystemBrushes.ControlText
e.Graphics.DrawString(strRowNumber, Me.Font, b, e.RowBounds.Location.X +
15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2))
End Sub
Public Sub Getdata()
Try
con = New SqlConnection(cs)
con.Open()
cmd = New SqlCommand("SELECT
(ID),RTRIM(name),RTRIM(position),RTRIM(Division) from signatories order by name",
con)
rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
dgw.Rows.Clear()
While (rdr.Read() = True)
dgw.Rows.Add(rdr(0), rdr(1), rdr(2), rdr(3))
End While
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Try
End Sub

Private Sub frmcategory_Load(sender As System.Object, e As System.EventArgs)


Getdata()
End Sub

Private Sub txtSearchByContactPerson_TextChanged(sender As Object, e As


EventArgs) Handles txtSearchByContactPerson.TextChanged
Try
con = New SqlConnection(cs)
con.Open()
cmd = New SqlCommand("SELECT (ID),RTRIM(name),RTRIM(position),
RTRIM(Division) from signatories where name like '%" &
txtSearchByContactPerson.Text & "%' order by name", con)
rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
dgw.Rows.Clear()
While (rdr.Read() = True)
dgw.Rows.Add(rdr(0), rdr(1), rdr(2), rdr(3))
End While
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Try
End Sub

Private Sub dgw_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles


dgw.MouseDoubleClick
Try
If dgw.Rows.Count > 0 Then
Dim dr As DataGridViewRow = dgw.SelectedRows(0)
txtID.Text = dr.Cells(0).Value.ToString()
txtContactPerson.Text = dr.Cells(1).Value.ToString()
txtContactNo.Text = dr.Cells(2).Value.ToString()
txtDivision.Text = dr.Cells(3).Value.ToString()
btnUpdate.Enabled = True
btnDelete.Enabled = True
btnSave.Enabled = False
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Try
End Sub

Private Sub cmbDivision_SelectedIndexChanged(sender As Object, e As EventArgs)


Handles cmbDivision.SelectedIndexChanged
Try
con = New SqlConnection(cs)
con.Open()
cmd = New SqlCommand("Select id,name,position,division from signatories
where division='" & cmbDivision.Text & "' order by name", con)
rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
dgw.Rows.Clear()
While (rdr.Read() = True)
dgw.Rows.Add(rdr(0), rdr(1), rdr(2), rdr(3))
End While
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Try
End Sub

Private Sub frmEmployees_Load(sender As Object, e As EventArgs) Handles


MyBase.Load
division()
Getdata()
End Sub
End Class

Vous aimerez peut-être aussi