datareader problem

G

Guest

I am having problem accessing a datareader from the aspx page here is my code:

private void Page_Load(object sender, System.EventArgs e)
{
BindData();
}

public void BindData()
{

objConn.Open();
SqlCommand selectCmd = new SqlCommand("SELECT * FROM tblFault
WHERE ID=" + Request.QueryString["id"] ,objConn);
SqlDataReader dr = selectCmd.ExecuteReader();
dr.Read();
// dr.Close();
objConn.Close();
}

when i try to call the dr in the aspx page i get error message:
The name 'dr' does not exist in the class or namespace 'ASP.details_aspx' ..
but it does exist

Here is the code i am using to call the dr in the aspx page
(I am imprting system.data and system.data.sqlclient namespace aswell)
<td><%# dr["ID"]%></td>

any idea why i am getting this error message?
 
J

Jarmo Muukka

Your dr exist in inside of method BindData(). You're trying to use it out of
scope. To make it work, move it as protected member of your page class.

JMu
 
G

Guest

sorry Jarmo.. i am new to asp.net can you show me with code?

Jarmo Muukka said:
Your dr exist in inside of method BindData(). You're trying to use it out of
scope. To make it work, move it as protected member of your page class.

JMu

huzz said:
I am having problem accessing a datareader from the aspx page here is my
code:

private void Page_Load(object sender, System.EventArgs e)
{
BindData();
}

public void BindData()
{

objConn.Open();
SqlCommand selectCmd = new SqlCommand("SELECT * FROM tblFault
WHERE ID=" + Request.QueryString["id"] ,objConn);
SqlDataReader dr = selectCmd.ExecuteReader();
dr.Read();
// dr.Close();
objConn.Close();
}

when i try to call the dr in the aspx page i get error message:
The name 'dr' does not exist in the class or namespace 'ASP.details_aspx'
..
but it does exist

Here is the code i am using to call the dr in the aspx page
(I am imprting system.data and system.data.sqlclient namespace aswell)
<td><%# dr["ID"]%></td>

any idea why i am getting this error message?
 
S

Scott Allen

Hi huzz:

When you declare a variable inside of a method it is known as a local
variable. Local variables can only be seen be the code inside of that
method.
 
J

Jarmo Muukka

I assume that you use code-behind technique to split html and C#-code.

I have never used SqlDataReader as DataSource in UI layer. I write layered
code and hide the data source from UI layer. I use strongly typed
collections that I fill in DAL.

Few things to remember.
1) You need to call DataBind() on all objects or thsi.DataBind(); to bind
your data source with controls
2) You need to close connection object, but don't close it too early i.e.
when you need the dataReader-object.

public class YourPage : Page
{
protected SqlDataReader dr;

public void BindData()
{
objConn.Open();
SqlCommand selectCmd = new SqlCommand("SELECT * FROM tblFault WHERE
ID=" + Request.QueryString["id"] ,objConn);
dr = selectCmd.ExecuteReader();
this.DataBind();
// I am not exactly sure can we close the dr here. Is the prev.
expression done its job?
dr.Close();
objConn.Close();
}

JMu

public class
huzz said:
sorry Jarmo.. i am new to asp.net can you show me with code?

Jarmo Muukka said:
Your dr exist in inside of method BindData(). You're trying to use it out
of
scope. To make it work, move it as protected member of your page class.

JMu

huzz said:
I am having problem accessing a datareader from the aspx page here is my
code:

private void Page_Load(object sender, System.EventArgs e)
{
BindData();
}

public void BindData()
{

objConn.Open();
SqlCommand selectCmd = new SqlCommand("SELECT * FROM
tblFault
WHERE ID=" + Request.QueryString["id"] ,objConn);
SqlDataReader dr = selectCmd.ExecuteReader();
dr.Read();
// dr.Close();
objConn.Close();
}

when i try to call the dr in the aspx page i get error message:
The name 'dr' does not exist in the class or namespace
'ASP.details_aspx'
..
but it does exist

Here is the code i am using to call the dr in the aspx page
(I am imprting system.data and system.data.sqlclient namespace aswell)
<td><%# dr["ID"]%></td>

any idea why i am getting this error message?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top