Showing posts with label access. Show all posts
Showing posts with label access. Show all posts

Thursday, March 29, 2012

object reference a mystery

Hi

Im busy building a class where i want the class members to have access to and be able to manipulate items on a web page.
Surely if i declare an instance of my page class (MyPage_Default) in this new class then i should have access to its members?

Could somebody please have a look at my code and tell me what i could be doing wrong?
thanks

Here is the code: (first my new class and then code for my page's code behind)

public class wizard

{

public int myNumber;

public void SetMyNumber(int newNumber)

{

myNumber = newNumber;

}

public void work()

{

MyPage_Default i = new MyPage_Default();

i.label("hello");

}

}

public partial class MyPage_Default : System.Web.UI.Page

{

wizard w = new wizard();

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

w.SetMyNumber(10);

}

w.work();

}

protected void btnNext_Click(object sender, EventArgs e)

{

w.SetMyNumber(30);

w.work();

}

public void PageDo()

{

Label1.Text = w.myNumber.ToString();

}

public void label(string newValue)

{

Label1.Text = newValue;

}

}

Hi,

there are several solutions to this. 2 that pop up in my mind are these:

1<%@. Page Language="C#" %>23<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">45<script runat="server">67 protected void Page_Load(object sender, EventArgs e)8 {9 ContextTest.SetNumber(13);10 ContextTest.SetNumberTry2(Label2, 8);11 }1213</script>1415<html xmlns="http://www.w3.org/1999/xhtml">16<head runat="server">17 <title>Untitled Page</title>18</head>19<body>20 <form id="form1" runat="server">21 <div>22 <asp:Label ID="Label1" runat="server"></asp:Label>23 <asp:Label ID="Label2" runat="server"></asp:Label>24 </div>25 </form>26</body>27</html>

and in the App_Code subfolder I put this class:

1using System;2using System.Data;3using System.Configuration;4using System.Web;5using System.Web.Security;6using System.Web.UI;7using System.Web.UI.WebControls;8using System.Web.UI.WebControls.WebParts;9using System.Web.UI.HtmlControls;1011/// <summary>12/// Summary description for ContextTest13/// </summary>14public class ContextTest15{16public static void SetNumber(int number)17 {18 ((Label)((Page)HttpContext.Current.CurrentHandler).FindControl("Label1")).Text = number.ToString();19 }2021public static void SetNumberTry2(Label control,int number)22 {23 control.Text = number.ToString();24 }25}

As you can see, you don't need to instantiate another object of the type of your particular class. In the SetNumber method I just pass an integer and in the method I get the CurrentHandler, which is the page, that runs in the current context. After casting you can use the FindControl method to find the Label1 and set its text.

In SetNumberTry2 however I just pass the Label2 control directly, which references the instance of the Label2 control, and set its Text property directly to the number.

Grz, Kris.


Hi Kris.

Thanks for your quick reply.


For some reason the first method doesn't work :( It gies me the same error. (Object reference...)
The second however does.

Once again thanks a mil! This will definitely be very useful in time to come
gem-code!


Hi,

Dewald:

For some reason the first method doesn't work :( It gies me the same error. (Object reference...)

can you provide the error you're getting? Did you put the class in the App_Code subfolder or not?

Grz, Kris.


Hi again!

Nah the code is def in the App_Code folder.

This is how i implement it:
//Show the current step. Assign to some label specified
public void ShowStep()
{
//control.Text = WizardCurStep.ToString();
((Label)((Page)HttpContext.Current.CurrentHandler).FindControl("Label1")).Text = WizardCurStep.ToString();
}

WizardCurStep is defined up in the same page as int

