Sunteți pe pagina 1din 7

Imports System Imports System.Data Imports System.Data.

SqlClient Public Class frmEmployee Dim connString As String = "Data Source=jspawn-pc\server2008;" & _ " Initial Catalog=JaySolutions;" & _ "Integrated Security = True;Mult ipleActiveResultSets=True;" Dim conn As New SqlConnection(connString) Dim Reader As SqlDataReader Dim navemployee As SqlCommand Dim Fname As String Dim sql As String Dim id As Integer ''////////////////////////////////////////////////////////////////////////// ///////////// '/////////////////////////////////////////////////////////////////////////// //////////// Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.Ev entArgs) End Sub Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, By Val e As System.EventArgs) Handles cboDept.SelectedIndexChanged End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.E ventArgs) Handles cmdSearch.Click Dim reader1 As SqlDataReader Fname = InputBox("Enter Employee First Name") If Fname = "" Then _ MessageBox.Show("Enter a valid First Name!") : Exit Sub sql = "Search_Employee" Dim comm As New SqlCommand comm = New SqlCommand(sql, conn) comm.CommandType = CommandType.StoredProcedure comm.Parameters.Add("@FNAME", System.Data.SqlDbType.NVarChar).Value = Fn ame.ToLower If conn.State = ConnectionState.Closed Then conn.Open() End If reader1 = comm.ExecuteReader() If reader1.Read Then Me.id = CInt(reader1("EMPLOYEEID")) Me.txtFnme.Text = reader1("FNAME") Me.txtLname.Text = reader1("LNAME") Me.txtAddress.Text = reader1("EADDRESS") Me.txtSalary.Text = reader1("SALARY") Me.txtSupersson.Text = reader1("SUPERSSN") Me.cboDept.Text = FindDept(CInt(reader1("DNO"))) Me.cboSex.Text = dispsex(reader1("SEX")) Me.txtEssn.Text = reader1("ESSN") Me.dtpBirthDate.Text = reader1("BDATE") Else MessageBox.Show("There is no user with the First Name: " & Fname) End If reader1.Close() comm = Nothing

conn.Close() End Sub Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.E ventArgs) Handles cmdSave.Click If (chkfields() <> "") Then _ MessageBox.Show(chkfields()) : Exit Sub If (empExists(txtEssn.Text)) Then _ MessageBox.Show("Employee Social Security No Already Exists") : Exit Sub sql = "Employee_Insert" Dim comm As New SqlCommand comm = New SqlCommand(sql, conn) comm.CommandType = CommandType.StoredProcedure comm.Parameters.Add("@FNAME", System.Data.SqlDbType.NVarChar).Value = tx tFnme.Text comm.Parameters.Add("@LNAME", System.Data.SqlDbType.NVarChar).Value = tx tLname.Text comm.Parameters.Add("@ESSN", System.Data.SqlDbType.Int).Value = txtEssn. Text comm.Parameters.Add("@BDATE", System.Data.SqlDbType.Date).Value = dtpBir thDate.Text comm.Parameters.Add("@ADDRESS", System.Data.SqlDbType.NVarChar).Value = txtAddress.Text comm.Parameters.Add("@SEX", System.Data.SqlDbType.NChar).Value = cboSex. Text comm.Parameters.Add("@SALARY", System.Data.SqlDbType.Money).Value = txtS alary.Text comm.Parameters.Add("@SUPERSSN", System.Data.SqlDbType.Int).Value = CInt (txtSupersson.Text) comm.Parameters.Add("@DNO", System.Data.SqlDbType.Int).Value = cboDept.S electedValue comm.Parameters.Add("@PHOTO", System.Data.SqlDbType.Text).Value = "photo _galore" Try conn.Open() comm.ExecuteNonQuery() For Each row As DataGridViewRow In DataGridView1.Rows If Not row.IsNewRow Then sql = "Add_Dependants" Dim adddepend As New SqlCommand adddepend = New SqlCommand(sql, conn) adddepend.CommandType = CommandType.StoredProcedure adddepend.Parameters.Add("@ESSN", System.Data.SqlDbType.Int) .Value = txtEssn.Text adddepend.Parameters.Add("@DNAME", System.Data.SqlDbType.NVa rChar).Value = row.Cells(0).Value.ToString adddepend.Parameters.Add("@SEX", System.Data.SqlDbType.NChar ).Value = row.Cells(1).Value.ToString adddepend.Parameters.Add("@BDATE", System.Data.SqlDbType.Dat e).Value = row.Cells(2).Value.ToString adddepend.Parameters.Add("@RELATIONSHIP", System.Data.SqlDbT ype.NVarChar).Value = row.Cells(1).Value.ToString adddepend.ExecuteNonQuery() End If Next MessageBox.Show("Employee Saved Successfully.") DataGridView1.Rows.Clear() EmptyTextBoxes() Catch ex As Exception

