473,395 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

instance of sqlDataReader - is not available to an exteranl method

I am trying to create a method GetDataFor(string column) becaues I have to repeat the same statements for several columns but I get an error as follows:

The name 'dr' does not exist in the current context

It seems the dr -the instance of sqlDataReader - is not becoming available to the method. How to make it available? This seems trivial and newbie problem related to OOP, but I am confused. Could you pl. look at the code or point me to some relevant URL.

Thanks !
protected void Page_Load(object sender, EventArgs e)
{
string strConnection = ConfigurationManager.ConnectionStrings["strConnMyDb"].ConnectionString;
SqlConnection objConnection = new SqlConnection(strConnection);
string strSQL = "SELECT meanTemp FROM tblWeather WHERE (Year(obsDate)=2004) ORDER BY obsDate;";
SqlCommand objCommand = new SqlCommand(strSQL, objConnection);
objConnection.Open();
SqlDataReader dr = objCommand.ExecuteReader();
while (dr.Read()) {
Response.Write(GetDataFor("meanTemp ").ToString() + "<br>");
}
dr.Close();
objConnection.Close();
}

public double GetDataFor(string column)
{
double result = (dr.IsDBNull(dr.GetOrdinal(column))) //ERROR: The name 'dr' does not exist in the current context
? 9999 : double.Parse(dr[colum].ToString());
return result;
}
Apr 7 '06 #1
7 1688
SqlDataReader dr is being declared inside Page_Load() that means it's scope
is limited to the Page_Load method. GetDataFor() cannot "see" dr. Declare
dr outside of Page_Load().

SqlDataReader dr = new SqlDataReader();