Here is the error im getting:

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 47: {Line 48: //control.Text = WizardCurStep.ToString();Line 49: ((Label)((Page)HttpContext.Current.CurrentHandler).FindControl("Label1")).Text = WizardCurStep.ToString();Line 50: }Line 51:


Dewald:

WizardCurStep is defined up in the same page as int

In the page but you don't pass it as an input parameter to your method in the custom class.

Grz, Kris.


Hi.

I'm trying to seperate the values used by the wizard class from the actual page that uses the wizard. Therefore ive declare several integers in the seperate class so that it can keep track of those values itself.
(Instead of having to assign values to viewstate or some label etc)

public class wizard
{
private int WizardMinStep;
private int WizardMaxStep;
private int WizardCurStep;

// Class Contructor
public wizard(int MinStep, int MaxStep)
{
WizardMinStep = MinStep;
WizardMaxStep = MaxStep;
}

//Step Forward
public void StepForward(Button control)
{
if (WizardCurStep >= WizardMaxStep)
{
control.Enabled = false;
}
else
{
WizardCurStep++;
control.Enabled = true;
}

//Show the current step. Assign to some label specified
public void ShowStep(Label control)
{
control.Text = WizardCurStep.ToString();
}

}

I wont be able to pass these values to the methods from the page without the page 'knowning' what they are.
Surely this is the same as you suggested?


Ok so ive given this a serious go now.
Actually my logic seem to work just fine. The code does increment the value set at the top of the page when i click my forward button and the "StepForward()" method is called. Only one problem now...
When i click the forward button for the second time... it doesn't increment that value again to 3. It just stays there on 2. (I pass to the class constructor the innitial value to 1)
Since VS doesn't want to break into debugger so i can step through the code i have to assume that the variable is stuck in some way.

I use the word 'stuck' because the value isn't incremeted beyond 2, however once i call the "StepPrevious()" method the value is decremented back to 1.
So it isn't a case of either loosing the value or it being set back to the innitial value of 1 with every server round trip. (If it was reset during server round trip the value would have been 0)

Kris would you know why this is? I know how to fix this problem using ViewState however that is not my prefered way of going about this.

Anyways i know this is off the subject i posted under.
Your advice so far will help me quite a bit in the future.

Thanks again!!


Ok so I’ve given this a serious go now.
Actually my logic seem to work just fine. The code does increment the value set at the top of the page when i click my forward button and the "StepForward()" method is called. Only one problem now...
When i click the forward button for the second time... it doesn't increment that value again to 3. It just stays there on 2. (I pass to the class constructor the initial value to 1)
Since VS doesn't want to break into debugger so i can step through the code i have to assume that the variable is stuck in some way.

I use the word 'stuck' because the value isn't incremented beyond 2, however once i call the "StepPrevious()" method the value is decremented back to 1.
So it isn't a case of either loosing the value or it being set back to the initial value of 1 with every server round trip. (If it was reset during server round trip the value would have been 0)

Kris would you know why this is? I know how to fix this problem using ViewState however that is not my preferred way of going about this.

Anyways I know this is off the subject i posted under.
Your advice so far will help me quite a bit in the future.

Thanks again!!

Object reference error

I have a datagrid and each column contains a different controls. When Iam trying to access the value of the ListBox controls inside thedatagrid it keeps on throwing "Object reference not set to an instanceof an object". The line is bold where it throws the error.
Here is my code:
private void Button1_Click(object sender, System.EventArgs e)
{
StringBuilder str = new StringBuilder();

foreach(DataGridItem dgi in myDataGrid.Items)
{
TextBox myTextBox = (TextBox)(dgi.Cells[0].Controls[1]);
ListBox myListBox = (ListBox)(dgi.Cells[1].Controls[1]);
DropDownList myList = (DropDownList)(dgi.Cells[2].Controls[1]);
CheckBox myCheckBox = (CheckBox)(dgi.Cells[3].Controls[1]);
str.Append(myTextBox.Text);
if(myListBox.SelectedItem.Value != null)
{
str.Append(myListBox.SelectedItem.Text);
}


str.Append(myList.SelectedItem.Text);
str.Append(myCheckBox.Checked);


}

Probably my eyes but there's no line in bold.

If you debug and check the type of the controls that you're using, are they of the correct type? I know I had some trouble in the past that sometimes I had to take Controls[0] and sometimes Controls[1] to obtain the correct control.

Grz, Kris.


Yeah I tried using Controls[0] but it did not worked and gave me that "Specific cast is not valid" error. The bold line below causes "Object reference not set to an instance of an object" This error is only thrown when I dont select any item in the ListBox.

foreach(DataGridItem dgiin myDataGrid.Items)

{

TextBox myTextBox = (TextBox) (dgi.Cells[0].Controls[1]);

ListBox myListBox = (ListBox) (dgi.Cells[1].Controls[1]);

DropDownList myList = (DropDownList) (dgi.Cells[2].Controls[1]);

CheckBox myCheckBox = (CheckBox) (dgi.Cells[3].Controls[1]);

str.Append(myTextBox.Text);

if(myListBox.SelectedItem.Value !=null)

{

str.Append(myListBox.SelectedItem.Text);

}

str.Append(myList.SelectedItem.Text);

str.Append(myCheckBox.Checked);

}


Okay I got it. I had to get the value from the ListBox into a string variable and than compare the string to null and empty.
The following code works:
foreach(DataGridItem dgi in myDataGrid.Items)
{
TextBox myTextBox = (TextBox)(dgi.Cells[0].Controls[1]);
ListBox myListBox = (ListBox)(dgi.Cells[1].Controls[1]);
DropDownList myList = (DropDownList)(dgi.Cells[2].Controls[1]);
CheckBox myCheckBox = (CheckBox)(dgi.Cells[3].Controls[1]);
str.Append(myTextBox.Text);
string a = myListBox.SelectedValue;
//string b = myListBox.SelectedItem.Value;
//string c = myListBox.SelectedItem.Text;
if(a != null && a != "")
{
str.Append(myListBox.SelectedItem.Text);
}


str.Append(myList.SelectedItem.Text);
str.Append(myCheckBox.Checked);


}

Monday, March 26, 2012

Object reference lost?

Dear all:

I develop a web application in VS2005. In order to reuse data access functionality and domain knowledge in other projects, I seperate data access functionality and domain knowledge into two different liraries name DataFarm and EPMLibrary.

DataFarm is used to deal with all database access jobs. And EPMLibrary is used to take care all project management logics and entities.

In order to hide data access details into EPMLibrary, I define a class in EPMLibrary like this

public class Configuration
{
[ThreadStatic]
public static DataFarm myFarm;

static Configuration()
{
myFarm = new DataFarm();
}
}

When I use my EPMLibrary in my web project, I add database connection in Application_Start and it works fine until now. But when I use some of my EPMLibrary objects, a null reference exception occurs usually but not regularly when the object needs operate with database.

Could any one teach me how to solve this problem and why it happens?

Great idea, however I have a few questions:

When you use the database you need to open a connection, use it and return it to the pool, each time you open a connection you create an object in the DataFarm(), without seen your code, why are you using static and [ThreadStatic]? Are you calling it from one place or many places in the app?

Can you post your code to check what are you doing in the connection and how you keep the connection?

Cheers

Al


First:

Beacuse the code is too long. I explain my design here.

The DataFarm class contains and maintains a collection of database connections, and it provides a set of execution commands for developers to do database operations.

Int32 DoCommand(Command cmd);
Int32 DoCommand(Command cmd, String connectionName);
Int32 DoCommand(IEnumerable cmdSet);
Int32 DoCommand(IEnumerable cmdSet, String connectionName);
.....

I list part of my function signature here, there actually exist a lot of overloaded function for DoCommand, and a set of DoSelect. The type "Command" is defined by myself to provide equivalent functionality with SqlCommand and OleDbCommand, and can be extended to support other database type.

The open, close and transaction are maintained in these functions.


Second:

Beacuse I want that all users use the same copy of DataFarm instance, so I use [ThreadStatic] to ensure there is only one copy in my web application.


You said:

Beacuse I want that all users use the same copy of DataFarm instance, so I use [ThreadStatic] to ensure there is only one copy in my web application.

Then if that's the function that opens a connectionl, the problem will be the each user override each other, please remove the static part and give it a try.

Hope this helps

Al

Saturday, March 24, 2012

Object reference not set to an instance of an object

Hi,

I want to access in code-behind a label within the ItemTemplate of a
Formview.

<asp:FormView ID="FormView1" runat="server" DataKeyNames="id"
DataSourceID="SqlDataSource1" >
<ItemTemplate>
<asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name")
%>'></asp:Label>

code-behind:
Dim name As String = CType(FormView1.FindControl("nameLabel"), Label).Text

I get the error: "Object reference not set to an instance of an object"

Could somebody tell me what's wrong in my code?
Thanks
BenTry

FormView1.Row.FindControl(...)

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Ben" <b@.bnwrote in message
news:%23obfXb8tHHA.3364@.TK2MSFTNGP02.phx.gbl...

Quote:

Originally Posted by

Hi,
>
I want to access in code-behind a label within the ItemTemplate of a
Formview.
>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="id"
DataSourceID="SqlDataSource1" >
<ItemTemplate>
<asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name")
%>'></asp:Label>
>
code-behind:
Dim name As String = CType(FormView1.FindControl("nameLabel"), Label).Text
>
>
I get the error: "Object reference not set to an instance of an object"
>
Could somebody tell me what's wrong in my code?
Thanks
Ben
>
>
>


On Jun 26, 2:06 pm, "Eliyahu Goldin"
<REMOVEALLCAPITALSeEgGoldD...@.mMvVpPsS.orgwrote:

Quote:

Originally Posted by

Try
>
FormView1.Row.FindControl(...)
>
--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net
>
"Ben" <b@.bnwrote in message
>
news:%23obfXb8tHHA.3364@.TK2MSFTNGP02.phx.gbl...
>

Quote:

Originally Posted by

Hi,


>

Quote:

Originally Posted by

I want to access in code-behind a label within the ItemTemplate of a
Formview.


>

Quote:

Originally Posted by

<asp:FormView ID="FormView1" runat="server" DataKeyNames="id"
DataSourceID="SqlDataSource1" >
<ItemTemplate>
<asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name")
%>'></asp:Label>


>

Quote:

Originally Posted by

code-behind:
Dim name As String = CType(FormView1.FindControl("nameLabel"), Label).Text


>

Quote:

Originally Posted by

I get the error: "Object reference not set to an instance of an object"


>

Quote:

Originally Posted by

Could somebody tell me what's wrong in my code?
Thanks
Ben


hi
,

if (FormView1.DefaultMode == FormViewMode.ReadOnly)
{
Label lbl = (Label) FormView1.FindControl("nameLabel");
}

thanks
Masudur
As a general rule, it's a good idea to break up the statements in a single
line multi-expression code line into their own lines of code to facilitate
setting breakpoints and being able to examine values. The way you have it
now, it would be extremely difficult to tell what is going on.

-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA): http://www.blogmetafinder.com
"Ben" wrote:

Quote:

Originally Posted by

Hi,
>
I want to access in code-behind a label within the ItemTemplate of a
Formview.
>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="id"
DataSourceID="SqlDataSource1" >
<ItemTemplate>
<asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name")
%>'></asp:Label>
>
code-behind:
Dim name As String = CType(FormView1.FindControl("nameLabel"), Label).Text
>
>
I get the error: "Object reference not set to an instance of an object"
>
Could somebody tell me what's wrong in my code?
Thanks
Ben
>
>
>
>

Object reference not set to an instance of an object

i'm using asp.net 1.1 & ms access.
the ms-access database is placed in a separate folder with permissions set as belo
Administrator : full contro
ASPNET : full contro
user1 : full contro
& set to open in shared mode, no record locking
the file is also shared for user1 to insert/update the database through a desktop Access Application

I get the data & display it in the aspx page (no updating/inserting is done through the asp.net appln.) as below
-get the records from Table1 & fill a DrpDown Box with th dat
-based on the selection of the above dropdown box, I get a set of related records from Table2 & fill a datagrid with i
the page works fine most of the times, but some times i get an error message "Object reference not set to an instance of an object." when the asp.net appln. tries to read data from the database & fill the dropdown box, though there is data in the table

Is it due to exclusive locking by some form in the access appln or some problem with permissions or what else

need urgent help pls

thanxtypically, this exception is thrown when a method or property is called on a
null object. i suspect it is coming from the data returned in the dataset.
are you explicitly testing before calling methods on the object? Consider:
DataSet ds = "some query was fired to return results"
test before you bind
if(ds != null && ds.Tables[0].Rows.Count > 0)
//bind
else
//indicate no data condition

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
"D Pahadsing" <dpahadsing@.yahoo.co.in> wrote in message
news:5F224CE0-C04D-4FB7-9BE5-74AFD2EEA622@.microsoft.com...
> i'm using asp.net 1.1 & ms access.
> the ms-access database is placed in a separate folder with permissions set
as below
> Administrator : full control
> ASPNET : full control
> user1 : full control
> & set to open in shared mode, no record locking.
> the file is also shared for user1 to insert/update the database through a
desktop Access Application.
> I get the data & display it in the aspx page (no updating/inserting is
done through the asp.net appln.) as below:
> -get the records from Table1 & fill a DrpDown Box with th data
> -based on the selection of the above dropdown box, I get a set of related
records from Table2 & fill a datagrid with it
> the page works fine most of the times, but some times i get an error
message "Object reference not set to an instance of an object." when the
asp.net appln. tries to read data from the database & fill the dropdown box,
though there is data in the table.
> Is it due to exclusive locking by some form in the access appln or some
problem with permissions or what else?
> need urgent help pls.
> thanx

Object reference not set to an instance of an object

Hi,
I want to access in code-behind a label within the ItemTemplate of a
Formview.
<asp:FormView ID="FormView1" runat="server" DataKeyNames="id"
DataSourceID="SqlDataSource1" >
<ItemTemplate>
<asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name")
%>'></asp:Label>
code-behind:
Dim name As String = CType(FormView1.FindControl("nameLabel"), Label).Text
I get the error: "Object reference not set to an instance of an object"
Could somebody tell me what's wrong in my code?
Thanks
BenTry
FormView1.Row.FindControl(...)
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Ben" <b@.bn> wrote in message
news:%23obfXb8tHHA.3364@.TK2MSFTNGP02.phx.gbl...
> Hi,
> I want to access in code-behind a label within the ItemTemplate of a
> Formview.
> <asp:FormView ID="FormView1" runat="server" DataKeyNames="id"
> DataSourceID="SqlDataSource1" >
> <ItemTemplate>
> <asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name")
> %>'></asp:Label>
> code-behind:
> Dim name As String = CType(FormView1.FindControl("nameLabel"), Label).Text
>
> I get the error: "Object reference not set to an instance of an object"
> Could somebody tell me what's wrong in my code?
> Thanks
> Ben
>
>
On Jun 26, 2:06 pm, "Eliyahu Goldin"
<REMOVEALLCAPITALSeEgGoldD...@.mMvVpPsS.org> wrote:
> Try
> FormView1.Row.FindControl(...)
> --
> Eliyahu Goldin,
> Software Developer & Consultant
> Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net
> "Ben" <b@.bn> wrote in message
> news:%23obfXb8tHHA.3364@.TK2MSFTNGP02.phx.gbl...
>
>
>
>
>
>
hi
,
if (FormView1.DefaultMode == FormViewMode.ReadOnly)
{
Label lbl = (Label) FormView1.FindControl("nameLabel");
}
thanks
Masudur
As a general rule, it's a good idea to break up the statements in a single
line multi-expression code line into their own lines of code to facilitate
setting breakpoints and being able to examine values. The way you have it
now, it would be extremely difficult to tell what is going on.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA): http://www.blogmetafinder.com
"Ben" wrote:

> Hi,
> I want to access in code-behind a label within the ItemTemplate of a
> Formview.
> <asp:FormView ID="FormView1" runat="server" DataKeyNames="id"
> DataSourceID="SqlDataSource1" >
> <ItemTemplate>
> <asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name")
> %>'></asp:Label>
> code-behind:
> Dim name As String = CType(FormView1.FindControl("nameLabel"), Label).Text
>
> I get the error: "Object reference not set to an instance of an object"
> Could somebody tell me what's wrong in my code?
> Thanks
> Ben
>
>

Object reference not set to an instance of an object

Hi,
I want to access in code-behind a label within the ItemTemplate of a
Formview.
<asp:FormView ID="FormView1" runat="server" DataKeyNames="id"
DataSourceID="SqlDataSource1" >
<ItemTemplate>
<asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name")
%>'></asp:Label>
code-behind:
Dim name As String = CType(FormView1.FindControl("nameLabel"), Label).Text
I get the error: "Object reference not set to an instance of an object"
Could somebody tell me what's wrong in my code?
Thanks
BenHi Ben,
The direct answer is: nothing wrong.
It's probably an issue outside given code. Checks FormView1.Row and FormView
1.DataItem
are not null. If yes then be sure you have at least one record in FormView1'
s
source and DataBind is performed on the FormView1. You cannot retrieve contr
ols
if they are not processed.
Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com
B> FormView1.FindControl("nameLabel")
B>

Object reference not set to an instance of an object

I keep getting the following error when I access my Schedule.aspx page.


[NullReferenceException: Object reference not set to an instance of an object.]
Sports.Games.ReadGame(Int32 GameID) in c:\Inetpub\wwwroot\app1\inc\components\games.cs:83
UBM.Schedule.Page_Load(Object sender, EventArgs e) in c:\Inetpub\wwwroot\app1\inc\cb\schedule.cs:24
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +731

I'm using schedule.cs as my CodeBehind and it uses a class file called Games. I was able to compile both successfully. I have a DataList control called "dlstGameList" in my Schedule.aspx page. The rest of my code are as follows:

schedule.cs


using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sports;

namespace UBM

{

public class Schedule : Page{

protected virtual void Page_Load(Object sender , EventArgs e) {

DataList dlstGameList;

Games objGame = new Games();
objGame.ReadGame(1);

dlstGameList = objGame.dlstGames;
}

}
}

games.cs

namespace Sports {

using System;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

public class Games {

private SqlCommand cmdQuery;

private string strQuery;
private SqlConnection conDados;
public DataList dlstGames;

private SqlDataReader dtrGames;

public Games(){

}

public void ReadGame(int GameID) {

conDados = new SqlConnection( "Server=(local);USER ID=*****;Password=*****;database=*****" );
conDados.Open();

strQuery = "Select * From tbGames WHERE ID = " + GameID;
cmdQuery = new SqlCommand(strQuery, conDados );

dtrGames = cmdQuery.ExecuteReader();
conDados.Close();

dlstGames.DataSource = dtrGames;
dlstGames.DataBind();
}

}
}

<b>Is there anything I'm missing here? I read somewhere that it is a bug and you need to register a DLL in the gacutil but I can't find that file in my .Net directory.

Thanks.Check to make sure dtrGames isn't null, maybe? Also, make sure GameID has a value.

Brian

Friday, March 16, 2012

Object reference not set to an instance of an object.

Hi all,Very strange problem. Everything was working just fine and then suddenly whatever page i access the following error message:

Object reference not set to an instance of an object.

The error line is always the first line of the .cs file.

The stack trace is:

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.UI.Control.CreateControlCollection() +0
System.Web.UI.Control.get_Controls() +18
System.Web.UI.Control.AddParsedSubObject(Object obj) +29
System.Web.UI.Control.System.Web.UI.IParserAccessor.AddParsedSubObject(Object obj) +4
ASP.MyStatus_aspx.__BuildControlTree(Control __ctrl) in C:\Inetpub\wwwroot\Status\Status.aspx:1
ASP.MyStatus_aspx.FrameworkInitialize() in c:\WINNT\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files\status\0a48e995\ae7978a4\1rs0b7cc.0.cs:0
System.Web.UI.Page.ProcessRequest() +85
System.Web.UI.Page.ProcessRequest(HttpContext context) +18
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication+IExecutionStep.Execute() +179
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +87

I dont understand how all of a sudden this error could crop up. I was debugging the app rigorously. Could this be a reason/

How can i solve this. Im using 1.1 framework.

i tried to check if the other 1.1 framework projects are working. they too are showing the same error.

Please help

Thanks

Hi,

Try restarting your system.If the error persists give us some more hints about the code.


I dont think its related to my code coz i tried to access another app which was in 1.1 it too gave the same error wherein the line1 was the point of error which is same as the one im getting here. I tried to restart but still its not coming the same.

What else could be the error.

Thanks


Do you have .NET 2.0 installed in the system?

Yes .net 2.0 is installed. But my app is in 1.1 framework.

I simply dont understand how this could happen all of a sudden. i am able to build my application successfully.

Any solutions.

Thanks


Hi,

Usually, this kind of error occured when the object is null. Sometime object doesnt initialize. In such cases, this kind of error occured.

Please know us about ur code more, so we can help you out.

Regards,

Nirav Patel


Nitinkcv:

Yes .net 2.0 is installed. But my app is in 1.1 framework.

I simply dont understand how this could happen all of a sudden. i am able to build my application successfully.

Any solutions.

Thanks

Was it working fine previously when .NET 2.0 was also installed? Please check the virtual directory properties -> ASP.NET and see whether the correct .NET version is selected.


Hi all,

I dont know how it worked but running iisreset in the prompt has solved my issue.

Anyway thanks for all your help.

Regards,

Nitin