private void usersInfoToolStripMenuItem_Click(object sender, EventArgs e) { frmUsers frmUsr = new frmUsers(); frmUsr.Show(); }
private void btnSave_Click(object sender, EventArgs e) { if (txtName.Text == "") { MessageBox.Show("Please enter employee name.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); txtName.Focus(); return; } if (txtSurname.Text == "") { MessageBox.Show("Please enter employee surname", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); txtSurname.Focus(); return; } if (txtPosition.Text == "") { MessageBox.Show("Please enter employee\'s position.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); txtPosition.Focus(); return; } if (dateOfbirth.Value.Year > System.DateTime.Now.Year || dateOfbirth.Value.Year == System.DateTime.Now.Year) { MessageBox.Show("Please enter correct birth year", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (txtAddress.Text == "") { MessageBox.Show("Please enter address.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); txtAddress.Focus(); return; } if (txtContactno.Text == "") { MessageBox.Show("Please enter Contact no.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); txtContactno.Focus(); return; } if (txtEmailAdd.Text == "") { MessageBox.Show("Please enter email.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); txtEmailAdd.Focus(); return; } try { frmUsers userFrm = new frmUsers(); txtEmpId.Text = "EMP-" + GetUniqueCode(5); //first Check if EMP_ID is existing connectionStr = new SqlConnection(conStr.connectString); connectionStr.Open(); string selectQry = "select emp_id from tblemployee where emp_id=@find"; cmd = new SqlCommand(selectQry, connectionStr); cmd.Parameters.Add(new SqlParameter("@find", SqlDbType.VarChar)).Value = txtEmpId.Text; rdr = cmd.ExecuteReader(); if (rdr.Read()) { MessageBox.Show("Employee ID already exist.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); txtEmpId.Text = ""; txtName.Focus(); if ((rdr != null)) { rdr.Close(); } return; } connectionStr = new SqlConnection(conStr.connectString); connectionStr.Open(); string insertQry = "insert into tblemployee values(@emp_id,@emp_name,@emp_sur,@emp_dob,@cont_no,@addr,@posi,@email)"; cmd = new SqlCommand(insertQry, connectionStr); cmd.Parameters.Add(new SqlParameter("@emp_id", SqlDbType.VarChar)).Value = txtEmpId.Text; cmd.Parameters.Add(new SqlParameter("@emp_name", SqlDbType.VarChar)).Value = txtName.Text; cmd.Parameters.Add(new SqlParameter("@emp_sur", SqlDbType.VarChar)).Value = txtSurname.Text; cmd.Parameters.Add(new SqlParameter("@emp_dob", SqlDbType.Date)).Value = dateOfbirth.Value; cmd.Parameters.Add(new SqlParameter("@cont_no", SqlDbType.NChar)).Value = txtContactno.Text; cmd.Parameters.Add(new SqlParameter("@addr", SqlDbType.VarChar)).Value = txtAddress.Text; cmd.Parameters.Add(new SqlParameter("@posi", SqlDbType.VarChar)).Value = txtPosition.Text; cmd.Parameters.Add(new SqlParameter("@email", SqlDbType.VarChar)).Value = txtEmailAdd.Text; cmd.ExecuteNonQuery(); MessageBox.Show("Employee info saved successful...!\n" + txtEmpId.Text, "New Record", MessageBoxButtons.OK, MessageBoxIcon.Information); Autocomplete(); userFrm.txtRole.Text = this.txtPosition.Text; userFrm.txtEmp.Text = this.txtEmpId.Text; this.Close(); userFrm.Show(); userFrm.txtRole.Enabled = false; reset(); connectionStr.Close(); cmd.Dispose(); } catch (SqlException ex) { MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }