我如何清除会话?

时间:2013-04-03 09:38:32

标签: c# sql-server

我正在使用实体框架,现在我正在保存两个作业,并希望将它们展示给我的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 个答案:

答案 0 :(得分: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;
    }
}

}

最好的问候