Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Server Error in '/WebSite3' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 23: {
Line 24: FillddlMainMenu();
Line 25: choice = Session["Choice"].ToString();
Line 26: if (choice == "Main")
Line 27: {

Source File: c:\Users\Surya\Documents\Visual Studio 2010\WebSites\WebSite3\menu\CreateMenu.aspx.cs Line: 25

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
menu_CreateMenu.Page_Load(Object sender, EventArgs e) in c:\Users\Surya\Documents\Visual Studio 2010\WebSites\WebSite3\menu\CreateMenu.aspx.cs:25
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +91
System.Web.UI.Control.LoadRecursive() +74
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207



please anyone help me to clear this error?
Posted

In your code your calling the session varriable.

C#
choice = Session["Choice"].ToString();


please be check it once your session having data or not. I think your session returns null value that's way it's showing this error "Object reference not set to an instance of an object".

error details

Asp.net-Object reference not set to an instance of an object[^]

session varriable

Exploring Session in ASP.NET[^]
 
Share this answer
 
Comments
v surya dev 11-Apr-13 11:55am    
thank u i cleard that error... but now i want to upload my webform using dynamic created datalist menus. can anyone send the code pls...
Check your Session variables before you use them!
It is reporting a problem with line 25:
C#
Line 25: choice = Session["Choice"].ToString();
The only part of this that can cause a Null reference exception is if Session["Choice"] returns null before you call ToString on it.

Somewhere, you have not set the Session correctly, and you haven;t checked to ensure that it isn't null before you use it.
 
Share this answer
 
Comments
v surya dev 11-Apr-13 3:54am    
s sir, but i am new to session creation, please help me how to clear this..?

this is my code...

my web form code....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class menu_CreateMenu : System.Web.UI.Page
{
public string connection = ConfigurationManager.ConnectionStrings["aspnetdbConnectionString"].ToString();
string choice = "0";

protected void Page_Load(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=aspnetdb;User ID=sa;Password=voyage");

if (!IsPostBack)
{
FillddlMainMenu();
choice = Session["Choice"].ToString();
if (choice == "Main")
{
pnlMainMenu.Visible = true;
}
else if (choice == "Sub")
{
pnlSubMenu.Visible = true;
}

}
else
{

}


}
protected void btnMainmenu_Click(object sender, EventArgs e)
{
int active;
if (chkMActive.Checked)
{
active = 1;
}
else
{
active = 0;
}
SqlConnection conn = new SqlConnection(connection);
string sql = "INSERT INTO MainMenu(MainMenu, MLocation, MenuOrder, IsActive)VALUES('" + txtMainmenu.Text + "','" + txtMLocation.Text + "'," + Convert.ToInt16(ddlMOrder.SelectedItem.Value) + "," + active + ")";
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
conn.Close();
FillddlMainMenu();
Response.Redirect("SuccessMsg.aspx");
}

public void FillddlMainMenu()
{
SqlConnection conn = new SqlConnection(connection);
DataSet DS = new DataSet();
SqlDataAdapter dataadapter = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("Select MainMenuId,MainMenu from MainMenu", conn);

//cmd.CommandType = CommandType.Text;
dataadapter.SelectCommand = cmd;
dataadapter.Fill(DS);
ddlMainMenu.DataSource = DS;
ddlMainMenu.DataBind();




}
protected void btnSubmenu_Click(object sender, EventArgs e)
{
int active;
if (chkSActive.Checked)
{
active = 1;
}
else
{
active = 0;
}
SqlConnection conn = new SqlConnection(connection);
string sql = "INSERT INTO SubMenu(SubMenu, SubMenuLocation, MenuOrder,ParentId, IsActive)VALUES('" + txtSubmenu.Text + "','" + txtSubMenuLocation.Text + "'," + Convert.ToInt16(ddlSOrder.SelectedItem.Value) + "," + Convert.ToInt16(ddlMainMenu.SelectedItem.Value) + "," + active + ")";
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
conn.Close();
Response.Redirect("SuccessMsg.aspx");
}
}



master page code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class menu_MasterPage : System.Web.UI.MasterPage
{
static DataSet static_dataset = new DataSet();

DataTable dt = new DataTable();
public string connection = ConfigurationManager.ConnectionStrings["aspnetdbConnectionString"].ToString();

protected void Page_Load(object sender, EventArgs e)
{
//if (!IsPostBack)
//{
static_dataset = ds();
dt = static_dataset.Tables[0].Copy();
dt.Clear();
OriginalGriff 11-Apr-13 3:59am    
None of that code sets the Session value at all - it just reads it out.
Since I have no idea when you should be setting it, or what value to expect to set it to, I think you will have to think about it for yourself! It may be you need it in the empty "else" block below where you use it...
Hint:
The code you need will be similar to:
Session["Choice"] = "a string of text";
Either that or you don't need to use the Session at all, but again, since I don't know what you are doing, that is up to you, really!
v surya dev 11-Apr-13 3:55am    
can any one help me..?
v surya dev 11-Apr-13 4:04am    
anyway thank u boss...
v surya dev 11-Apr-13 4:07am    
i tried to add menus and submenus at runtime.... this is what i tried...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900