2013-04-03 128 views
1

我正在使用實體框架,現在我正在保存兩個作業,並希望將它們展示給我的gridview。我如何清除會話?

在這裏,我將它們保存到我的數據庫表,並顯示他們在GridView: enter image description here

這裏的代碼:

protected void ButtonAddAssignmentClick(object sender, EventArgs e) 
{ 
    Session.Remove("DataTable"); 

//Add to DB and show in gridview 
using (var db = new KnowItCvdbEntities()) 
    { 
     SPWeb theSite = SPControl.GetContextWeb(Context); 
     SPUser theUser = theSite.CurrentUser; 
     string strUserName = theUser.LoginName; 
     var theEmplAssignment = (
            from p 
            in db.EMPLOYEES 
            where p.username == strUserName 
            select p).FirstOrDefault(); 

     _emp = theEmplAssignment; 

     if (_emp != null) 
     { 
      //Create assignment 
      var myAssignment = new EMPLOYEES_ASSIGNMENT 
      { 
       assignment_id = new Random().Next(), 
       employee_id = _emp.employee_id, 

       reference_name = TextBoxReference.Text, 
       company_name = TextBoxCompanyName.Text, 
       sector = TextBoxSector.Text, 
       area = TextBoxArea.Text, 
       from_date = TextBoxFromDate.Text, 
       to_date = TextBoxToDate.Text, 
       description = TextBoxDesc.Text, 
      }; 

      //Create assignment tools 
      for (int i = 0; i < ListBoxAssignmentTools.Items.Count; i++) 
      { 
       var myTool = new ASSIGNMENT_TOOLS 
       { 
        assignment_tools_id = new Random().Next(), 
        assignment_id = myAssignment.assignment_id, 
        employee_id = myAssignment.employee_id, 

        tool_name = ListBoxAssignmentTools.Items[i].ToString() 
       }; 

       myAssignment.ASSIGNMENT_TOOLS.Add(myTool); 
      } 

      //Create assignment technology 
      for (int i = 0; i < ListBoxAssignmentTechnology.Items.Count; i++) 
      { 
       var myTech = new ASSIGNMENT_TECHNOLOGY 
       { 
        assignment_technology_id = new Random().Next(), 
        assignment_id = myAssignment.assignment_id, 
        employee_id = myAssignment.employee_id, 

        technology_name = ListBoxAssignmentTechnology.Items[i].ToString() 
       }; 

       myAssignment.ASSIGNMENT_TECHNOLOGY.Add(myTech); 
      } 

      //Add assignment to db     
      _emp.EMPLOYEES_ASSIGNMENT.Add(myAssignment); 
      db.SaveChanges(); 

      //Populate gridview 
      var dt = new DataTable(); 
      if (Session["DataTable"] != null) 
      { 
       dt = (DataTable)Session["DataTable"]; 
      } 
      else 
      { 
       dt.Columns.Add("Company name"); 
       dt.Columns.Add("Sector"); 
       dt.Columns.Add("Area"); 
       dt.Columns.Add("From"); 
       dt.Columns.Add("To"); 
       dt.Columns.Add("Tools"); 
       dt.Columns.Add("Technology"); 
       dt.Columns.Add("Description"); 
       dt.Columns.Add("Reference"); 
       dt.Rows.Clear(); 
      } 

      DataRow dr = dt.NewRow(); 
      dr["Company name"] = TextBoxCompanyName.Text; 
      dr["Sector"] = TextBoxSector.Text; 
      dr["Area"] = TextBoxArea.Text; 
      dr["From"] = TextBoxFromDate.Text; 
      dr["To"] = TextBoxToDate.Text; 
      dr["Description"] = TextBoxDesc.Text; 
      dr["Reference"] = TextBoxReference.Text; 

      string sToolsValue = string.Empty; 
      for (int i = 0; i < ListBoxAssignmentTools.Items.Count; i++) 
      { 
       sToolsValue += ListBoxAssignmentTools.Items[i] + " "; 
      } 
      dr["Tools"] = sToolsValue; 

      string sTechValue = string.Empty; 
      for (int i = 0; i < ListBoxAssignmentTechnology.Items.Count; i++) 
      { 
       sTechValue += ListBoxAssignmentTechnology.Items[i] + " "; 
      } 
      dr["Technology"] = sTechValue; 

      dt.Rows.Add(dr); 
      Session["DataTable"] = dt; 

      //Add to gridview 
      GridViewShowAssignments.DataSource = dt; 
      GridViewShowAssignments.DataBind(); 

      TextBoxCompanyName.Text = string.Empty; 
      TextBoxArea.Text = string.Empty; 
      TextBoxSector.Text = string.Empty; 
      TextBoxFromDate.Text = string.Empty; 
      TextBoxToDate.Text = string.Empty; 
      TextBoxDesc.Text = string.Empty; 
      TextBoxReference.Text = string.Empty; 
      ListBoxAssignmentTools.Items.Clear(); 
      ListBoxAssignmentTechnology.Items.Clear(); 
     }     
    } 
} 

