CNSA-212-Personal/Database3/frmSearch.cs

143 lines
4.1 KiB
C#
Raw Normal View History

2024-02-13 10:30:17 -08:00
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
2024-02-14 06:26:08 -08:00
2024-02-13 11:30:26 -08:00
2024-02-13 10:30:17 -08:00
namespace Database3
{
public partial class frmSearch : Form
{
public static string myID = "";
2024-02-13 10:30:17 -08:00
public frmSearch()
{
InitializeComponent();
dgvStudents.DoubleClick += new EventHandler(dgvStudents_DoubleClick);
}
private void dgvStudents_DoubleClick(object sender, EventArgs e)
{
string studentid = "";
if (dgvStudents.Rows.Count > 0)
{
DataGridViewRow row = dgvStudents.SelectedRows[0];
2024-02-15 09:04:47 -08:00
frmEdit aform = new frmEdit(this);
studentid = row.Cells[0].Value.ToString().Trim();
myID = studentid;
aform.ShowDialog();
}
2024-02-13 10:30:17 -08:00
}
2024-02-13 10:34:44 -08:00
2024-02-15 08:52:59 -08:00
2024-02-15 09:04:47 -08:00
public void btnSearch_Click(object sender, EventArgs e)
2024-02-13 10:34:44 -08:00
{
2024-02-13 12:46:12 -08:00
string studentid = "9999999999";
string lname = "9999999999";
2024-02-14 06:26:08 -08:00
DateTime dob = new DateTime();
2024-02-13 13:59:13 -08:00
ErrorProvider ep1 = new ErrorProvider();
2024-02-13 12:46:12 -08:00
2024-02-13 11:30:26 -08:00
try
{
DataSet ds = new DataSet();
StudentDataTier stuDT = new StudentDataTier();
2024-02-13 12:46:12 -08:00
studentid = txtStuID.Text;
lname = txtlname.Text;
2024-02-13 13:59:13 -08:00
try
{
dob = DateTime.Parse(txtDOB.Text);
2024-02-13 11:30:26 -08:00
2024-02-13 12:46:12 -08:00
}
catch (Exception exception)
{
2024-02-13 13:59:13 -08:00
ep1.SetError(txtDOB, "Non valid value, but that's ok");
}
if (string.IsNullOrWhiteSpace(txtStuID.Text) &&
string.IsNullOrWhiteSpace(txtlname.Text) &&
string.IsNullOrWhiteSpace(txtDOB.Text))
{
throw new Exception("Must Enter at least one value");
2024-02-13 12:46:12 -08:00
}
// Get the dataset using all three search parameters.
ds = stuDT.GetStudents(studentid, lname, dob);
// Check to see if any record is returned.
if (ds.Tables[0].Rows.Count > 0) // There is a record.
2024-02-13 11:30:26 -08:00
{
dgvStudents.Visible = true;
2024-02-13 12:46:12 -08:00
// Get data source.
2024-02-13 11:30:26 -08:00
dgvStudents.DataSource = ds.Tables[0];
dgvStudents.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGreen;
// Set the row and column header styles.
dgvStudents.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
dgvStudents.ColumnHeadersDefaultCellStyle.BackColor = Color.Green;
}
2024-02-13 12:46:12 -08:00
else
{
dgvStudents.Visible = false; // Hide the DataGridView if no results are found.
MessageBox.Show("No records found.");
}
2024-02-13 11:30:26 -08:00
}
catch (Exception ex)
{
2024-02-13 12:46:12 -08:00
MessageBox.Show($"An error occurred: {ex.Message}");
2024-02-13 11:30:26 -08:00
}
2024-02-13 10:34:44 -08:00
}
2024-02-14 07:02:15 -08:00
private void frmSearch_Load(object sender, EventArgs e)
{
dgvStudents.Visible = false;
contextMenuStrip1.Items[1].Enabled = false;
}
private void cmuSearch_Click(object sender, EventArgs e)
{
btnSearch_Click(sender,e);
}
private void txtStuID_TextChanged(object sender, EventArgs e)
{
cmuSearch.Enabled = txtStuID.Text.Length>0;
}
private void cmuEdit_Click(object sender, EventArgs e)
2024-02-14 07:02:15 -08:00
{
dgvStudents_DoubleClick(sender, e);
2024-02-13 12:46:12 -08:00
}
2024-02-15 08:52:59 -08:00
private void cmuDelete_Click(object sender, EventArgs e)
{
string studentid = "";
DataSet ds = new DataSet();
StudentDataTier stuDT = new StudentDataTier();
DataGridViewRow row = dgvStudents.SelectedRows[0];
studentid = row.Cells[0].Value.ToString().Trim();
stuDT.DeleteStudents(studentid);
btnSearch_Click(sender, e);
}
2024-02-14 07:02:15 -08:00
}
2024-02-13 11:30:26 -08:00
2024-02-13 10:30:17 -08:00
}