0% found this document useful (0 votes)
78 views

Using Using Using Using Using Using Using Using Using Using Namespace Public Partial Class Public

This document contains code for a C# Windows Forms application that allows users to manage employee records stored in a SQL database. It includes code to load and display employee data, add/update/delete records, and validate user input. Functions are defined to handle form load, selection changes in the employee ID dropdown, clicking buttons to save, update, delete records, and more. Connections to the database are established using ADO.NET.
Copyright
© Attribution Non-Commercial (BY-NC)
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)
78 views

Using Using Using Using Using Using Using Using Using Using Namespace Public Partial Class Public

This document contains code for a C# Windows Forms application that allows users to manage employee records stored in a SQL database. It includes code to load and display employee data, add/update/delete records, and validate user input. Functions are defined to handle form load, selection changes in the employee ID dropdown, clicking buttons to save, update, delete records, and more. Connections to the database are established using ADO.NET.
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 7

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using formlogin;

namespace logn_form
{
public partial class employee : Form
{
public employee()
{
InitializeComponent();
}
SqlConnection con;
SqlDataAdapter da;
SqlCommand cmd;

private void employee_Load(object sender, EventArgs e)


{
try
{
txtaddress.Enabled = false;
txtdesignation.Enabled = false;
txtempid.Enabled = false;
txtname.Enabled = false;
txtphone.Enabled = false;
txtLocation.Enabled = false;
txtLocation.Enabled = false;
con = new
SqlConnection(Properties.Settings.Default.CALLCENTERConnectionString.ToString(
));
da = new SqlDataAdapter("select * from employee", con);
DataSet ds = new DataSet();
da.Fill(ds);
for (int x = 0; x < ds.Tables[0].Rows.Count; x++)
{
cmbid.Items.Add(ds.Tables[0].Rows[x][0].ToString());
}
}
catch (Exception )
{
MessageBox.Show("Please exit and again open the
application","Loadind Data Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}

private int validate()


{
int flag=0;
if (txtempid.Text.Length == 0)
{
txtempid.Focus();
errorProvider1.SetError(txtempid, "please check the employee
id");
flag = 1;
}
else
{
errorProvider1.Clear();
}
if (txtname.Text.Length == 0)
{
txtname.Focus();
errorProvider1.SetError(txtname, "please check the employee
name");
flag = 1;
}
if (txtaddress.Text.Length == 0)
{
txtaddress.Focus();
errorProvider1.SetError(txtaddress, "please check the
address");
flag = 1;
}
if (txtphone.Text.Length == 0)
{
txtphone.Focus();
errorProvider1.SetError(txtphone, "please check the phone
number");
flag = 1;
}
if (txtdesignation.Text.Length == 0)
{
txtdesignation.Focus();
errorProvider1.SetError(txtdesignation, "please check the
designation");
flag = 1;
}
if (txtLocation.Text.Length == 0)
{
txtLocation.Focus();
errorProvider1.SetError(txtLocation, "please check the
image");
flag = 1;
}
return flag;
}

private void cmbid_SelectedIndexChanged(object sender, EventArgs e)


{
try
{
errorProvider1.Clear();
txtaddress.Enabled = true;
txtdesignation.Enabled = true;
txtempid.Enabled = true;
txtname.Enabled = true;
txtphone.Enabled = true;
txtLocation.Enabled = false;
da = new SqlDataAdapter("Select * from employee where
employee_ID='" + cmbid.SelectedItem.ToString() + "'", con);
DataSet ds = new DataSet();
da.Fill(ds);
DataRow drow = ds.Tables[0].Rows[0];
txtempid.Text = drow[0].ToString();
txtname.Text = drow[1].ToString();
txtaddress.Text = drow[2].ToString();
txtphone.Text = drow[3].ToString();
txtdesignation.Text = drow[4].ToString();
try
{
byte[] b = (byte[])drow[5];
MemoryStream ms = new MemoryStream(b);
pictureBox1.Image = Image.FromStream(ms);
//ms.Flush();
}
catch (Exception )
{
// MessageBox.Show(ee.Message);
}
}
catch (Exception xp)
{
MessageBox.Show("Error:-" + xp.Message);
}
}

private void btnBrowse_Click(object sender, EventArgs e)


{
OpenFileDialog of = new OpenFileDialog();
if (of.ShowDialog() == DialogResult.OK)
{
txtLocation.Text = of.FileName;
pictureBox1.Image = Image.FromFile(of.FileName);
}
}

private void btnsave_Click(object sender, EventArgs e)


{
try
{
if (validate() != 0)
{
validate();
}
else
{
cmd = new SqlCommand("Select * from employee", con);
da = new SqlDataAdapter();
SqlCommandBuilder cb = new SqlCommandBuilder(da);
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
DataRow row = ds.Tables[0].NewRow();
row[0] = txtempid.Text;
row[1] = txtname.Text;
row[2] = txtaddress.Text;
row[3] = txtphone.Text;
row[4] = txtdesignation.Text;
Bitmap bmp = new Bitmap(txtLocation.Text);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] b = (byte[])ms.ToArray();
row[5] = b;
ds.Tables[0].Rows.Add(row);
da.Update(ds);
MessageBox.Show("Record insert successfully",
"Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
SqlDataAdapter da1 = new SqlDataAdapter("select * from
employee", con);
DataSet ds1 = new DataSet();
da1.Fill(ds1);
cmbid.Items.Clear();
for (int a = 0; a < ds1.Tables[0].Rows.Count; a++)
{
cmbid.Items.Add(ds1.Tables[0].Rows[a][0].ToString());
}
}

catch (Exception )
{
MessageBox.Show("This entry is already exist.please check the
details.", "Error occured during saving the entry.", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}

private void btnexit_Click(object sender, EventArgs e)


{
Form.ActiveForm.Close();
}

private void btnupdate_Click(object sender, EventArgs e)


{
try
{
con.Open();
cmd = new SqlCommand(" update employee set name='" +
txtname.Text + "' where employee_id='" + cmbid.Text + "'", con);
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
int v = cmd.ExecuteNonQuery();
if (v == 1)
{
if (txtname.Text.Length == 0)
{
if (MessageBox.Show("Record Not Update Successfully!",
"Error Occured During Updation", MessageBoxButtons.RetryCancel,
MessageBoxIcon.Error) == DialogResult.Retry)
{
txtname.Enabled = true;
txtname.Focus();
}
else
{
txtname.Enabled = false;
}
}
else
{
MessageBox.Show("Record Update Successfully!",
"Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
if (MessageBox.Show("Record Not Update Successfully!",
"Error Occured During Updation", MessageBoxButtons.RetryCancel,
MessageBoxIcon.Error) == DialogResult.Retry)
{
txtname.Enabled = true;
txtname.Focus();
}
}
}

catch (Exception xp)


{
if (MessageBox.Show("Please check the update entry",
"Information", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information) ==
DialogResult.Retry)
{
MessageBox.Show("error" + xp);
txtname.Enabled = true;
txtname.Focus();
}
else
{
txtname.Enabled = false;
button1.Focus();
}
}
con.Close();
}

private void btnshow_Click(object sender, EventArgs e)


{
showemployee frm = new showemployee();
frm.Show();
}

private void btnback_Click(object sender, EventArgs e)


{
welcome frm = new welcome();
frm.Show();
this.Hide();
}
private void btndelete_Click(object sender, EventArgs e)
{
DELETE_RECORDS frm = new DELETE_RECORDS();
frm.Show();
}

private void btnadd_Click(object sender, EventArgs e)


{
txtempid.Enabled = true;
txtname.Enabled = true;
txtaddress.Enabled = true;
txtphone.Enabled = true;
txtdesignation.Enabled = true;
txtLocation.Enabled = true;
errorProvider1.Clear();
cmbid.Text = "";
txtaddress.Text = "";
txtdesignation.Text = "";
txtempid.Text = "";
txtname.Text = "";
txtphone.Text = "";
txtLocation.Text = "";
pictureBox1.Image = null;

private void btnsalary_Click(object sender, EventArgs e)


{
salary frm = new salary();
frm.Show();
}
private void btnBrowse_MouseEnter(object sender, EventArgs e)
{

txtLocation.Enabled = true;
}

private void btnBrowse_MouseLeave(object sender, EventArgs e)


{
txtLocation.Enabled = false;

private void cmbid_Click(object sender, EventArgs e)


{
try
{
da = new SqlDataAdapter("select * from employee", con);
DataSet ds = new DataSet();
da.Fill(ds);
cmbid.Items.Clear();
for (int x = 0; x < ds.Tables[0].Rows.Count; x++)
{
cmbid.Items.Add(ds.Tables[0].Rows[x][0].ToString());
}
if (ds.Tables[0].Rows.Count == 0)
{
cmbid.Text = "";
txtaddress.Text = "";
txtdesignation.Text = "";
txtempid.Text = "";
txtname.Text = "";
txtphone.Text = "";
txtLocation.Text = "";
pictureBox1.Image = null;

}
}
catch
{
}
}

private void button1_Click(object sender, EventArgs e)


{
Form.ActiveForm.Close();
}

private void cmbid_TextChanged(object sender, EventArgs e)


{
pictureBox1.Image = null;
}
}
}

You might also like