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

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

The document defines a Windows Forms application that manages department data from a SQL database. It connects to the database, retrieves department records into a DataTable, and displays them in a DataGridView. Buttons allow navigating between records and performing CRUD operations by updating the DataTable and calling a SqlDataAdapter. Methods handle loading data initially and responding to button clicks for navigation, updating, deleting, adding new records.
Copyright
© © All Rights Reserved
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)
39 views

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

The document defines a Windows Forms application that manages department data from a SQL database. It connects to the database, retrieves department records into a DataTable, and displays them in a DataGridView. Buttons allow navigating between records and performing CRUD operations by updating the DataTable and calling a SqlDataAdapter. Methods handle loading data initially and responding to button clicks for navigation, updating, deleting, adding new records.
Copyright
© © All Rights Reserved
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/ 3

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Data.SqlClient;

namespace HRMIS2
{
public partial class Departments : Form
{
SqlConnection con = new SqlConnection();
SqlDataAdapter daDept;
SqlCommandBuilder cb;
DataTable dtDept = new DataTable();
int rowPosition = 0;

public Departments()
{
InitializeComponent();
}

private void Departments_Load(object sender, EventArgs e)


{
con.ConnectionString = @"Data Source=EMASPC\SQLEXPRESS;Initial
Catalog=hr;Integrated Security=True";
con.Open();
daDept = new SqlDataAdapter("SELECT * FROM Departments",con);
cb = new SqlCommandBuilder(daDept);
daDept.Fill(dtDept);

dataGridViewDept.DataSource = dtDept;

showCurrentRecord();
}

private void Departments_FormClosing(object sender, FormClosingEventArgs e)


{
con.Close();
con.Dispose();
}

private void showCurrentRecord()


{
if (dtDept.Rows.Count == 0)
{
textBoxDeptId.Text = "";
textBoxDeptName.Text = "";
}
else
{
textBoxDeptId.Text = dtDept.Rows[rowPosition]["did"].ToString();
textBoxDeptName.Text = dtDept.Rows[rowPosition]["dname"].ToString();
}
}

private void buttonNext_Click(object sender, EventArgs e)


{

if (rowPosition < dtDept.Rows.Count - 1)


{
rowPosition++;
showCurrentRecord();
}
else
{
MessageBox.Show("IHEREREZO");
}

private void buttonPrevious_Click(object sender, EventArgs e)


{
if (rowPosition !=0)
{
rowPosition--;
showCurrentRecord();
}
else
{
MessageBox.Show("INTANGIRIRO");
}
}

private void buttonLast_Click(object sender, EventArgs e)


{
if (rowPosition != dtDept.Rows.Count - 1)
{
rowPosition = dtDept.Rows.Count - 1;
showCurrentRecord();
}
else
{
MessageBox.Show("END OF RECORDS");
}
}

private void buttonFirst_Click(object sender, EventArgs e)


{
if (rowPosition != 0)
{
rowPosition = 0;
showCurrentRecord();
}
else
{
MessageBox.Show("START OF RECORDS");
}
}
private void buttonUpdate_Click(object sender, EventArgs e)
{
if(dtDept.Rows.Count != 0)
{
dtDept.Rows[rowPosition]["did"] = textBoxDeptId.Text;
dtDept.Rows[rowPosition]["dname"] = textBoxDeptName.Text;
daDept.Update(dtDept);

MessageBox.Show("UPDATE SECCESSFUL");
}

private void buttonDelete_Click(object sender, EventArgs e)


{
if (dtDept.Rows.Count != 0)
{
dtDept.Rows[rowPosition].Delete();
daDept.Update(dtDept);
MessageBox.Show("DELETE SECCESS");
}
}

private void buttonAdd_Click(object sender, EventArgs e)


{
textBoxDeptId.Text = "";
textBoxDeptName.Text = "";
}

private void buttonSave_Click(object sender, EventArgs e)


{
DataRow drDept = dtDept.NewRow();
dtDept.Rows[rowPosition]["did"] = textBoxDeptId.Text;
dtDept.Rows[rowPosition]["dname"] = textBoxDeptName.Text;
dtDept.Rows.Add(drDept);
daDept.Update(dtDept);
MessageBox.Show("SAVE SECCESSFULL");
}
}
}

You might also like