而且我使用的頁面加載,檢索的方法在當前登錄用戶上的所有分配並在gridview上顯示它。 但是,gridview填充重複!我懷疑我必須在頁面加載時清除會話,但我不知道如何去做。 enter image description here

的方法的代碼:

//Get assignment from db and populate gridview 
private void GetEmployeeAssignment(EMPLOYEE theEmpl) 
{ 
    Session.Remove("DataTable"); 

    using (var db = new KnowItCvdbEntities()) 
    { 
     if (_emp != null) 
     { 
      var assignmentList = from p in db.EMPLOYEES_ASSIGNMENT.AsEnumerable() 
           join at in db.ASSIGNMENT_TOOLS.AsEnumerable() on p.assignment_id equals at.assignment_id 
           join ate in db.ASSIGNMENT_TECHNOLOGY.AsEnumerable() on p.assignment_id equals ate.assignment_id 
           where p.employee_id == theEmpl.employee_id 
           select new EmployeeAssignmentInfo 
           { 
            CompanyName = p.company_name, 
            AssignmentId = p.assignment_id, 
            Area = p.area, 
            From = p.from_date, 
            To = p.to_date, 
            Description = p.description, 
            Sector = p.sector, 
            Reference = p.reference_name, 

            ToolName = at.tool_name, 
            AssignmentToolsId = at.assignment_tools_id, 
            TechnologyName = ate.technology_name, 
            AssignmentTechnologyId = ate.assignment_technology_id 
           }; 

      foreach (var vAssignment in assignmentList) 
      { 
       //Populate gridview 
       var dt = new DataTable(); 
       if (Session["DataTable"] != null) 
       { 
        dt = (DataTable)Session["DataTable"]; 
       } 
       else 
       { 
        dt.Columns.Add("Company name"); 
        dt.Columns.Add("Sector"); 
        dt.Columns.Add("Area"); 
        dt.Columns.Add("From"); 
        dt.Columns.Add("To"); 
        dt.Columns.Add("Tools"); 
        dt.Columns.Add("Technology"); 
        dt.Columns.Add("Description"); 
        dt.Columns.Add("Reference"); 
        dt.Rows.Clear(); 
       } 

       DataRow dr = dt.NewRow(); 
       dr["Company name"] = vAssignment.CompanyName; 
       dr["Sector"] = vAssignment.Sector; 
       dr["Area"] = vAssignment.Area; 
       dr["From"] = vAssignment.From; 
       dr["To"] = vAssignment.To; 
       dr["Description"] = vAssignment.Description; 
       dr["Reference"] = vAssignment.Reference; 
       dr["Tools"] = vAssignment.ToolName + " "; 
       dr["Technology"] = vAssignment.TechnologyName + " "; 

       dt.Rows.Add(dr); 
       Session["DataTable"] = dt; 

       GridViewShowAssignments.DataSource = dt; 
       GridViewShowAssignments.DataBind(); 
      } 
     } 
     else 
     { 
      LabelPleaseRegister.Visible = true; 
      LabelPleaseRegister.Text = "Please register your personal information"; 
      PanelRegisterCv.Visible = false; 
      PanelRegisterPersonalInfo.Visible = false; 
     } 
    } 
} 

