semi working multi search

This commit is contained in:
EggMan20339 2024-02-13 15:46:12 -05:00
parent 63402fb1f4
commit 9212fe4650
2 changed files with 34 additions and 14 deletions

View File

@ -10,7 +10,7 @@ using System.Collections;
using System.Data.SqlClient;
namespace Database2
namespace Database3
{
public class StudentDataTier
{
@ -18,7 +18,7 @@ namespace Database2
static SqlConnection myConn = new SqlConnection(connString);
static System.Data.SqlClient.SqlCommand cmdString = new System.Data.SqlClient.SqlCommand();
public DataSet GetStudents(string studid)
public DataSet GetStudents(string studid, string lname, string dob)
{
try
{
@ -30,9 +30,11 @@ namespace Database2
cmdString.Connection = myConn;
cmdString.CommandType = CommandType.StoredProcedure;
cmdString.CommandTimeout = 1500;
cmdString.CommandText = "GetStudentByID";
cmdString.CommandText = "SearchStudent";
// Define input parameter
cmdString.Parameters.Add("@studentid", SqlDbType.VarChar, 6).Value = studid;
cmdString.Parameters.Add("@lname", SqlDbType.VarChar, 25).Value = lname;
cmdString.Parameters.Add("@dob", SqlDbType.Date).Value = dob;
// adapter and dataset
SqlDataAdapter aAdapter = new SqlDataAdapter();
aAdapter.SelectCommand = cmdString;

View File

@ -17,7 +17,7 @@ using System.Data;
using System.Configuration;
using System.Collections;
using System.Data.SqlClient;
using Database2;
using Database3;
namespace Database3
@ -31,23 +31,36 @@ namespace Database3
private void btnSearch_Click(object sender, EventArgs e)
{
string studentid = "";
string studentid = "9999999999";
string lname = "9999999999";
string dob = null;
try
{
DataSet ds = new DataSet();
StudentDataTier stuDT = new StudentDataTier();
studentid = txtStuID.Text.Trim();
try
{
//get the dataset
ds = stuDT.GetStudents(studentid);
studentid = txtStuID.Text;
lname = txtlname.Text;
dob = txtDOB.Text;
// check to see if any record is returned
if (ds.Tables[0].Rows.Count > 0) //there is a record
}
catch (Exception exception)
{
MessageBox.Show("Error Parsing Search Parameters","Error", MessageBoxButtons.OK);
}
// 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.
{
dgvStudents.Visible = true;
// get data source
// Get data source.
dgvStudents.DataSource = ds.Tables[0];
dgvStudents.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGreen;
@ -55,12 +68,17 @@ namespace Database3
dgvStudents.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
dgvStudents.ColumnHeadersDefaultCellStyle.BackColor = Color.Green;
}
else
{
dgvStudents.Visible = false; // Hide the DataGridView if no results are found.
MessageBox.Show("No records found.");
}
}
catch (Exception ex)
{
// Handle exception
MessageBox.Show($"An error occurred: {ex.Message}");
}
}
}
}
}