diff --git a/Database2/Database2.csproj b/Database2/Database2.csproj index 45b392c..56c6282 100644 --- a/Database2/Database2.csproj +++ b/Database2/Database2.csproj @@ -34,6 +34,7 @@ + diff --git a/Database3/App.config b/Database3/App.config index 787dcbe..b90b072 100644 --- a/Database3/App.config +++ b/Database3/App.config @@ -3,4 +3,8 @@ + + + + \ No newline at end of file diff --git a/Database3/Database3.csproj b/Database3/Database3.csproj index 553de8d..ab254a9 100644 --- a/Database3/Database3.csproj +++ b/Database3/Database3.csproj @@ -34,6 +34,7 @@ + @@ -54,6 +55,7 @@ + frmSearch.cs diff --git a/Database3/StudentDataTier.cs b/Database3/StudentDataTier.cs new file mode 100644 index 0000000..1673110 --- /dev/null +++ b/Database3/StudentDataTier.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.VisualBasic; +using System.Data; +using System.Configuration; +using System.Collections; +using System.Data.SqlClient; + + +namespace Database2 +{ + public class StudentDataTier + { + static String connString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString; + static SqlConnection myConn = new SqlConnection(connString); + static System.Data.SqlClient.SqlCommand cmdString = new System.Data.SqlClient.SqlCommand(); + + public DataSet GetStudents(string studid) + { + try + { + // open connection + myConn.Open(); + //clear any parameters + cmdString.Parameters.Clear(); + // command + cmdString.Connection = myConn; + cmdString.CommandType = CommandType.StoredProcedure; + cmdString.CommandTimeout = 1500; + cmdString.CommandText = "GetStudentByID"; + // Define input parameter + cmdString.Parameters.Add("@studentid", SqlDbType.VarChar, 6).Value = studid; + // adapter and dataset + SqlDataAdapter aAdapter = new SqlDataAdapter(); + aAdapter.SelectCommand = cmdString; + DataSet aDataSet = new DataSet(); + + // fill adapter + aAdapter.Fill(aDataSet); + + // return dataSet + return aDataSet; + } + catch (Exception ex) + { + throw new ArgumentException(ex.Message); + } + finally + { + myConn.Close(); + } + } + + + } + + +} \ No newline at end of file diff --git a/Database3/frmSearch.Designer.cs b/Database3/frmSearch.Designer.cs index 338b7b0..5e1d7cc 100644 --- a/Database3/frmSearch.Designer.cs +++ b/Database3/frmSearch.Designer.cs @@ -30,14 +30,14 @@ private void InitializeComponent() { this.lblDisStID = new System.Windows.Forms.Label(); - this.dgvTable = new System.Windows.Forms.DataGridView(); + this.dgvStudents = new System.Windows.Forms.DataGridView(); this.lblDislname = new System.Windows.Forms.Label(); this.lblDisDOB = new System.Windows.Forms.Label(); this.txtStuID = new System.Windows.Forms.TextBox(); this.txtlname = new System.Windows.Forms.TextBox(); this.txtDOB = new System.Windows.Forms.TextBox(); this.btnSearch = new System.Windows.Forms.Button(); - ((System.ComponentModel.ISupportInitialize)(this.dgvTable)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.dgvStudents)).BeginInit(); this.SuspendLayout(); // // lblDisStID @@ -49,13 +49,13 @@ this.lblDisStID.Text = "Student ID:"; this.lblDisStID.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // - // dgvTable + // dgvStudents // - this.dgvTable.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; - this.dgvTable.Location = new System.Drawing.Point(77, 250); - this.dgvTable.Name = "dgvTable"; - this.dgvTable.Size = new System.Drawing.Size(638, 150); - this.dgvTable.TabIndex = 1; + this.dgvStudents.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dgvStudents.Location = new System.Drawing.Point(77, 250); + this.dgvStudents.Name = "dgvStudents"; + this.dgvStudents.Size = new System.Drawing.Size(638, 150); + this.dgvStudents.TabIndex = 1; // // lblDislname // @@ -108,6 +108,7 @@ // // frmSearch // + this.AcceptButton = this.btnSearch; this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); @@ -117,11 +118,11 @@ this.Controls.Add(this.txtStuID); this.Controls.Add(this.lblDisDOB); this.Controls.Add(this.lblDislname); - this.Controls.Add(this.dgvTable); + this.Controls.Add(this.dgvStudents); this.Controls.Add(this.lblDisStID); this.Name = "frmSearch"; this.Text = "Form1"; - ((System.ComponentModel.ISupportInitialize)(this.dgvTable)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.dgvStudents)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } @@ -134,7 +135,7 @@ private System.Windows.Forms.Button btnSearch; private System.Windows.Forms.Label lblDisStID; - private System.Windows.Forms.DataGridView dgvTable; + private System.Windows.Forms.DataGridView dgvStudents; #endregion } diff --git a/Database3/frmSearch.cs b/Database3/frmSearch.cs index 322d098..1427f13 100644 --- a/Database3/frmSearch.cs +++ b/Database3/frmSearch.cs @@ -7,6 +7,18 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.VisualBasic; +using System.Data; +using System.Configuration; +using System.Collections; +using System.Data.SqlClient; +using Database2; + namespace Database3 { @@ -19,7 +31,36 @@ namespace Database3 private void btnSearch_Click(object sender, EventArgs e) { - + string studentid = ""; + + try + { + DataSet ds = new DataSet(); + StudentDataTier stuDT = new StudentDataTier(); + + studentid = txtStuID.Text.Trim(); + + //get the dataset + ds = stuDT.GetStudents(studentid); + + // 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 + 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; + } + } + catch (Exception ex) + { + // Handle exception + } } + } } \ No newline at end of file