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

Namespace Registration

The document discusses connecting to a database and performing CRUD operations on employee records. It defines connection strings and methods for filling list views, inserting, updating, and deleting records from a database table.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Namespace Registration

The document discusses connecting to a database and performing CRUD operations on employee records. It defines connection strings and methods for filling list views, inserting, updating, and deleting records from a database table.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

"Data sorce =.

;initial catalog=northwind;user id =sa; password=css"

Data
Source=myServerAddress;InitialCatalog=myDataBase;UserId=myUsername;Pa
ssword=myPassword;

namespace Registration
{
public partial class Form1 : Form
{

#region "declaration"
/*Declaring Connection variables
Here i had difined the connection variables so i can use
them anywhere*/
public string constring = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=" + Application.StartupPath + "/Records.mdb" +
";Persist Security Info=False";
Boolean states = false;
#endregion

public Form1()
{
InitializeComponent();
}

#region "filldatagrid"
//to fill data in Listview
public void fill(string str, string tstr)
{
listView1.Items.Clear();
listView1.Sorting = SortOrder.Ascending;
try
{
OleDbConnection con = new OleDbConnection(constring);
OleDbCommand com1 = new OleDbCommand(str, con);
con.Open();
OleDbDataReader datareader;
datareader = com1.ExecuteReader();
while (datareader.Read())
{
ListViewItem lvitem = new
ListViewItem(datareader[0].ToString());
for (int i = 1; i <= datareader.FieldCount - 1;
i++)
{

lvitem.SubItems.Add(datareader[i].ToString());

listView1.Items.Add(lvitem);
}
con.Close();
}
catch (Exception err) {
MessageBox.Show(err.ToString()); }
}
////////////////
private void filltext(string str, string tstr)
{
OleDbConnection con = new OleDbConnection(constring);
OleDbCommand com1 = new OleDbCommand(str, con);
con.Open();
OleDbDataReader datareader;
datareader = com1.ExecuteReader();
datareader.Read();
{
txtcode.Text =
datareader["Employee_Code"].ToString();
txtname.Text =
datareader["Employee_Name"].ToString();
txtaddress.Text =
datareader["Employee_Address"].ToString();
txtphone.Text =
datareader["Telephone_Number"].ToString();
txtsal.Text =
datareader["Monthly_Salary"].ToString();
txtjoin.Text = datareader["Joining_Date"].ToString();

}
datareader.Close();
con.Close();
}
//////////////////////
private void unable(bool a)
{
txtcode.Enabled = a;
txtname.Enabled = a;
txtaddress.Enabled = a;
txtphone.Enabled = a;
txtsal.Enabled = a;
txtjoin.Enabled = a;

#endregion

#region "insert save delete record"


private void saverecord()
{
OleDbConnection savecon = new OleDbConnection(constring);
OleDbCommand savecom = new OleDbCommand("insert into
Employee_Details values (?,?,?,?,?,?)", savecon);
OleDbParameter param;
param = savecom.Parameters.Add("@empcode",
OleDbType.VarChar, 10);
param.Value = txtcode.Text;
param = savecom.Parameters.Add("@empname",
OleDbType.VarChar, 25);
param.Value = txtname.Text;
param = savecom.Parameters.Add("@empadd",
OleDbType.VarChar, 50);
param.Value = txtaddress.Text;
param = savecom.Parameters.Add("@empphone",
OleDbType.VarChar, 20);
param.Value = txtphone.Text;
param = savecom.Parameters.Add("@empsal",
OleDbType.VarChar, 7);
param.Value = txtsal.Text;
param = savecom.Parameters.Add("@empjoin",
OleDbType.Date);
param.Value = DateTime.Now.ToShortDateString();
savecon.Open();
int rows = savecom.ExecuteNonQuery();
MessageBox.Show(rows.ToString() + "rows affected");
btnref.PerformClick();
savecon.Close();
}
private void updaterecord()
{
OleDbConnection upcon = new OleDbConnection(constring);
OleDbCommand upcom = new OleDbCommand("Update
Employee_Details set Employee_Name=?, Employee_Address=?,
Telephone_Number=?, Monthly_Salary=?, Joining_Date=?" + "where
Employee_Code=?",upcon);
OleDbParameter param;
param = upcom.Parameters.Add("@empname",
OleDbType.VarChar, 25);
param.Value = txtname.Text;
param = upcom.Parameters.Add("@empadd",
OleDbType.VarChar, 50);
param.Value = txtaddress.Text;
param = upcom.Parameters.Add("@empphone",
OleDbType.VarChar, 20);
param.Value = txtphone.Text;
param = upcom.Parameters.Add("@empsal",
OleDbType.VarChar, 7);
param.Value = txtsal.Text;
param = upcom.Parameters.Add("@empjoin", OleDbType.Date);
param.Value = DateTime.Now.ToShortDateString();
param = upcom.Parameters.Add("@empcode",
OleDbType.VarChar, 10);
param.Value = txtcode.Text;
upcon.Open();
int rowss = upcom.ExecuteNonQuery();
MessageBox.Show(rowss.ToString() + "rows effected");
btnref.PerformClick();
upcon.Close();
}
private void deleterecord()
{
OleDbConnection delcon = new OleDbConnection(constring);
OleDbCommand delcom = new OleDbCommand("Delete from
Employee_Details where Employee_Code='" + txtcode.Text + "'",delcon);
delcon.Open();
int rows = delcom.ExecuteNonQuery();
MessageBox.Show(rows.ToString() + " Rows Deleted");
btnref.PerformClick();
delcon.Close();
}
#endregion
private void toolStripButton14_Click(object sender, EventArgs
e)
{//close the form
this.Close();
}

private void toolStripButton6_Click(object sender, EventArgs


e)
{//Reload the data
unable(false);
try
{
string str = "Select * from Employee_Details order by
Employee_Code";
string tstr = "Employee_Details";
fill(str, tstr);
filltext(str, tstr);
comboBox1.SelectedIndex = 0;
}
catch (Exception er) { MessageBox.Show(er.ToString()); }
}

private void Form1_Load(object sender, EventArgs e)


{
//it will fill the listview with data on load event
//Calling the connect function which we had difined
earlier

unable(false);
try
{
string str = "Select * from Employee_Details order by
Employee_Code";
string tstr = "Employee_Details";
// string cbtext = "Employee_Code";
fill(str, tstr);
filltext(str, tstr);
comboBox1.SelectedIndex = 0;

// listView1.SelectedItems =
listView1.Items.IndexOfKey("Emp001");
//MessageBox.Show(listView1.Items.Count.ToString());
//txtcode.Text =
(listView1.Items[listView1.FocusedItem.Index].SubItems[0].Text);

}
catch (Exception err) {
MessageBox.Show(err.ToString()); }

}
private void textBox1_TextChanged(object sender,
EventArgs e)
{
//search the data which you enter in text box and select
the field from combo.
try
{
string cbtext = comboBox1.Text;
string str = "Select * from Employee_Details " +
"where " + comboBox1.Text + " like'%" + textBox1.Text + "%'";
string tstr = "Employee_Details";
fill(str, tstr);
//filltext(str, tstr, cbtext);
}
catch (Exception err) {
MessageBox.Show(err.ToString()); }
}

private void btnadd_Click(object sender, EventArgs e)


{
label10.Visible = true;
label10.Text = "Adding Records";
unable(true);
txtjoin.Enabled = false;
txtjoin.Text = DateTime.Now.ToShortDateString();
txtcode.Text = "";
txtname.Text = "";
txtaddress.Text = "";
txtphone.Text = "";
txtsal.Text = "";

private void btnEdit_Click(object sender, EventArgs e)


{
label10.Visible = true;
label10.Text = "Editing Record : First Click on Record
list which you want to Edit";
states = true;
unable(true);
txtjoin.Enabled = false;
txtjoin.Text = DateTime.Now.ToShortDateString();

private void btnref_Click(object sender, EventArgs e)


{
unable(false);
try
{
string str = "Select * from Employee_Details order by
Employee_Code";
string tstr = "Employee_Details";
fill(str, tstr);
filltext(str, tstr);
comboBox1.SelectedIndex = 0;
}
catch (Exception er) { MessageBox.Show(er.ToString()); }
}

private void btnClose_Click(object sender, EventArgs e)


{
//close the form
this.Close();
}

private void btndelete_Click(object sender, EventArgs e)


{
deleterecord();
}

private void btnsave_Click(object sender, EventArgs e)


{
if (states == false)
{
saverecord();
}
else { updaterecord(); }
}

private void listView1_SelectedIndexChanged(object sender,


EventArgs e)
{
txtcode.Text =
(listView1.Items[listView1.FocusedItem.Index].SubItems[0].Text);
txtname.Text =
(listView1.Items[listView1.FocusedItem.Index].SubItems[1].Text);
txtaddress.Text =
(listView1.Items[listView1.FocusedItem.Index].SubItems[2].Text);
txtphone.Text =
(listView1.Items[listView1.FocusedItem.Index].SubItems[3].Text);
txtsal.Text =
(listView1.Items[listView1.FocusedItem.Index].SubItems[4].Text);
txtjoin.Text =
(listView1.Items[listView1.FocusedItem.Index].SubItems[5].Text);
}

}
}

You might also like