頁負載:

 protected void Page_Load(object sender, EventArgs e) 
    { 
     SPWeb theSite = SPControl.GetContextWeb(Context); 
     SPUser theUser = theSite.CurrentUser; 
     string strUserName = theUser.LoginName; 

     LabelUsername.Text = strUserName; 

     if (!IsPostBack) 
     { 
      _emp = GetEmployee(strUserName);     

      GetEmployeeAssignment(_emp); 
     } 
    } 

回答

1

只要做

Session["DataTable"] = null; 

Session.Remove("DataTable") 

更新你的代碼,如:

//Get assignment from db and populate gridview 
private void GetEmployeeAssignment(EMPLOYEE theEmpl) 
    { 
Session["DataTable"]=null; 

using (var db = new KnowItCvdbEntities()) 
{ 
    if (_emp != null) 
    { 
     var assignmentList = from p in db.EMPLOYEES_ASSIGNMENT.AsEnumerable() 
          join at in db.ASSIGNMENT_TOOLS.AsEnumerable() on p.assignment_id equals at.assignment_id 
          join ate in db.ASSIGNMENT_TECHNOLOGY.AsEnumerable() on p.assignment_id equals ate.assignment_id 
          where p.employee_id == theEmpl.employee_id 
          select new EmployeeAssignmentInfo 
          { 
           CompanyName = p.company_name, 
           AssignmentId = p.assignment_id, 
           Area = p.area, 
           From = p.from_date, 
           To = p.to_date, 
           Description = p.description, 
           Sector = p.sector, 
           Reference = p.reference_name, 

           ToolName = at.tool_name, 
           AssignmentToolsId = at.assignment_tools_id, 
           TechnologyName = ate.technology_name, 
           AssignmentTechnologyId = ate.assignment_technology_id 
          }; 
     var dt = new DataTable(); 

     foreach (var vAssignment in assignmentList) 
     { 
      //Populate gridview 

      if (Session["DataTable"] != null) 
      { 
       dt = (DataTable)Session["DataTable"]; 
      } 
      else 
      { 
       dt.Columns.Add("Company name"); 
       dt.Columns.Add("Sector"); 
       dt.Columns.Add("Area"); 
       dt.Columns.Add("From"); 
       dt.Columns.Add("To"); 
       dt.Columns.Add("Tools"); 
       dt.Columns.Add("Technology"); 
       dt.Columns.Add("Description"); 
       dt.Columns.Add("Reference"); 
       dt.Rows.Clear(); 
      } 

      DataRow dr = dt.NewRow(); 
      dr["Company name"] = vAssignment.CompanyName; 
      dr["Sector"] = vAssignment.Sector; 
      dr["Area"] = vAssignment.Area; 
      dr["From"] = vAssignment.From; 
      dr["To"] = vAssignment.To; 
      dr["Description"] = vAssignment.Description; 
      dr["Reference"] = vAssignment.Reference; 
      dr["Tools"] = vAssignment.ToolName + " "; 
      dr["Technology"] = vAssignment.TechnologyName + " "; 

      dt.Rows.Add(dr); 

     } 

      Session["DataTable"] = dt; 

      GridViewShowAssignments.DataSource = dt; 
      GridViewShowAssignments.DataBind(); 


    } 
    else 
    { 
     LabelPleaseRegister.Visible = true; 
     LabelPleaseRegister.Text = "Please register your personal information"; 
     PanelRegisterCv.Visible = false; 
     PanelRegisterPersonalInfo.Visible = false; 
    } 
} 

}

問候