MessageBox.Show("Employee Save failed miserably!") Finally conn.Close() End Try End Sub Private Sub EmptyTextBoxes() txtFnme.Text = "" txtLname.Text = "" txtAddress.Text = "" txtSalary.Text = "" txtSupersson.Text = "" cboDept.Text = "" cboSex.Text = "" txtEssn.Text = "" dtpBirthDate.Text = Date.Now End Sub Private Sub FolderBrowserDialog1_HelpRequest_1(ByVal sender As System.Object , ByVal e As System.EventArgs) End Sub Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System .EventArgs) Handles GroupBox1.Enter End Sub Private Function chkfields() As String Dim msg As String = "" If (txtFnme.Text = "") Then msg &= "First Name Field is empty! " & vbCrLf End If If (txtLname.Text = "") Then msg &= "Last Name Field is empty!" & vbCrLf End If If (txtAddress.Text = "") Then msg &= "Address Field is empty!" & vbCrLf End If If (txtSalary.Text = "" Or containsNum(txtSalary.Text) <> True) Then msg &= "Salary Field Field is empty Or is not a vaild Figure!" & vbC rLf End If If (txtSupersson.Text = "" Or containsNum(txtSupersson.Text) <> True) Th en msg &= "Supervisor's Social Security No is empty or invalid!" & vbCr Lf End If If (dtpBirthDate.Value = Date.Now) Then msg &= "No worker was born today!" & vbCrLf End If If (txtEssn.Text = "" Or containsNum(txtEssn.Text) <> True) Then msg &= "The Employee Social Security No is empty or not a valid SS N umber!" & vbCrLf End If If txtEssn.Text.Length <> 9 Then msg &= "Invalid Employee Social Security!" & vbCrLf End If If txtSupersson.Text.Length <> 9 Then msg &= "Invalid Supervisor Social Security!" & vbCrLf End If Return msg End Function

Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System .EventArgs) Handles cmdUpdate.Click If (chkfields() <> "") Then _ MessageBox.Show(chkfields()) : Exit Sub sql = "Employee_Update" Dim comm As New SqlCommand comm = New SqlCommand(sql, conn) comm.CommandType = CommandType.StoredProcedure comm.Parameters.Add("@EMPID", System.Data.SqlDbType.Int).Value = id comm.Parameters.Add("@FNAME", System.Data.SqlDbType.NVarChar).Value = tx tFnme.Text comm.Parameters.Add("@LNAME", System.Data.SqlDbType.NVarChar).Value = tx tLname.Text comm.Parameters.Add("@ESSN", System.Data.SqlDbType.NVarChar).Value = txt Essn.Text comm.Parameters.Add("@BDATE", System.Data.SqlDbType.DateTime).Value = dt pBirthDate.Text comm.Parameters.Add("@ADDRESS", System.Data.SqlDbType.NVarChar).Value = txtAddress.Text comm.Parameters.Add("@SEX", System.Data.SqlDbType.NVarChar).Value = cboS ex.Text comm.Parameters.Add("@SALARY", System.Data.SqlDbType.NVarChar).Value = t xtSalary.Text comm.Parameters.Add("@SUPERSSN", System.Data.SqlDbType.NVarChar).Value = txtSupersson.Text comm.Parameters.Add("@DNO", System.Data.SqlDbType.NVarChar).Value = cboD ept.SelectedValue comm.Parameters.Add("@PHOTO", System.Data.SqlDbType.NVarChar).Value = "p hoto_location" Try conn.Open() comm.ExecuteNonQuery() MessageBox.Show("Employee Information Updated Successfully.") EmptyTextBoxes() Catch ex As Exception MessageBox.Show("Employee Information Update failed.") Finally conn.Close() End Try End Sub Function containsNum(ByVal num As String) As Boolean Dim res As Boolean If (IsNumeric(num)) Then res = True Else res = False End If Return res End Function Private Sub frmEmployee_Load(ByVal sender As System.Object, ByVal e As Syste m.EventArgs) Handles MyBase.Load 'TODO: This line of code loads data into the 'JaySolutionsDataSet4.Depen dant' table. You can move, or remove it, as needed. Me.DependantTableAdapter.Fill(Me.JaySolutionsDataSet4.Dependant) End Sub Private Sub cmdDel_Click(ByVal sender As System.Object, ByVal e As System.Ev entArgs) Handles cmdDel.Click If MsgBox("Are you sure you want to delete the record " & _ "for " & _

