From 48dff87b84d59bbce13a26ec2bdb83f1b18949ba Mon Sep 17 00:00:00 2001 From: EggMan20339 <99349302+EggMan20339@users.noreply.github.com> Date: Mon, 25 Mar 2024 12:09:20 -0400 Subject: [PATCH] delete and edit and new work --- CH78/Display.aspx.cs | 88 +++++++++++++++--- CH78/Home.aspx | 8 +- CH78/Home.aspx.cs | 67 ++++++++++++-- CH78/Home.aspx.designer.cs | 4 +- CH78/StudentDataTier.cs | 104 ++++++++++++++++++++-- CNSA-216-Personal-v2.sln.DotSettings.user | 2 + 6 files changed, 238 insertions(+), 35 deletions(-) create mode 100644 CNSA-216-Personal-v2.sln.DotSettings.user diff --git a/CH78/Display.aspx.cs b/CH78/Display.aspx.cs index 85d69f4..009654b 100644 --- a/CH78/Display.aspx.cs +++ b/CH78/Display.aspx.cs @@ -2,11 +2,13 @@ using System.Web.UI; using System.Data; using System.Drawing; +using System.Security.Principal; namespace CH78 { public partial class Display : Page { + protected string pageType; protected void Page_Load(object sender, EventArgs e) { string studentID = ""; @@ -14,6 +16,8 @@ namespace CH78 if (!IsPostBack) { + pageType = Request.QueryString["type"].Trim().ToUpper(); + // retrieve the querystring if (String.IsNullOrEmpty(Request.QueryString["ID"])) { @@ -23,7 +27,7 @@ namespace CH78 else if (Request.QueryString["ID"].Trim().ToUpper() == "NEW") { - // do nothing + BindDDL(); } else if (Request.QueryString["type"].Trim().ToUpper() == "VIEW") // display @@ -81,17 +85,7 @@ namespace CH78 //txtSalary.Text = mysalary.ToString("C") //txtSalary.Enabled = False - txtState.DataSource = StudentDataTier.GetStates(); - txtState.DataTextField = "Name"; - txtState.DataValueField = "StateID"; - txtState.SelectedValue = "1"; - txtState.DataBind(); - - txtGender.DataSource = StudentDataTier.GetGenders(); - txtGender.DataTextField = "CommonName"; - txtGender.DataValueField = "Gender_ID"; - txtGender.SelectedValue = "0"; - txtGender.DataBind(); + BindDDL(); // txtState.SelectedValue = ds.Tables[0].Rows[0]["state"].ToString(); if (type.ToUpper() == "VIEW") @@ -104,6 +98,18 @@ namespace CH78 } else if (type.ToUpper() == "EDIT") { + txtGender.Enabled = true; + txtStuID.Enabled = false; + btnGoStu.Visible = true; + btnCancelStu.Visible = true; + btnGoStu.Text = "UPDATE"; + btnCancelStu.Text = "REFRESH"; + btnGoStu.ForeColor = Color.Red; + btnCancelStu.ForeColor = Color.Blue; + } + else if (type.ToUpper() == "NEW") + { + txtGender.Enabled = true; txtStuID.Enabled = false; btnGoStu.Visible = true; btnCancelStu.Visible = true; @@ -115,14 +121,68 @@ namespace CH78 } } + protected void BindDDL() + { + txtState.DataSource = StudentDataTier.GetStates(); + txtState.DataTextField = "Name"; + txtState.DataValueField = "StateID"; + txtState.SelectedValue = "1"; + txtState.DataBind(); + + txtGender.DataSource = StudentDataTier.GetGenders(); + txtGender.DataTextField = "CommonName"; + txtGender.DataValueField = "Gender_ID"; + txtGender.SelectedValue = "0"; + txtGender.DataBind(); + } + protected void btnCancelStu_OnClick(object sender, EventArgs e) { - throw new NotImplementedException(); + } protected void btnGoStu_OnClick(object sender, EventArgs e) { - throw new NotImplementedException(); + + if (Request.QueryString["type"].Trim().ToUpper() == "VIEW") + { + } + else if (Request.QueryString["type"].Trim().ToUpper() == "EDIT") + { StudentDataTier.UpdateStudents(0, + txtStuID.Text, + txtFNAME.Text, + txtLNAME.Text, + txtMidInit.Text, + DateTime.Parse(txtDOB.Text), + txtGender.Text, + decimal.Parse(txtActBal.Text), + txtMajor.Text, txtCellPhone.Text, + txtAdr1.Text, + txtCity.Text, + txtState.ToString(), + txtZip.Text, + decimal.Parse(txtCreds.Text)); + } + else if (Request.QueryString["type"].Trim().ToUpper() == "NEW") + { StudentDataTier.UpdateStudents(1, + txtStuID.Text, + txtFNAME.Text, + txtLNAME.Text, + txtMidInit.Text, + DateTime.Parse(txtDOB.Text), + txtGender.Text.ToString(), + decimal.Parse(txtActBal.Text), + txtMajor.Text, txtCellPhone.Text, + txtAdr1.Text, + txtCity.Text, + txtState.Text.ToString(), + txtZip.Text, + decimal.Parse(txtCreds.Text)); + } + + + } + } } \ No newline at end of file diff --git a/CH78/Home.aspx b/CH78/Home.aspx index 998500e..2eca2ad 100644 --- a/CH78/Home.aspx +++ b/CH78/Home.aspx @@ -47,11 +47,7 @@ function SelectAll(id)   - - New - Student - + @@ -95,7 +91,7 @@ function SelectAll(id) - + diff --git a/CH78/Home.aspx.cs b/CH78/Home.aspx.cs index 21f9b99..b80e930 100644 --- a/CH78/Home.aspx.cs +++ b/CH78/Home.aspx.cs @@ -122,10 +122,10 @@ namespace CH78 txtFName.Text = FNAME; txtLName.Text = LNAME; - if (textHasValues) - { + // if (textHasValues) + // { ds = StudentDataTier.SearchStudents(FNAME, LNAME, stuID); - } + // } // ds = dt.GetStudents(); @@ -190,7 +190,35 @@ namespace CH78 protected void Delete_Click(object sender, CommandEventArgs e) { - + try + { + CheckBox chk = new CheckBox(); + Label lbl = new Label(); + string stuid = ""; + StudentDataTier std = new StudentDataTier(); + if (gvStudent.Rows.Count > 0) // only do it if there is a row + { + //For Each item As GridView In grdCustomer.items + foreach (GridViewRow row in gvStudent.Rows) + { + //get the selected checkbox + chk = (CheckBox)row.FindControl("chkStudentID"); + if (chk.Checked) + { + lbl = (Label)row.Controls[0].FindControl("hidStudentID"); + stuid = lbl.Text.Trim(); + //delete the record one at a time + StudentDataTier.DeleteStudents(stuid); + } + } + //refresh datagrid + BindData(); + } + } + catch (Exception ex) + { + throw new Exception(ex.Message, ex.InnerException); + } } protected void lbtnEdit_Click(object sender, CommandEventArgs e) @@ -211,7 +239,7 @@ namespace CH78 // this script will open a popup sb.Append(""); // register with ClientScript @@ -222,5 +250,34 @@ namespace CH78 throw new Exception(ex.Message, ex.InnerException); } } + + protected void btnNew_OnClick(object sender, EventArgs e) + { + + string recordToBeEdited; + Int64 mEditedRecord = 0; + System.Text.StringBuilder sb = new System.Text.StringBuilder(); + + try + { + Session["vStudentID"] = txtStudentID.Text.Trim(); + Session["vFName"] = txtFName.Text.Trim(); + Session["vLName"] = txtLName.Text.Trim(); + + // this script will open a popup + sb.Append(""); + + // register with ClientScript + Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "PopupScript", sb.ToString()); + } + catch (Exception ex) + { + throw new Exception(ex.Message, ex.InnerException); + } + + } } } \ No newline at end of file diff --git a/CH78/Home.aspx.designer.cs b/CH78/Home.aspx.designer.cs index 950d233..9f72076 100644 --- a/CH78/Home.aspx.designer.cs +++ b/CH78/Home.aspx.designer.cs @@ -24,13 +24,13 @@ namespace CH78 protected global::System.Web.UI.HtmlControls.HtmlForm frmHome; /// - /// hplNew control. + /// btnNew control. /// /// /// Auto-generated field. /// To modify move field declaration from designer file to code-behind file. /// - protected global::System.Web.UI.WebControls.HyperLink hplNew; + protected global::System.Web.UI.WebControls.Button btnNew; /// /// Label1 control. diff --git a/CH78/StudentDataTier.cs b/CH78/StudentDataTier.cs index 5728581..b051d54 100644 --- a/CH78/StudentDataTier.cs +++ b/CH78/StudentDataTier.cs @@ -12,13 +12,6 @@ namespace CH78 static String connString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString; static SqlConnection myConn = new SqlConnection(connString); static System.Data.SqlClient.SqlCommand cmdString = new System.Data.SqlClient.SqlCommand(); - - private void BindData() - { - - - - } protected void grdStudnet_RowDataBind(object sender, GridViewRowEventArgs e) { @@ -119,7 +112,7 @@ namespace CH78 cmdString.CommandType = CommandType.StoredProcedure; cmdString.CommandTimeout = 1500; cmdString.CommandText = "GetAllGenders"; - // adapter and dataset + // adapter and dataset SqlDataAdapter aAdapter = new SqlDataAdapter(); aAdapter.SelectCommand = cmdString; DataSet aDataSet = new DataSet(); @@ -140,6 +133,101 @@ namespace CH78 } } + public static void DeleteStudents(string StuID) + { + try + { + // open connection + myConn.Open(); + //clear any parameters + cmdString.Parameters.Clear(); + // command + cmdString.Connection = myConn; + cmdString.CommandType = CommandType.StoredProcedure; + cmdString.CommandTimeout = 1500; + cmdString.CommandText = "DELETESTUDENT"; + + cmdString.Parameters.Add("@studentid", SqlDbType.VarChar, 6).Value = StuID; + + cmdString.ExecuteNonQuery(); + + } + catch (Exception ex) + { + throw new ArgumentException(ex.Message); + } + finally + { + myConn.Close(); + } + } + + + public static void UpdateStudents( + int update, + string student_id, + string fname, + string lname, + string midinit, + DateTime dob, + string gender, + Decimal acct_bal, + string major, + // string home_phone, + string cell_phone, + // string work_phone, + string address_one, + string city, + string stu_state, + string zip, + Decimal totalcredits) + { + try + { + string home_phone = "0"; + string work_phone = "0"; + + // open connection + myConn.Open(); + //clear any parameters + cmdString.Parameters.Clear(); + // command + cmdString.Connection = myConn; + cmdString.CommandType = CommandType.StoredProcedure; + cmdString.CommandTimeout = 1500; + cmdString.CommandText = "UpdateStudent"; + + cmdString.Parameters.Add("@update", SqlDbType.Int).Value = update; + cmdString.Parameters.Add("@student_id", SqlDbType.VarChar, 6).Value = student_id; + cmdString.Parameters.Add("@fname", SqlDbType.VarChar, 25).Value = fname; + cmdString.Parameters.Add("@lname", SqlDbType.VarChar, 25).Value = lname; + cmdString.Parameters.Add("@midinit", SqlDbType.Char).Value = midinit; + cmdString.Parameters.Add("@dob", SqlDbType.Date).Value = dob; + cmdString.Parameters.Add("@gender", SqlDbType.Char, 6).Value = gender; + cmdString.Parameters.Add("@acct_bal", SqlDbType.Decimal, 7).Value = acct_bal; + cmdString.Parameters.Add("@major", SqlDbType.VarChar, 60).Value = major; + cmdString.Parameters.Add("@home_phone", SqlDbType.VarChar, 15).Value = home_phone; + cmdString.Parameters.Add("@cell_phone", SqlDbType.VarChar, 15).Value = cell_phone; + cmdString.Parameters.Add("@work_phone", SqlDbType.VarChar, 15).Value = work_phone; + cmdString.Parameters.Add("@address_one", SqlDbType.VarChar, 60).Value = address_one; + cmdString.Parameters.Add("@city", SqlDbType.VarChar, 60).Value = city; + cmdString.Parameters.Add("@stu_state", SqlDbType.Char, 2).Value = stu_state; + cmdString.Parameters.Add("@zip", SqlDbType.VarChar, 5).Value = zip; + cmdString.Parameters.Add("@totalcredits", SqlDbType.Decimal, 5).Value = totalcredits; + + cmdString.ExecuteNonQuery(); + + } + catch (Exception ex) + { + throw new ArgumentException(ex.Message); + } + finally + { + myConn.Close(); + } + } + public static DataSet SearchStudents(string fname, string lname, string stuID) { diff --git a/CNSA-216-Personal-v2.sln.DotSettings.user b/CNSA-216-Personal-v2.sln.DotSettings.user new file mode 100644 index 0000000..f591ba1 --- /dev/null +++ b/CNSA-216-Personal-v2.sln.DotSettings.user @@ -0,0 +1,2 @@ + + INFO \ No newline at end of file