.NETGURU
Scope of the variable in ASP.NET Page
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngcs' list.


Naweed Akram
Hi,
I have a Webform with two text boxes and one button "Next". The
following is the Code behind class for my webform. Whenever the pages
loads (for the first time), I open a connection to the database, execute
an sql statement and store the result in a DataReader Object. Also, I
read the first record and display it in the text boxes. Now the problem
occures is when I press the Next Button. Logically, I have declared the
connection and DataReader on the Page level (PROTECTED), so these
objects should be available to me. But when I press the Next button, and
exception is raised saying that my DataReader object is NULL.

Any solution to this.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApp11
{
public class WebForm1 : System.Web.UI.Page
{
protected OleDbConnection myConn;
protected OleDbCommand myCommand;
protected System.Web.UI.WebControls.TextBox
TextBox1;
protected System.Web.UI.WebControls.TextBox
TextBox2;
protected System.Web.UI.WebControls.Button
Button1;
protected OleDbDataReader myReader;

private void Page_Load(object sender,
System.EventArgs e)
{
if(!Page.IsPostBack)
{
string
conStm=@"Provider=Microsoft.Jet.OLEDB.4.0; Data";
conStm+=@"
Source=e:\coes\dbcoes.mdb";
myConn=new
OleDbConnection(conStm);
myConn.Open();
myCommand=new
OleDbCommand("select * from tblCustomers", myConn);

myReader=myCommand.ExecuteReader();

if(myReader.Read())
{

TextBox1.Text=myReader["custName"].ToString();

TextBox2.Text=myReader["custTel"].ToString();
}
}
}

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Button1.Click += new
System.EventHandler(this.Button1_Click);
this.Load += new
System.EventHandler(this.Page_Load);
}

private void Button1_Click(object sender,
System.EventArgs e)
{
if(myReader.Read())
{

TextBox1.Text=myReader["custName"].ToString();

TextBox2.Text=myReader["custTel"].ToString();
}
}
}
}
Reply to this message...
 
    
Sills, Adam
ASP.NET is stateless. Any code you run in your codebehind is re-run every
single page request. The first time around (since it's not a postback) your
data read is initialized. The second time around that code is not run,
because it is a postback, hence your data reader is null. How many items
does your sql statement return? If it's not very many, I would suggest
reading everything into an array, storing the array in ViewState (again, if
it's not big) and keep track of how many clicks you've performed using
ViewState also.

adam..
-----Original Message-----
From: Naweed Akram [mailto:Click here to reveal e-mail address]
Sent: Tuesday, July 02, 2002 4:44 PM
To: aspngcs
Subject: [aspngcs] Scope of the variable in ASP.NET Page

Hi,
I have a Webform with two text boxes and one button "Next". The following is
the Code behind class for my webform. Whenever the pages loads (for the
first time), I open a connection to the database, execute an sql statement
and store the result in a DataReader Object. Also, I read the first record
and display it in the text boxes. Now the problem occures is when I press
the Next Button. Logically, I have declared the connection and DataReader on
the Page level (PROTECTED), so these objects should be available to me. But
when I press the Next button, and exception is raised saying that my
DataReader object is NULL.

Any solution to this.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApp11
{
public class WebForm1 : System.Web.UI.Page
{
protected OleDbConnection myConn;
protected OleDbCommand myCommand;
protected System.Web.UI.WebControls.TextBox
TextBox1;
protected System.Web.UI.WebControls.TextBox
TextBox2;
protected System.Web.UI.WebControls.Button Button1;
protected OleDbDataReader myReader;

private void Page_Load(object sender,
System.EventArgs e)
{
if(!Page.IsPostBack)
{
string
conStm=@"Provider=Microsoft.Jet.OLEDB.4.0; Data";
conStm+=@"
Source=e:\coes\dbcoes.mdb";
myConn=new
OleDbConnection(conStm);
myConn.Open();
myCommand=new
OleDbCommand("select * from tblCustomers", myConn);

myReader=myCommand.ExecuteReader();

if(myReader.Read())
{

TextBox1.Text=myReader["custName"].ToString();

TextBox2.Text=myReader["custTel"].ToString();
}
}
}

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Button1.Click += new
System.EventHandler(this.Button1_Click);
this.Load += new
System.EventHandler(this.Page_Load);
}

private void Button1_Click(object sender,
System.EventArgs e)
{
if(myReader.Read())
{

TextBox1.Text=myReader["custName"].ToString();

TextBox2.Text=myReader["custTel"].ToString();
}
}
}
}
| [aspngcs] member Click here to reveal e-mail address = YOUR ID |
http://www.asplists.com/asplists/aspngcs.asp = JOIN/QUIT
Reply to this message...
 
    
Glavich, Paul C
You will need to re-establish the connection for each page, or persist this
somewhere for each page to access. You mentioned the scope of variables,
which is correct, you can access them, however their state is not maintained
between requests so even though you have access to that variable, its
value/state is not what it was when you set it at the last page.

So you need to re-connect on each page, persist the connectoin object in
session state, have an application wide connection object or something
similar...

* Paul Glavich
Professional Web Services
Email: <mailto:Click here to reveal e-mail address> Click here to reveal e-mail address
(Currently no fixed phone or street address)
-----Original Message-----
From: Naweed Akram [mailto:Click here to reveal e-mail address]
Sent: Wednesday, 3 July 2002 6:44 AM
To: aspngcs
Subject: [aspngcs] Scope of the variable in ASP.NET Page

Hi,
I have a Webform with two text boxes and one button "Next". The following is
the Code behind class for my webform. Whenever the pages loads (for the
first time), I open a connection to the database, execute an sql statement
and store the result in a DataReader Object. Also, I read the first record
and display it in the text boxes. Now the problem occures is when I press
the Next Button. Logically, I have declared the connection and DataReader on
the Page level (PROTECTED), so these objects should be available to me. But
when I press the Next button, and exception is raised saying that my
DataReader object is NULL.

Any solution to this.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApp11
{
public class WebForm1 : System.Web.UI.Page
{
protected OleDbConnection myConn;
protected OleDbCommand myCommand;
protected System.Web.UI.WebControls.TextBox
TextBox1;
protected System.Web.UI.WebControls.TextBox
TextBox2;
protected System.Web.UI.WebControls.Button Button1;
protected OleDbDataReader myReader;

private void Page_Load(object sender,
System.EventArgs e)
{
if(!Page.IsPostBack)
{
string
conStm=@"Provider=Microsoft.Jet.OLEDB.4.0; Data";
conStm+=@"
Source=e:\coes\dbcoes.mdb";
myConn=new
OleDbConnection(conStm);
myConn.Open();
myCommand=new
OleDbCommand("select * from tblCustomers", myConn);

myReader=myCommand.ExecuteReader();

if(myReader.Read())
{

TextBox1.Text=myReader["custName"].ToString();

TextBox2.Text=myReader["custTel"].ToString();
}
}
}

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Button1.Click += new
System.EventHandler(this.Button1_Click);
this.Load += new
System.EventHandler(this.Page_Load);
}

private void Button1_Click(object sender,
System.EventArgs e)
{
if(myReader.Read())
{

TextBox1.Text=myReader["custName"].ToString();

TextBox2.Text=myReader["custTel"].ToString();
}
}
}
}
| [aspngcs] member Click here to reveal e-mail address = YOUR ID |
http://www.asplists.com/asplists/aspngcs.asp = JOIN/QUIT
Reply to this message...
 
    
Adnan Qamar
if(!Page.IsPostBack)
{
.....
}
is the problem. You need to populate your
datareader again everytime your page loads.