txtFnme.Text & " " & txtLname.Text & "?", _ MsgBoxStyle.Question Or MsgBoxStyle.YesNo, _ "Confirm") = MsgBoxResult.Yes Then If (chkfields() <> "") Then _ MessageBox.Show(chkfields()) : Exit Sub sql = "Employee_Delete" Dim comm As New SqlCommand comm = New SqlCommand(sql, conn) comm.CommandType = CommandType.StoredProcedure comm.Parameters.Add("@EMPID", System.Data.SqlDbType.Int).Value = id Try conn.Open() comm.ExecuteNonQuery() MessageBox.Show("Employee Information Deleted Successfully.") EmptyTextBoxes() Catch ex As Exception MessageBox.Show("Employee Information Delete failed.") Finally conn.Close() End Try End If End Sub Function empExists(ByVal ssn As String) As Boolean Dim res As Boolean sql = "Employee_SSN" Dim comm As New SqlCommand Dim reader1 As SqlDataReader comm = New SqlCommand(sql, conn) comm.CommandType = CommandType.StoredProcedure comm.Parameters.Add("@ESSN", System.Data.SqlDbType.Int).Value = txtEssn. Text If conn.State = ConnectionState.Closed Then conn.Open() End If reader1 = comm.ExecuteReader() If reader1.Read Then res = True Else res = False End If conn.Close() reader1.Close() comm = Nothing Return res End Function Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System. EventArgs) Handles btnFirst.Click sql = "SELECT TOP(1) * FROM Employee ORDER BY EMPLOYEEID" Dim comm As New SqlCommand Dim reader2 As SqlDataReader comm = New SqlCommand(sql, conn) comm.CommandType = CommandType.Text If conn.State = ConnectionState.Closed Then conn.Open() End If reader2 = comm.ExecuteReader() If reader2.Read Then Me.id = CInt(reader2("EMPLOYEEID")) Me.txtFnme.Text = reader2("FNAME") Me.txtLname.Text = reader2("LNAME")

Me.txtAddress.Text = reader2("EADDRESS") Me.txtSalary.Text = reader2("SALARY") Me.txtSupersson.Text = reader2("SUPERSSN") Me.cboDept.Text = FindDept(CInt(reader2("DNO"))) Me.cboSex.Text = dispsex(reader2("SEX")) Me.txtEssn.Text = reader2("ESSN") Me.dtpBirthDate.Text = reader2("BDATE") End If reader2.Close() comm = Nothing conn.Close() End Sub Function FindDept(ByVal dno As Integer) As String Dim reader1 As SqlDataReader Dim connfind As SqlCommand Dim Name As String = "" sql = "SELECT * FROM Department WHERE DNO = @DNO" connfind = New SqlCommand(sql, conn) connfind.CommandType = CommandType.Text connfind.Parameters.Add("@DNO", System.Data.SqlDbType.Int).Value = dno ' If conn.State = ConnectionState.Closed Then 'conn.Open() 'End If reader1 = connfind.ExecuteReader() If reader1.Read Then Name = reader1("DNAME") End If reader1.Close() connfind = Nothing ' conn.Close() Return Name End Function Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.E ventArgs) Handles btnLast.Click sql = "SELECT TOP(1) * FROM Employee ORDER BY EMPLOYEEID DESC" Dim comm As New SqlCommand Dim reader1 As SqlDataReader comm = New SqlCommand(sql, conn) comm.CommandType = CommandType.Text If conn.State = ConnectionState.Closed Then conn.Open() End If reader1 = comm.ExecuteReader() If reader1.Read Then Me.id = CInt(reader1("EMPLOYEEID")) Me.txtFnme.Text = reader1("FNAME") Me.txtLname.Text = reader1("LNAME") Me.txtAddress.Text = reader1("EADDRESS") Me.txtSalary.Text = reader1("SALARY") Me.txtSupersson.Text = reader1("SUPERSSN") Me.cboDept.Text = FindDept(CInt(reader1("DNO"))) Me.cboSex.Text = dispsex(reader1("SEX")) Me.txtEssn.Text = reader1("ESSN") Me.dtpBirthDate.Text = reader1("BDATE") End If reader1.Close() comm = Nothing conn.Close() End Sub Function dispsex(ByVal s As Char) As String

Dim sex As String = "" If s = "M" Then sex = "Male" ElseIf s = "F" Then sex = "Female" End If Return sex End Function Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.E ventArgs) Handles Button1.Click id = Nothing Me.Refresh() Me.Hide() Me.Show() End Sub Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As Syst em.EventArgs) Handles btnPrevious.Click Dim sql1 As String = "SELECT * FROM Employee" navemployee = New SqlCommand(sql1, conn) navemployee.CommandType = CommandType.Text If conn.State = ConnectionState.Closed Then conn.Open() End If Dim reader1 As SqlDataReader navemployee.ExecuteReader() End Sub Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, By Val e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.C ellContentClick End Sub Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.E ventArgs) Handles btnNext.Click End Sub End Class

S-ar putea să vă placă și