protected void Deletedata(object s, GridViewDeleteEventArgs e) { dependants = GetExistingData(); //DataSet ds = gv.DataSource as DataSet; //ds.Tables[0].Rows[gv.Rows[e.RowIndex].DataItemIndex].Delete(); //ds.WriteXml(Server.MapPath("empdata.xml")); int id = dependants[e.RowIndex].IdStudentDependant; StudentDependant sp = entity.StudentDependants.Where(p => p.IdStudentDependant == id).FirstOrDefault(); entity.StudentDependants.DeleteObject(sp); entity.SaveChanges(); dependants.RemoveAt(e.RowIndex); BindData(dependants); Cache["Dependants"] = dependants; }
protected void Save(object sender, EventArgs e) { dependants = GetExistingData(); DateTime dob; DateTime.TryParse(tbDDateOfBirth.Text, out dob); int idGender = int.Parse(lbDGenders.SelectedValue); Gender gender = entity.Genders.Where(p => p.IdGender == idGender).FirstOrDefault(); int idDependantType = int.Parse(lbDependantTypes.SelectedValue); DependantType dType = entity.DependantTypes.Where(p => p.IdDependantType == idDependantType).FirstOrDefault(); Student student = entity.Students.Where(p => p.ReferenceNumber == hfReference.Value).FirstOrDefault(); StudentDependant newDep = new StudentDependant { DateOfBirth = dob, IdDependantType = idDependantType, Gender = gender, Name = tbDName.Text, Surname = tbDSurname.Text, IdGender = idGender, DependantType = dType, IdStudent = student.IdStudent }; dependants.Add(newDep); entity.AddToStudentDependants(newDep); entity.SaveChanges(); //DataSet ds = gv.DataSource as DataSet; //DataRow dr = ds.Tables[0].NewRow(); //dr[0] = txtempID.Text; //dr[1] = txtName.Text; //dr[2] = txtCity.Text; //dr[3] = txtSalary.Text; //ds.Tables[0].Rows.Add(dr); //ds.AcceptChanges(); //ds.WriteXml(Server.MapPath("empdata.xml")); Cache["Dependants"] = dependants; BindData(dependants); tbDName.Text = string.Empty; tbDSurname.Text = string.Empty; tbDDateOfBirth.Text = string.Empty; }