Showing posts with label nullreferenceexception. Show all posts
Showing posts with label nullreferenceexception. Show all posts

Thursday, March 29, 2012

Object reference

I get an error message:
Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

I get an error in line:
If Session("user") Is Nothing Then...

What could be wrong?Apparently, the Session doesn't exist in the context of the line you posted.

--
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"simon" <simon.zupan@.stud-moderna.si> wrote in message
news:e4bE4IDtDHA.2148@.TK2MSFTNGP12.phx.gbl...
> I get an error message:
> Exception Details: System.NullReferenceException: Object reference not set
> to an instance of an object.
> I get an error in line:
> If Session("user") Is Nothing Then...
> What could be wrong?
>
>
Hi,

> I get an error in line:
> If Session("user") Is Nothing Then...
> What could be wrong?

Session object is null (Nothing).

1) Check <sessionState> element in your Web.config file.
2) Make sure that you retrieve it correctly.

--
______________________________
With best wishes, Arthur Nesterovsky
Please visit my home page:
http://www.nesterovsky-bros.com
Hi,

If I use session object in user control(aspx page), it doesn't work.
If I write the same code in default.aspx then everything is fine.

In past I have code and all my functions in include.asp page, which I
included on all my pages.
I had line:
If Session("user") Is Nothing Then
response.redirect "login.asp"
End If

Now I move all my functions to user control (by the book) and register it on
all my pages - approximate the similar like with include.
But if the session won't work, I don't want to write line with session
checking on all my pages.
Should I use include statement(which is past from asp) or is there some
other way?

Thank you,
Simon

"Arthur Nesterovsky" <arthur_NO_SPAM_PLEASE_@.nesterovsky-bros.com> wrote in
message news:uHYr6XDtDHA.1876@.TK2MSFTNGP09.phx.gbl...
> Hi,
> > I get an error in line:
> > If Session("user") Is Nothing Then...
> > What could be wrong?
> Session object is null (Nothing).
> 1) Check <sessionState> element in your Web.config file.
> 2) Make sure that you retrieve it correctly.
> --
> ______________________________
> With best wishes, Arthur Nesterovsky
> Please visit my home page:
> http://www.nesterovsky-bros.com
Hi,

> I had line:
> If Session("user") Is Nothing Then
> response.redirect "login.asp"
> End If
> Now I move all my functions to user control (by the book) and register it
on
> all my pages - approximate the similar like with include.

Try to check

HttpContext.Current.Session("user") instead.

--
______________________________
With best wishes, Arthur Nesterovsky
Please visit my home page:
http://www.nesterovsky-bros.com
I have control leviMenu.ascx, where I define session:
If HttpContext.Current.Session("user") Is Nothing Then
Dim uporabnik(2)
uporabnik(0) = "1"
uporabnik(1) = "Simon Zupan"
uporabnik(2) = "2"
Session("user") = uporabnik
End If

Then I register it on the page:

<%@. Register TagPrefix ="Menu" TagName = "LeviMenu" Src =
"userControls\leviMenu.ascx"%
then use it on page:

<menu:leviMenu runat="server" ID="Levimenu1"></menu:leviMenu
Then on the same page in Private Sub Page_Load event, I'm reading the value
from session:

HttpContext.Current.Session("user")(0)

And I gett an error message:

System.NullReferenceException: Object variable or With block variable not
set.

The session object is not recognized.

How can I solve this problem?

I have session object in user control because it's registered on all pages.
I can write the session code on each page separeted, it works than, but than
I don't have the code on one place, it doesn't make any sence.

Thank you,
Simon

"Arthur Nesterovsky" <arthur_NO_SPAM_PLEASE_@.nesterovsky-bros.com> wrote in
message news:Oz3PxfMtDHA.1756@.TK2MSFTNGP09.phx.gbl...
> Hi,
> > I had line:
> > If Session("user") Is Nothing Then
> > response.redirect "login.asp"
> > End If
> > Now I move all my functions to user control (by the book) and register
it
> on
> > all my pages - approximate the similar like with include.
> Try to check
> HttpContext.Current.Session("user") instead.
> --
> ______________________________
> With best wishes, Arthur Nesterovsky
> Please visit my home page:
> http://www.nesterovsky-bros.com

Saturday, March 24, 2012

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

Thursday, March 22, 2012

Object reference not set to an instance of an object.

Hi

I am getting an 'System.NullReferenceException: Object reference not set to
an instance of an object.' error on the following line;

Dim sSQL As String = "INSERT INTO applications ( title, forenames, surname,
occupation) " & _ '<== This line gives the error.
"VALUES('" & txtTitle.Text & "', '" &
txtForenames.Text & "', '" & txtSurname.Text & "', '" &
txtOccupation.SelectedItem.Text & "')"

What am I doing wrong?

Thanks

Regardshi john,
The line of code that you gave below might not throw this exception. If you
can paste a few lines below and after it would be much clear.
the best
srini

"John" wrote:

> Hi
> I am getting an 'System.NullReferenceException: Object reference not set to
> an instance of an object.' error on the following line;
> Dim sSQL As String = "INSERT INTO applications ( title, forenames, surname,
> occupation) " & _ '<== This line gives the error.
> "VALUES('" & txtTitle.Text & "', '" &
> txtForenames.Text & "', '" & txtSurname.Text & "', '" &
> txtOccupation.SelectedItem.Text & "')"
> What am I doing wrong?
> Thanks
> Regards
>

Friday, March 16, 2012

Object reference not set to an instance of an object.

Hi
I am getting an 'System.NullReferenceException: Object reference not set to
an instance of an object.' error on the following line;
Dim sSQL As String = "INSERT INTO applications ( title, forenames, surname,
occupation) " & _ '<== This line gives the error.
"VALUES('" & txtTitle.Text & "', '" &
txtForenames.Text & "', '" & txtSurname.Text & "', '" &
txtOccupation.SelectedItem.Text & "')"
What am I doing wrong?
Thanks
Regardshi john,
The line of code that you gave below might not throw this exception. If you
can paste a few lines below and after it would be much clear.
the best
srini
"John" wrote:

> Hi
> I am getting an 'System.NullReferenceException: Object reference not set t
o
> an instance of an object.' error on the following line;
> Dim sSQL As String = "INSERT INTO applications ( title, forenames, surname
,
> occupation) " & _ '<== This line gives the error.
> "VALUES('" & txtTitle.Text & "', '" &
> txtForenames.Text & "', '" & txtSurname.Text & "', '" &
> txtOccupation.SelectedItem.Text & "')"
> What am I doing wrong?
> Thanks
> Regards
>
>