/// <summary> /// Update a homework record in the database by hw_id /// </summary> /// <param name="hw_id">the specific homework to update</param> /// <param name="hw">the new homework to update</param> /// <returns>string of an error or a string.Empty if the action is completed</returns> public static string UpdateHomework(int hw_id, ch_homework hw) { try { DataSet ds_hw = GetHomeworks(); ds_hw.Tables[0].Columns.Remove("pro_name"); ds_hw.Tables[0].Columns.Remove("les_name"); ds_hw.Tables[0].Columns.Remove("hr_name"); ds_hw.Tables[0].Columns.Remove("hw_timeleft"); foreach (DataRow dr_hw in ds_hw.Tables[0].Rows) { int current_hw_id = Convert.ToInt32(dr_hw["hw_id"]); if (current_hw_id == hw_id) { dr_hw["les_id"] = hw.les_Id; dr_hw["hr_id"] = hw.hr_Id; dr_hw["hw_txt"] = hw.hw_Txt; dr_hw["hw_deadlinedate"] = hw.hw_Deadlinedate; dr_hw["hw_editdate"] = DateTime.Now.ToShortDateString(); } } ds_hw.WriteXml(HttpContext.Current.Server.MapPath("App_Data/homework.xml")); return(""); } catch (Exception) { return("אירעה תקלה בעת עדכון הנתונים.\n נסה שוב מאוחר יותר"); } }
protected void btnAddHw_Click(object sender, EventArgs e) { //check validation if (!ValidatePage()) { return; } int les_id = Convert.ToInt32(ddlLessons.SelectedValue); int hr_id = Convert.ToInt32(ddlDeadlineHr.SelectedValue); ch_homework hw = new ch_homework(les_id, txtHw.Text, txtDeadlineDate.Text, hr_id); lblErr.Text = ch_homeworkSvc.AddHomework(hw); if (lblErr.Text == "") { Response.Redirect("Homework.aspx"); } }
/// <summary> /// Add homework to the database /// </summary> /// <param name="hw">the new homework to add</param> /// <returns>string of an error or a string.Empty if the action is completed</returns> public static string AddHomework(ch_homework hw) { try { //file name string filename = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, @"App_Data\homework.xml"); //create new instance of XmlDocument XmlDocument doc = new XmlDocument(); //load from file doc.Load(filename); //create node and add value XmlNode node = doc.CreateNode(XmlNodeType.Element, "homework_item", null); //create hw_id node XmlNode node_hw_id = doc.CreateElement("hw_id"); //add value for it node_hw_id.InnerText = GetMaxId() + 1 + ""; //create les_id node XmlNode node_les_id = doc.CreateElement("les_id"); node_les_id.InnerText = hw.les_Id.ToString(); //create hw_pubdate node XmlNode node_hw_pubdate = doc.CreateElement("hw_pubdate"); node_hw_pubdate.InnerText = DateTime.Now.ToShortDateString(); //create hw_deadlinedate node XmlNode node_hw_deadlinedate = doc.CreateElement("hw_deadlinedate"); node_hw_deadlinedate.InnerText = hw.hw_Deadlinedate; //create hr_id node XmlNode node_hr_id = doc.CreateElement("hr_id"); node_hr_id.InnerText = hw.hr_Id.ToString(); //create hw_txt node XmlNode node_hw_txt = doc.CreateElement("hw_txt"); node_hw_txt.InnerText = hw.hw_Txt; //create hw_editdate node XmlNode node_hw_editdate = doc.CreateElement("hw_editdate"); node_hw_editdate.InnerText = ""; //add to parent node node.AppendChild(node_hw_id); node.AppendChild(node_les_id); node.AppendChild(node_hw_pubdate); node.AppendChild(node_hw_deadlinedate); node.AppendChild(node_hr_id); node.AppendChild(node_hw_txt); node.AppendChild(node_hw_editdate); //add to elements collection doc.DocumentElement.AppendChild(node); //save back doc.Save(filename); return(""); } catch (Exception) { return("אירעה תקלה בעת הכנסת הנתונים לשרת.\n נסה שוב מאוחר יותר"); } }