473,513 Members | 2,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

datareader problem

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?

Nov 18 '05 #1
4 1154
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" <hu**@discussions.microsoft.com> wrote in message
news:DA**********************************@microsof t.com...
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?

Nov 18 '05 #2
sorry Jarmo.. i am new to asp.net can you show me with code?

"Jarmo Muukka" wrote:
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" <hu**@discussions.microsoft.com> wrote in message
news:DA**********************************@microsof t.com...
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?


Nov 18 '05 #3
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 1 Nov 2004 03:10:05 -0800, "huzz"
<hu**@discussions.microsoft.com> wrote:
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?


Nov 18 '05 #4
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" <hu**@discussions.microsoft.com> wrote in message
news:B7**********************************@microsof t.com...
sorry Jarmo.. i am new to asp.net can you show me with code?

"Jarmo Muukka" wrote:
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" <hu**@discussions.microsoft.com> wrote in message
news:DA**********************************@microsof t.com...
>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?
>


Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
363
by: Yasutaka Ito | last post by:
Hi, My friend had a little confusion about the working of DataReader after reading an article from MSDN. Following is a message from him... <!-- Message starts --> I was going thru DataReader...
4
3710
by: VB Programmer | last post by:
I have a function that returns a datareader. The problem is that in the function's FINALLY I close the datareader and set it to nothing. But, BEFORE the Finally I "Return" the datareader (in the...
12
467
by: Thomas Scheiderich | last post by:
I have 2 dropdowns that are exactly the same and I don't want to re-read for each. Is there a way to do something like below where I read the data, bind to depCity1 and then bind to destCity2. ...
20
5510
by: Mark | last post by:
Hi all, quick question , a DataView is memory resident "view" of data in a data table therefore once populated you can close the connection to the database. Garbage collection can then be used to...
5
4052
by: robecflo | last post by:
Hi Forum, i have a problem, hope somebody can give me ideas. I'm developing with windows forms and vb.net, and oracle as a database. At this moment i have a table called amortizaciones, this table...
17
2363
by: Alan Silver | last post by:
Hello, I have a generic method in a utility class that grabs an sqldatareader and returns it. Due to the fact that (AFAIK), you can't close the database connection before you've read the data,...
3
2481
by: Carlos Lozano | last post by:
Hello, I am having a problem getting the selectedValue from a dropdownlist that is populated with a dataReader and just can't see the problem. I did the following: dim dr as DataReader dr...
7
2895
by: Diffident | last post by:
Hello All, I would like to use DataReader based accessing in my Data Access Layer (DAL). What is considered to be a best practice while returning from a DAL method that executes a query and...
1
3056
by: Steve | last post by:
Hi, I currently display all the data on a website using tableadaptors and objectdatasources. Would it be significantly faster if I was to display the data by writing the code for a datareader...
3
3377
by: Johnny Jörgensen | last post by:
I've got an error that I simply cannot locate: I've got a form in which I use a datareader object to read information from a db. After the read, I close the reader and I dispose of both the...
0
7259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7158
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7380
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5683
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.