这段代码有什么问题?为什么会话在页面加载事件时未正确检查 [英] what is wrong in this code ? why session is not checking properly at page load event
本文介绍了这段代码有什么问题?为什么会话在页面加载事件时未正确检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我总是遇到错误
对象引用未设置为对象的实例
在检查(Session ["DETAILS"].ToString()=="NEW")时是否
我的源代码是:
I am always getting error
object reference not set to the instance of object
when checking if (Session["DETAILS"].ToString() == "NEW")
My source code is :
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class SCDetails : System.Web.UI.Page
{
Properties p = new Properties();
Functions Fun = new Functions();
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (IsPostBack == false)
{
FillDD();
if (Session["DETAILS"].ToString() == "NEW")
{
Label1.Text = "NEW SUB CATEGORY DETAILS [ADD]";
DropDownList1.Focus();
}
else if (Session["DETAILS"].ToString() == "EDIT")
{
if (Session["SCID"].ToString() != "")
{
Label1.Text = "EDIT SUB CATEGORY DETAILS [EDIT]";
Label12.Text = Session["SCID"].ToString();
Session["SCID"] = "";
GetDetails();
DropDownList1.Focus();
}
}
}
}
catch
{
}
}
protected void Button2_Click(object sender, EventArgs e)
{
if (Label12.Text == "")
{
Boolean cn;
cn = Fun.ChkvalueExist("SELECT * FROM CMS_TB_MASTER_SUBCATEGORY WHERE CNAME='" + DropDownList1.SelectedItem.Text + "' AND SCNAME='" + TextBox1.Text + "'");
if (cn == false)
{
p.CNAME = DropDownList1.SelectedItem.Text;
p.SGNAME = TextBox1.Text;
p.DESC = TextBox2.Text;
string sql = "INSERT INTO CMS_TB_MASTER_SUBCATEGORY VALUES('" + p.CNAME + "','" + p.SGNAME + "','" + p.DESC + "')";
Fun.Insert(sql);
//fun.INFYDetails(p);
Clear();
Response.Write("<script language='javascript'>window.opener.document.getElementById('Button4').click();window.close();window.location='SubCategory.aspx';</script>");
}
else
{
Response.Write("<script language='javascript'>window.alert('This sub category name already exists !');</script>");
}
}
else
{
Boolean cn, cn1;
cn = Fun.ChkvalueExist("SELECT * FROM CMS_TB_MASTER_SUBCATEGORY WHERE SCID='" + Label12.Text + "' AND CNAME='" + DropDownList1.SelectedItem.Text + "' AND SCNAME='" + TextBox1.Text + "'");
cn1 = Fun.ChkvalueExist("SELECT * FROM CMS_TB_MASTER_SUBCATEGORY WHERE SCID!='" + Label12.Text + "' AND CNAME='" + DropDownList1.SelectedItem.Text + "' AND SCNAME='" + TextBox1.Text + "'");
if ((cn == true && cn1 == false) || (cn == false && cn1 == false))
{
p.CNAME = DropDownList1.SelectedItem.Text;
p.SGNAME = TextBox1.Text;
p.DESC = TextBox2.Text;
string sql = "UPDATE CMS_TB_MASTER_SUBCATEGORY SET CNAME='" + p.CNAME + "',SCNAME='" + p.SGNAME + "', DESCP='" + p.DESC + "' WHERE SCID='" + Label12.Text + "'";
Fun.Insert(sql);
//fun.UDFYDetails(p);
Clear();
//Response.Write("<script language='javascript'>window.opener.document.getElementById('Button4').click();window.close();window.location='SubCategory.aspx';</script>");
Response.Redirect("SubCategory.aspx");
GetDetails();
}
else
{
Response.Write("<script language='javascript'>window.alert('This sub category name already exists !');</script>");
}
}
}
#region Functions
public void FillDD()
{
string sql = "SELECT CNAME,CID FROM CMS_TB_MASTER_CATEGORY ORDER BY CNAME";
Fun.BDD_WITH_ID(DropDownList1, sql, "--SELECT--", "CID", "CNAME");
}
public void GetDetails()
{
DataSet DS = new DataSet();
string sql = "SELECT * FROM CMS_TB_MASTER_SUBCATEGORY WHERE SCID='" + Label12.Text + "'";
DS = Fun.DBSelect(sql);
if (DS.Tables[0].Rows.Count > 0)
{
FillDD();
DropDownList1.SelectedValue = DS.Tables[0].Rows[0]["CNAME"].ToString();
TextBox1.Text = DS.Tables[0].Rows[0]["SCNAME"].ToString();
TextBox2.Text = DS.Tables[0].Rows[0]["DESCP"].ToString();
}
else
{
Clear();
Button2.Enabled = false;
}
}
public void Clear()
{
DropDownList1.SelectedIndex = 0;
TextBox1.Text = "";
TextBox2.Text = "";
}
#endregion
protected void Button1_Click1(object sender, EventArgs e)
{
if (Label12.Text == "")
{
Clear();
}
else
{
Response.Redirect("SubCategory.aspx");
}
}
}
推荐答案
在使用任何会话信息之前,应该存在null检查.
Before using any session information, The null check should be there.
if(Session["DETAILS"]!=null)
{
//Code goes here.
}
使用前,请务必检查会话,否则会给出NullReferanceException
.
试试这个:
You should always check your sessions before using it, otherwise it''ll giveNullReferanceException
.
Try this:
if (IsPostBack == false)
{
FillDD();
if(Session["DETAILS"] != null){
if (Session["DETAILS"].ToString() == "NEW")
{
Label1.Text = "NEW SUB CATEGORY DETAILS [ADD]";
DropDownList1.Focus();
}
else if (Session["DETAILS"].ToString() == "EDIT")
{
if (Session["SCID"].ToString() != "")
{
Label1.Text = "EDIT SUB CATEGORY DETAILS [EDIT]";
Label12.Text = Session["SCID"].ToString();
Session["SCID"] = "";
GetDetails();
DropDownList1.Focus();
}
}
}
}
--Amit
--Amit
在您的全局aspx页面中设置会话变量,然后在页面加载事件中使用它.您尚未初始化它...
set session variable in ur global aspx page before using it in page load event. u haven''t initialized it ...
这篇关于这段代码有什么问题?为什么会话在页面加载事件时未正确检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文