Adnan

-----Original Message-----
From: Naweed Akram [mailto:Click here to reveal e-mail address]
Sent: Wednesday, July 03, 2002 1:44 AM
To: aspngcs
Subject: [aspngcs] Scope of the variable in ASP.NET Page

Hi,
I have a Webform with two text boxes and one button "Next". The following is
the Code behind class for my webform. Whenever the pages loads (for the
first time), I open a connection to the database, execute an sql statement
and store the result in a DataReader Object. Also, I read the first record
and display it in the text boxes. Now the problem occures is when I press
the Next Button. Logically, I have declared the connection and DataReader on
the Page level (PROTECTED), so these objects should be available to me. But
when I press the Next button, and exception is raised saying that my
DataReader object is NULL.

Any solution to this.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApp11
{
public class WebForm1 : System.Web.UI.Page
{
protected OleDbConnection myConn;
protected OleDbCommand myCommand;
protected System.Web.UI.WebControls.TextBox
TextBox1;
protected System.Web.UI.WebControls.TextBox
TextBox2;
protected System.Web.UI.WebControls.Button Button1;
protected OleDbDataReader myReader;

private void Page_Load(object sender,
System.EventArgs e)
{
if(!Page.IsPostBack)
{
string
conStm=@"Provider=Microsoft.Jet.OLEDB.4.0; Data";
conStm+=@"
Source=e:\coes\dbcoes.mdb";
myConn=new
OleDbConnection(conStm);
myConn.Open();
myCommand=new
OleDbCommand("select * from tblCustomers", myConn);

myReader=myCommand.ExecuteReader();

if(myReader.Read())
{

TextBox1.Text=myReader["custName"].ToString();

TextBox2.Text=myReader["custTel"].ToString();
}
}
}

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Button1.Click += new
System.EventHandler(this.Button1_Click);
this.Load += new
System.EventHandler(this.Page_Load);
}

private void Button1_Click(object sender,
System.EventArgs e)
{
if(myReader.Read())
{

TextBox1.Text=myReader["custName"].ToString();

TextBox2.Text=myReader["custTel"].ToString();
}
}
}
}
| [aspngcs] member Click here to reveal e-mail address = YOUR ID |
http://www.asplists.com/asplists/aspngcs.asp = JOIN/QUIT
Reply to this message...
 
 
System.Data.OleDb.OleDbCommand
System.Data.OleDb.OleDbConnection
System.Data.OleDb.OleDbDataReader
System.EventArgs
System.EventHandler
System.Web.UI.Page
System.Web.UI.WebControls.Button
System.Web.UI.WebControls.TextBox




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification