ashx中应用HttpContext.Current.Session ,呈现未将对象引用设置到实例上

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq719365064/article/details/52925159

解决办法:


1.引用using System.Web.SessionState;


2.在aspx类实现IReadOnlySessionState接口。如:public class {ClassName}: IHttpHandler, IReadOnlySessionState{…}


原因:


“IReadOnlySessionState”,指定目标HTTP处理惩罚法度只须要具有对话接见状况值的读接见权限。


实际上是ashx文件起首是实现了IHttpHandler接口。但要接见page中的session状况值时,是两个应用范畴。


微软默认状况下没有接见权限,所以ashx文件接见不了HttpContext.Current.Session,故呈现未将对象设置到实例上。


只须要在实现IReadOnlySessionState接口即可。


转自:http://www.byywee.com/page/M0/S906/906631.html

展开阅读全文

未将对象引用设置实例

11-16

public static void GridViewToExcel(GridView gv, string excelName)rn rn HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");rn HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + excelName + ".xls");rnrn //关闭 ViewStatern //this.EnableViewState = false;rn System.IO.StringWriter tw = new System.IO.StringWriter();rnrn System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw);rn //获取control的HTMLrn gv.RenderControl(hw);rn //把HTML写回浏览器rn HttpContext.Current.Response.Write(tw.ToString());rn HttpContext.Current.Response.End(); rn tw.Close();rn hw.Close();rn rn public override void VerifyRenderingInServerForm(Control control)rn rn //方法重写rn rnprotected void btnExcel_Click(object sender, EventArgs e)rn rn string[] excelCustName = new string[dtgOdOrderlt.Columns.Count];rn for (int i = 0; i < dtgOdOrderlt.Columns.Count; i++)rn rn view_ALLodOrder col = new view_ALLodOrder();rn if (!string.IsNullOrEmpty(col.CustName.ToString()))rn rn string headText = col.CustName.ToString();rn string names = dtgOdOrderlt.Columns[i].ToString();rn names = headText;rn int charIndex = dtgOdOrderlt.Columns[i].ToString().IndexOf('/');rn if (charIndex > -1)rn rn excelCustName[i] = headText.Substring(0, charIndex);rn rn elsern rn excelCustName[i] = headText;rn rn rn elsern rn excelCustName[i] = null;rn rn rn GridViewToExcel(dtgOdOrderlt, "OrderLook");rn rn[color=#FF0000] 报错:如下[/color]“/AlbumFoundation”应用程序中的服务器错误。rn--------------------------------------------------------------------------------rnrn未将对象引用设置到对象的实例。 rn说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 rnrn异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。rnrn源错误: rnrnrn行 878: getrn行 879: rn行 880: return base.Getstring(ColumnNames.CustName);rn行 881: rn行 882: setrn rnrn源文件: d:\Projects\AlbumFoundation.root\AlbumFoundation\WebSites\AlbumFoundation\App_Code\Concrete\view_ALLodOrder.cs 行: 880 rnrn堆栈跟踪: rnrnrn[NullReferenceException: 未将对象引用设置到对象的实例。]rn MyGeneration.dOOdads.BusinessEntity.Getstring(String columnName) +72rn Watabe.Shanghai.Albumfoundation.view_ALLodOrder.get_CustName() in d:\Projects\AlbumFoundation.root\AlbumFoundation\WebSites\AlbumFoundation\App_Code\Concrete\view_ALLodOrder.cs:880rn orders_ODOrderListtest.btnExcel_Click(Object sender, EventArgs e) in d:\Projects\AlbumFoundation.root\AlbumFoundation\WebSites\AlbumFoundation\orders\ODOrderListtest.aspx.cs:448rn System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111rn System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110rn System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10rn System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13rn System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36rn System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565rnrn rn 论坛

"未将对象引用设置到对象的实例。"?

11-30

想点击gridview1中的newstitle时跳到newscontents.aspx中显示新闻信息rnrn[color=#0000FF]newscontents.aspx.cs[/color]rnpublic partial class newscontents : System.Web.UI.Pagernrn protected void Page_Load(object sender, EventArgs e)rn rn SqlConnection objconn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["sql_connstr"].ToString());rn objconn.Open();rnrn SqlCommand objcmd = new SqlCommand("NewsDetail",objconn);rn objcmd.CommandType = CommandType.StoredProcedure; rnrn SqlParameter paramNewsID = new SqlParameter("@NewsID",SqlDbType.Int,4);rn [color=#FF0000] paramNewsID.Value = Convert.ToInt32(Request.QueryString["NewsID"].ToString());[/color]出错rn objcmd.Parameters.Add(paramNewsID);rn提示:[color=#FF0000]试图在代码中引用不存在的对象时,会发生 NullReferenceException。例如,您可能试图在未先使用 New 关键字的情况下使用对象,或试图使用值设置为 null(在 Visual Basic 中为 Nothing)的对象。[/color]rn SqlDataReader dr = objcmd.ExecuteReader();rn if (dr.Read())rn rn tbxtitle.Text = dr["NewsTitle"].ToString();rn tbxcontents.Text = dr["NewsContents"].ToString();rn tbxpublisher.Text = dr["NewsPublisher"].ToString();rn tbxdatetime.Text=dr["NewsDatetime"].ToString();rn rn elsern rn Response.Write("Sorry,没有该新闻信息!");rn Response.End();rn rn rnrn[color=#0000FF]news.aspx[/color]rnrn.....rnColumns>rn rn rn rn rn rn.....rnrn数据库中有newsid的值呀,怎么是空呢?如我说的不清楚可以再补充rn请大家帮个忙哈,先谢谢了!!!!!!! 论坛

没有更多推荐了,返回首页