日期:2014-05-17  浏览次数:20482 次

类库中使用session
[code=C#][/code]using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.SessionState;
using DtCms.BLL;
using DtCms.Model;
using System.Data;
using System.Web.Security; 

namespace BitmapCutter.Core
{
  public class UpdateUserImg : IRequiresSessionState
  {
  //HttpSessionState Session = HttpContext.Current.Session;
   
  public UpdateUserImg() 
  {
   
  }
  public bool InsertInto(string imgsrc)
  {
  try
  {
  //写入数据库操作
  DtCms.BLL.re_UserInfo bll = new DtCms.BLL.re_UserInfo();
  bll.UpdateField(HttpContext.Current.Session["UserEmail"].ToString(), " UserImgUrl='" + imgsrc + "'");
  DtCms.BLL.re_Resume rbll = new DtCms.BLL.re_Resume();
  int pcount = rbll.GetCount(" UserEmail='" + HttpContext.Current.Session["UserEmail"] + "'");
  if (pcount > 0)
  {
  DataSet ds = rbll.GetList(0, " UserEmail='" + HttpContext.Current.Session["UserEmail"].ToString() + "'", " AddTime asc");
  DataTable dt = ds.Tables[0];
  foreach (DataRow dr in dt.Rows)
  {
  int rid = int.Parse(dr["ID"].ToString());
  if (rid != 0)
  {
  rbll.UpdateField(rid, " ImgUrl='" + imgsrc + "'");
  }
  }
  }
  return true;
  }
  catch
  {
  return false;
  }
  }
  }
}
类库中使用session 百度 google了好多 都不管用 依然提示未将对象引用设置到对象的实例 求求各位大神 帮帮忙吧

------解决方案--------------------
学会断点 调试
------解决方案--------------------
草书
------解决方案--------------------
HttpContext.Current.Session["UserEmail"].ToString()是和这些有关系吧,和这HttpSessionState Session = HttpContext.Current.Session;没关系啊,估计是你的UserEmail不存在才报错的,把这个换成固定字符串试试

------解决方案--------------------
探讨
HttpContext.Current.Session["UserEmail"].ToString()是和这些有关系吧,和这HttpSessionState Session = HttpContext.Current.Session;没关系啊,估计是你的UserEmail不存在才报错的,把这个换成固定字符串试试

------解决方案--------------------
你需要先判断才能使用
if(HttpContext.Current.Session["UserEmail"]!=null)
{
 //你的代码
}
------解决方案--------------------
为什么不将Session作为参数传进来?

如果封装好了,你忘了Session的名字怎么办?或者你的Session不想用UserEmail这个名字了怎么办?或者接替你的开发人员根本不知道有Session["UserEmail"]这个东西怎么办。