protected void Page_Load(object sender, EventArgs e)
{
...

http://msdn.microsoft.com/library/de...rpspec_3_7.asp

Hope this helps.

Andrew

"Web learner" <be******@learning.edu> wrote in message
news:O$**************@TK2MSFTNGP03.phx.gbl...
I am trying to create a method GetDataFor(string column) becaues I have to
repeat the same statements for several columns but I get an error as
follows:

The name 'dr' does not exist in the current context

It seems the dr -the instance of sqlDataReader - is not becoming available
to the method. How to make it available? This seems trivial and newbie
problem related to OOP, but I am confused. Could you pl. look at the code or
point me to some relevant URL.

Thanks !
protected void Page_Load(object sender, EventArgs e)
{
string strConnection =
ConfigurationManager.ConnectionStrings["strConnMyDb"].ConnectionString;
SqlConnection objConnection = new SqlConnection(strConnection);
string strSQL = "SELECT meanTemp FROM tblWeather WHERE
(Year(obsDate)=2004) ORDER BY obsDate;";
SqlCommand objCommand = new SqlCommand(strSQL, objConnection);
objConnection.Open();
SqlDataReader dr = objCommand.ExecuteReader();
while (dr.Read()) {
Response.Write(GetDataFor("meanTemp ").ToString() + "<br>");
}
dr.Close();
objConnection.Close();
}

public double GetDataFor(string column)
{
double result = (dr.IsDBNull(dr.GetOrdinal(column))) //ERROR: The name
'dr' does not exist in the current context
? 9999 : double.Parse(dr[colum].ToString());
return result;
}
Apr 7 '06 #2
if you want your SqlDataReader to be visible in private or public methods
within which it was not declared, you would need to declare it at the class
level:

private SqlDataReader dr=null;
protected void Page_Load(object sender, EventArgs e)
{
string strConnection =
ConfigurationManager.ConnectionStrings["strConnMyDb"].ConnectionString;
SqlConnection objConnection = new SqlConnection(strConnection);
string strSQL = "SELECT meanTemp FROM tblWeather WHERE (Year(obsDate)=2004)
ORDER BY obsDate;";
SqlCommand objCommand = new SqlCommand(strSQL, objConnection);
objConnection.Open();
dr = objCommand.ExecuteReader();
while (dr.Read()) {
Response.Write(GetDataFor("meanTemp ").ToString() + "<br>");
}
dr.Close();
objConnection.Close();
}

-- Does that help?
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Web learner" wrote:
I am trying to create a method GetDataFor(string column) becaues I have to repeat the same statements for several columns but I get an error as follows:

The name 'dr' does not exist in the current context

It seems the dr -the instance of sqlDataReader - is not becoming available to the method. How to make it available? This seems trivial and newbie problem related to OOP, but I am confused. Could you pl. look at the code or point me to some relevant URL.

Thanks !
protected void Page_Load(object sender, EventArgs e)
{
string strConnection = ConfigurationManager.ConnectionStrings["strConnMyDb"].ConnectionString;
SqlConnection objConnection = new SqlConnection(strConnection);
string strSQL = "SELECT meanTemp FROM tblWeather WHERE (Year(obsDate)=2004) ORDER BY obsDate;";
SqlCommand objCommand = new SqlCommand(strSQL, objConnection);
objConnection.Open();
SqlDataReader dr = objCommand.ExecuteReader();
while (dr.Read()) {
Response.Write(GetDataFor("meanTemp ").ToString() + "<br>");
}
dr.Close();
objConnection.Close();
}

public double GetDataFor(string column)
{
double result = (dr.IsDBNull(dr.GetOrdinal(column))) //ERROR: The name 'dr' does not exist in the current context
? 9999 : double.Parse(dr[colum].ToString());
return result;
}

Apr 7 '06 #3
Modify the function signature to:

public double GetDataFor(SqlDataReader dr, string column)

and the call to it as:
Response.Write(GetDataFor(dr, "meanTemp ").ToString() + "<br>");

Resist the urge to make the scope of dr page level to avoid conflicts with
other functions on the page.
"Web learner" <be******@learning.edu> wrote in message
news:O$**************@TK2MSFTNGP03.phx.gbl...
I am trying to create a method GetDataFor(string column) becaues I have to
repeat the same statements for several columns but I get an error as
follows:

The name 'dr' does not exist in the current context

It seems the dr -the instance of sqlDataReader - is not becoming available
to the method. How to make it available? This seems trivial and newbie
problem related to OOP, but I am confused. Could you pl. look at the code or
point me to some relevant URL.

Thanks !
protected void Page_Load(object sender, EventArgs e)
{
string strConnection =
ConfigurationManager.ConnectionStrings["strConnMyDb"].ConnectionString;
SqlConnection objConnection = new SqlConnection(strConnection);
string strSQL = "SELECT meanTemp FROM tblWeather WHERE
(Year(obsDate)=2004) ORDER BY obsDate;";
SqlCommand objCommand = new SqlCommand(strSQL, objConnection);
objConnection.Open();
SqlDataReader dr = objCommand.ExecuteReader();
while (dr.Read()) {
Response.Write(GetDataFor("meanTemp ").ToString() + "<br>");
}
dr.Close();
objConnection.Close();
}

public double GetDataFor(string column)
{
double result = (dr.IsDBNull(dr.GetOrdinal(column))) //ERROR: The name
'dr' does not exist in the current context
? 9999 : double.Parse(dr[colum].ToString());
return result;
}
Apr 7 '06 #4
Peter's suggestion just worked, and I am happy. However, to get wiser and
become a little bit helping like you all, I need to make my fundamentals
clear. I'll appreciate poiniting to any ASP.NET example on the web.

I am trying to have a look at suggestions of Jim and Andrew.

Jim, do you mean I should avoid adding line
private SqlDataReader dr=null;
above Page_load method? Without adding SqlDataReader dr=null; the suggested
code did not work. May be I have to add something more.

Thanks everybody,

web_learner

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:93**********************************@microsof t.com...
if you want your SqlDataReader to be visible in private or public methods
within which it was not declared, you would need to declare it at the
class
level:

private SqlDataReader dr=null;
protected void Page_Load(object sender, EventArgs e)
{
string strConnection =
ConfigurationManager.ConnectionStrings["strConnMyDb"].ConnectionString;
SqlConnection objConnection = new SqlConnection(strConnection);
string strSQL = "SELECT meanTemp FROM tblWeather WHERE
(Year(obsDate)=2004)
ORDER BY obsDate;";
SqlCommand objCommand = new SqlCommand(strSQL, objConnection);
objConnection.Open();
dr = objCommand.ExecuteReader();
while (dr.Read()) {
Response.Write(GetDataFor("meanTemp ").ToString() + "<br>");
}
dr.Close();
objConnection.Close();
}

-- Does that help?
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Web learner" wrote:
I am trying to create a method GetDataFor(string column) becaues I have
to repeat the same statements for several columns but I get an error as
follows:

The name 'dr' does not exist in the current context

It seems the dr -the instance of sqlDataReader - is not becoming
available to the method. How to make it available? This seems trivial and
newbie problem related to OOP, but I am confused. Could you pl. look at
the code or point me to some relevant URL.

Thanks !
protected void Page_Load(object sender, EventArgs e)
{
string strConnection =
ConfigurationManager.ConnectionStrings["strConnMyDb"].ConnectionString;
SqlConnection objConnection = new SqlConnection(strConnection);
string strSQL = "SELECT meanTemp FROM tblWeather WHERE
(Year(obsDate)=2004) ORDER BY obsDate;";
SqlCommand objCommand = new SqlCommand(strSQL, objConnection);
objConnection.Open();
SqlDataReader dr = objCommand.ExecuteReader();
while (dr.Read()) {
Response.Write(GetDataFor("meanTemp ").ToString() + "<br>");
}
dr.Close();
objConnection.Close();
}

public double GetDataFor(string column)
{
double result = (dr.IsDBNull(dr.GetOrdinal(column))) //ERROR: The
name 'dr' does not exist in the current context
? 9999 : double.Parse(dr[colum].ToString());
return result;
}

Apr 8 '06 #5
Yes, that is what I meant. Both changes that I suggested were necessary, and
no others. Don't mix my suggestion with Peter's. While his suggestion will
work, it increases the scope of dr unecessarily.

Other things you should look at are the c# "using" statement for the
SqlConnection, the DataReader CommandBehavior.CloseConnection and
parameterized queries (regarding hard coding 2004).

the entire code should read:

protected void Page_Load(object sender, EventArgs e)
{
string strConnection =
ConfigurationManager.ConnectionStrings["strConnMyDb"].ConnectionString;
SqlConnection objConnection = new SqlConnection(strConnection);
string strSQL = "SELECT meanTemp FROM tblWeather WHERE
(Year(obsDate)=2004) ORDER BY obsDate;";
SqlCommand objCommand = new SqlCommand(strSQL, objConnection);
objConnection.Open();
SqlDataReader dr = objCommand.ExecuteReader();
while (dr.Read()) {
Response.Write(GetDataFor(dr, "meanTemp ").ToString() + "<br>");
}
dr.Close();
objConnection.Close();
}

public double GetDataFor(SqlDataReader dr, string column)
{
double result = (dr.IsDBNull(dr.GetOrdinal(column)))
? 9999 : double.Parse(dr[colum].ToString());
return result;
}

"Web learner" <be******@learning.edu> wrote in message
news:ei*************@TK2MSFTNGP04.phx.gbl...
Jim, do you mean I should avoid adding line
private SqlDataReader dr=null;
above Page_load method? Without adding SqlDataReader dr=null; the
suggested code did not work. May be I have to add something more.

Apr 8 '06 #6
This too worked fine. Following your suggestion about using the "c# using",
I've tried to look at example codes of Personal Web starter kit for Visual
web developer express 2005. There are examples, but being a learner/novice
who wants to grasp basic concepts in ASP.NET web programming, I found it a
bit overwhelming. I'll keep on trying and would prefer a book with
exercises.

Thanks/regards,
web_learner.
"Jim Hughes" <NO*********@Hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Yes, that is what I meant. Both changes that I suggested were necessary,
and no others. Don't mix my suggestion with Peter's. While his suggestion
will work, it increases the scope of dr unecessarily.

Other things you should look at are the c# "using" statement for the
SqlConnection, the DataReader CommandBehavior.CloseConnection and
parameterized queries (regarding hard coding 2004).

the entire code should read:

protected void Page_Load(object sender, EventArgs e)
{
string strConnection =
ConfigurationManager.ConnectionStrings["strConnMyDb"].ConnectionString;
SqlConnection objConnection = new SqlConnection(strConnection);
string strSQL = "SELECT meanTemp FROM tblWeather WHERE
(Year(obsDate)=2004) ORDER BY obsDate;";
SqlCommand objCommand = new SqlCommand(strSQL, objConnection);
objConnection.Open();
SqlDataReader dr = objCommand.ExecuteReader();
while (dr.Read()) {
Response.Write(GetDataFor(dr, "meanTemp ").ToString() + "<br>");
}
dr.Close();
objConnection.Close();
}

public double GetDataFor(SqlDataReader dr, string column)
{
double result = (dr.IsDBNull(dr.GetOrdinal(column)))
? 9999 : double.Parse(dr[colum].ToString());
return result;
}

"Web learner" <be******@learning.edu> wrote in message
news:ei*************@TK2MSFTNGP04.phx.gbl...
Jim, do you mean I should avoid adding line
private SqlDataReader dr=null;
above Page_load method? Without adding SqlDataReader dr=null; the
suggested code did not work. May be I have to add something more.


Apr 8 '06 #7
Here is an example with my suggestions.

protected void Page_Load(object sender, EventArgs e)
{
string strConnection =
ConfigurationManager.ConnectionStrings["strConnMyDb"].ConnectionString;

// using means you do not have to explicitly close or dispose the connection
using (SqlConnection objConnection = new SqlConnection(strConnection) )
{
string strSQL = "SELECT meanTemp FROM tblWeather WHERE
(Year(obsDate)=@TargetYear) ORDER BY obsDate;";

SqlCommand objCommand = new SqlCommand(strSQL, objConnection);

// @TargetYear value could be obtained from a form field or querystring
// Validation of parameters should also be performed here.
objCommand.Parameters.AddWithValue(@TargetYear, "2004");

// CommandBehavior.CloseConnection means you don't have to remember to close
the Reader
SqlDataReader dr =
objCommand.ExecuteReader(CommandBehavior.CloseConn ection);
while (dr.Read()) {
Response.Write(GetDataFor(dr, "meanTemp ").ToString() + "<br>");
}
}
}

"Web learner" <be******@learning.edu> wrote in message
news:uK**************@TK2MSFTNGP03.phx.gbl...
This too worked fine. Following your suggestion about using the "c#
using", I've tried to look at example codes of Personal Web starter kit
for Visual web developer express 2005. There are examples, but being a
learner/novice who wants to grasp basic concepts in ASP.NET web
programming, I found it a bit overwhelming. I'll keep on trying and would
prefer a book with exercises.

Thanks/regards,
web_learner.
"Jim Hughes" <NO*********@Hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Yes, that is what I meant. Both changes that I suggested were necessary,
and no others. Don't mix my suggestion with Peter's. While his suggestion
will work, it increases the scope of dr unecessarily.

Other things you should look at are the c# "using" statement for the
SqlConnection, the DataReader CommandBehavior.CloseConnection and
parameterized queries (regarding hard coding 2004).

the entire code should read:

protected void Page_Load(object sender, EventArgs e)
{
string strConnection =
ConfigurationManager.ConnectionStrings["strConnMyDb"].ConnectionString;
SqlConnection objConnection = new SqlConnection(strConnection);
string strSQL = "SELECT meanTemp FROM tblWeather WHERE
(Year(obsDate)=2004) ORDER BY obsDate;";
SqlCommand objCommand = new SqlCommand(strSQL, objConnection);
objConnection.Open();
SqlDataReader dr = objCommand.ExecuteReader();
while (dr.Read()) {
Response.Write(GetDataFor(dr, "meanTemp ").ToString() + "<br>");
}
dr.Close();
objConnection.Close();
}

public double GetDataFor(SqlDataReader dr, string column)
{
double result = (dr.IsDBNull(dr.GetOrdinal(column)))
? 9999 : double.Parse(dr[colum].ToString());
return result;
}

"Web learner" <be******@learning.edu> wrote in message
news:ei*************@TK2MSFTNGP04.phx.gbl...
Jim, do you mean I should avoid adding line
private SqlDataReader dr=null;
above Page_load method? Without adding SqlDataReader dr=null; the
suggested code did not work. May be I have to add something more.



Apr 8 '06 #8

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

Similar topics

6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
17
by: Rodusa | last post by:
I am getting this exception error which is driving me nuts. System.NullReferenceException - Object reference not set to an instance of an object If I comment this line, I don't get any errors:...
1
by: Neo Chou | last post by:
Greetings! I met the same question as in ADO a few months ago. I'm working on MS SQL Server 2000. I have a stored procedure that returns a return value as well as a record set (by "select"...
4
by: Moe Sizlak | last post by:
Hi There, I have a user control that has 2 listmenus populated from a database, I want the form to submit when the listmenu are changed which seems to happen no problem when the page is on it's...
2
by: Cameron Frasnelly | last post by:
I emulated the code from the .Net Framework help (Titled "Using Stored Procedures with a Command") and I still receive and error... Error Received = "Invalid attempt to read when no data is...
2
by: SP | last post by:
Hello All, I have a web method in a web service as follows: public SqlDataReader Display_test_data() { string connectionInfo = ConfigurationSettings.AppSettings;
3
by: wolfgang.lipp | last post by:
some time after posting my `Linkdict recipe`__ to aspn__ -- basically, a dictionary with run-time delegational lookup, but this is not important here -- i thought gee that would be fun to make...
7
by: Web learner | last post by:
I am trying to create a method GetDataFor(string column) becaues I have to repeat the same statements for several columns but I get an error as follows: The name 'dr' does not exist in the current...
2
by: teresa | last post by:
Hi Everyone, I appreciate any help if any of you know or have used this method before and can give me some suggestion. It's a bit difficult to explain: Here is my table: Table1: there are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.