实施会话时出现问题.
问题描述:
我想采用会话形式的checkout.aspx页的值来确认.aspx页.
但我遇到了错误
i want to take the values form checkout.aspx page to confirm.aspx page.. using sessions.
but i am getting error
Object Reference not set to Instance of an object Exception
在checkout.aspx页上.我将代码写为
on checkout.aspx page. i wrote the code as
// Checkout.aspx
Order o = new Order();
o.CustomerID = User.Identity.Name;
Session["CustomerID"] = o.CustomerID;
o.OrderDate = DateTime.Now;
o.FirstName = firstnametxt.Text;
Session["FirstName"] = o.FirstName;
o.LastName = lastnametxt.Text;
Session["LastName"] = o.LastName;
o.Company = companytxt.Text;
Session["Company"] = o.Company;
o.Address = addresstxt.Text;
Session["Address"] = o.Address;
o.Country = dpcountry.Text;
Session["Country"] = o.Country;
o.City = citytxt.Text;
Session["City"] = o.City;
o.Province = provincetxt.Text;
Session["Province"] = o.Province;
o.ZipCode = zipcodetxt.Text;
Session["ZipCode"] = o.ZipCode;
o.Telephone = telephonetxt.Text;
Session["Telephone"] = o.Telephone;
o.Fax = faxtxt.Text;
Session["Fax"] = o.Fax;
在Confirm.aspx页面加载中,我编写了代码.但无法在标签字段中获取会话的值
On Confirm.aspx page load i wrote the code. but not able to get value of session in label fieldss
protected void Page_Load(object sender, EventArgs e)
{
lblfirstname.Text = Session["FirstName"].ToString();
lbllastname.Text = Session["LastName"].ToString();
lbladdress.Text = Session["Address"].ToString();
lblphoneno.Text = Session["Telephone"].ToString();
lblprovince.Text = Session["Province"].ToString();
lblcity.Text = Session["City"].ToString();
lblpostalcode.Text = Session["ZipCode"].ToString();
bind();
}
答
可能的一种可能是Confirm.aspx的Page_Load ()
中的代码在.确保在 Conform.aspx
的Page_Load()
之前触发checkout.aspx事件并执行该事件中的代码.
此外,优良作法是初始化订单对象的所有必需属性并立即设置会话对象.
希望这会对您有所帮助.
one of the possibilities is may be the code withinPage_Load ()
of Confirm.aspx is getting executed before the piece of code incheckout.aspx
. ensure that checkout.aspx event gets fired and code within the event gets executed beforeConform.aspx
''sPage_Load()
.
Moreover, it will be good practice to initialize all the required properties of order object and set the session object at once.
Hope this will help you.
调试代码,并查看您在 checkout.aspx 中的会话变量中得到了什么.
您收到此错误的原因仅仅是会话变量不包含对内存中任何对象的引用.如果您尝试在某个对象上调用.ToString()
,而您的变量为 null ,则会收到此异常.
希望对您有所帮助:)
Debug the code and see what you are getting in session variables in you checkout.aspx.
You are getting this error just because session variables doesn’t contain a reference to any object in memory. If you try to call.ToString()
on an object, and your variable is null, you’ll get this exception.
hope it helps :)
您需要通过Web配置文件配置会话状态,如 ^ ].
You need to configure session state via the web config file as shown here[^].