I am trying to create a method GetDataFor(stri ng 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(objec t sender, EventArgs e)
{
string strConnection = ConfigurationMa nager.Connectio nStrings["strConnMyD b"].ConnectionStri ng;
SqlConnection objConnection = new SqlConnection(s trConnection);
string strSQL = "SELECT meanTemp FROM tblWeather WHERE (Year(obsDate)= 2004) ORDER BY obsDate;";
SqlCommand objCommand = new SqlCommand(strS QL, objConnection);
objConnection.O pen();
SqlDataReader dr = objCommand.Exec uteReader();
while (dr.Read()) {
Response.Write( GetDataFor("mea nTemp ").ToString () + "<br>");
}
dr.Close();
objConnection.C lose();
}
public double GetDataFor(stri ng column)
{
double result = (dr.IsDBNull(dr .GetOrdinal(col umn))) //ERROR: The name 'dr' does not exist in the current context
? 9999 : double.Parse(dr[colum].ToString());
return result;
} 7 1714
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(objec t sender, EventArgs e)
{
... http://msdn.microsoft.com/library/de...rpspec_3_7.asp
Hope this helps.
Andrew
"Web learner" <be******@learn ing.edu> wrote in message
news:O$******** ******@TK2MSFTN GP03.phx.gbl...
I am trying to create a method GetDataFor(stri ng 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(objec t sender, EventArgs e)
{
string strConnection =
ConfigurationMa nager.Connectio nStrings["strConnMyD b"].ConnectionStri ng;
SqlConnection objConnection = new SqlConnection(s trConnection);
string strSQL = "SELECT meanTemp FROM tblWeather WHERE
(Year(obsDate)= 2004) ORDER BY obsDate;";
SqlCommand objCommand = new SqlCommand(strS QL, objConnection);
objConnection.O pen();
SqlDataReader dr = objCommand.Exec uteReader();
while (dr.Read()) {
Response.Write( GetDataFor("mea nTemp ").ToString () + "<br>");
}
dr.Close();
objConnection.C lose();
}
public double GetDataFor(stri ng column)
{
double result = (dr.IsDBNull(dr .GetOrdinal(col umn))) //ERROR: The name
'dr' does not exist in the current context
? 9999 : double.Parse(dr[colum].ToString());
return result;
}
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(objec t sender, EventArgs e)
{
string strConnection =
ConfigurationMa nager.Connectio nStrings["strConnMyD b"].ConnectionStri ng;
SqlConnection objConnection = new SqlConnection(s trConnection);
string strSQL = "SELECT meanTemp FROM tblWeather WHERE (Year(obsDate)= 2004)
ORDER BY obsDate;";
SqlCommand objCommand = new SqlCommand(strS QL, objConnection);
objConnection.O pen();
dr = objCommand.Exec uteReader();
while (dr.Read()) {
Response.Write( GetDataFor("mea nTemp ").ToString () + "<br>");
}
dr.Close();
objConnection.C lose();
}
-- 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(stri ng 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(objec t sender, EventArgs e) { string strConnection = ConfigurationMa nager.Connectio nStrings["strConnMyD b"].ConnectionStri ng; SqlConnection objConnection = new SqlConnection(s trConnection); string strSQL = "SELECT meanTemp FROM tblWeather WHERE (Year(obsDate)= 2004) ORDER BY obsDate;"; SqlCommand objCommand = new SqlCommand(strS QL, objConnection); objConnection.O pen(); SqlDataReader dr = objCommand.Exec uteReader(); while (dr.Read()) { Response.Write( GetDataFor("mea nTemp ").ToString () + "<br>"); } dr.Close(); objConnection.C lose(); }
public double GetDataFor(stri ng column) { double result = (dr.IsDBNull(dr .GetOrdinal(col umn))) //ERROR: The name 'dr' does not exist in the current context ? 9999 : double.Parse(dr[colum].ToString()); return result; }
Modify the function signature to:
public double GetDataFor(SqlD ataReader 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******@learn ing.edu> wrote in message
news:O$******** ******@TK2MSFTN GP03.phx.gbl...
I am trying to create a method GetDataFor(stri ng 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(objec t sender, EventArgs e)
{
string strConnection =
ConfigurationMa nager.Connectio nStrings["strConnMyD b"].ConnectionStri ng;
SqlConnection objConnection = new SqlConnection(s trConnection);
string strSQL = "SELECT meanTemp FROM tblWeather WHERE
(Year(obsDate)= 2004) ORDER BY obsDate;";
SqlCommand objCommand = new SqlCommand(strS QL, objConnection);
objConnection.O pen();
SqlDataReader dr = objCommand.Exec uteReader();
while (dr.Read()) {
Response.Write( GetDataFor("mea nTemp ").ToString () + "<br>");
}
dr.Close();
objConnection.C lose();
}
public double GetDataFor(stri ng column)
{
double result = (dr.IsDBNull(dr .GetOrdinal(col umn))) //ERROR: The name
'dr' does not exist in the current context
? 9999 : double.Parse(dr[colum].ToString());
return result;
}
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*******@yaho o.nospammin.com > wrote in message
news:93******** *************** ***********@mic rosoft.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(objec t sender, EventArgs e) { string strConnection = ConfigurationMa nager.Connectio nStrings["strConnMyD b"].ConnectionStri ng; SqlConnection objConnection = new SqlConnection(s trConnection); string strSQL = "SELECT meanTemp FROM tblWeather WHERE (Year(obsDate)= 2004) ORDER BY obsDate;"; SqlCommand objCommand = new SqlCommand(strS QL, objConnection); objConnection.O pen(); dr = objCommand.Exec uteReader(); while (dr.Read()) { Response.Write( GetDataFor("mea nTemp ").ToString () + "<br>"); } dr.Close(); objConnection.C lose(); }
-- 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(stri ng 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(objec t sender, EventArgs e) { string strConnection = ConfigurationMa nager.Connectio nStrings["strConnMyD b"].ConnectionStri ng; SqlConnection objConnection = new SqlConnection(s trConnection); string strSQL = "SELECT meanTemp FROM tblWeather WHERE (Year(obsDate)= 2004) ORDER BY obsDate;"; SqlCommand objCommand = new SqlCommand(strS QL, objConnection); objConnection.O pen(); SqlDataReader dr = objCommand.Exec uteReader(); while (dr.Read()) { Response.Write( GetDataFor("mea nTemp ").ToString () + "<br>"); } dr.Close(); objConnection.C lose(); }
public double GetDataFor(stri ng column) { double result = (dr.IsDBNull(dr .GetOrdinal(col umn))) //ERROR: The name 'dr' does not exist in the current context ? 9999 : double.Parse(dr[colum].ToString()); return result; }
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 .CloseConnectio n and
parameterized queries (regarding hard coding 2004).
the entire code should read:
protected void Page_Load(objec t sender, EventArgs e)
{
string strConnection =
ConfigurationMa nager.Connectio nStrings["strConnMyD b"].ConnectionStri ng;
SqlConnection objConnection = new SqlConnection(s trConnection);
string strSQL = "SELECT meanTemp FROM tblWeather WHERE
(Year(obsDate)= 2004) ORDER BY obsDate;";
SqlCommand objCommand = new SqlCommand(strS QL, objConnection);
objConnection.O pen();
SqlDataReader dr = objCommand.Exec uteReader();
while (dr.Read()) {
Response.Write( GetDataFor(dr, "meanTemp ").ToString () + "<br>");
}
dr.Close();
objConnection.C lose();
}
public double GetDataFor(SqlD ataReader dr, string column)
{
double result = (dr.IsDBNull(dr .GetOrdinal(col umn)))
? 9999 : double.Parse(dr[colum].ToString());
return result;
}
"Web learner" <be******@learn ing.edu> wrote in message
news:ei******** *****@TK2MSFTNG P04.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.
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*********@Ho tmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP04.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 .CloseConnectio n and parameterized queries (regarding hard coding 2004).
the entire code should read:
protected void Page_Load(objec t sender, EventArgs e) { string strConnection = ConfigurationMa nager.Connectio nStrings["strConnMyD b"].ConnectionStri ng; SqlConnection objConnection = new SqlConnection(s trConnection); string strSQL = "SELECT meanTemp FROM tblWeather WHERE (Year(obsDate)= 2004) ORDER BY obsDate;"; SqlCommand objCommand = new SqlCommand(strS QL, objConnection); objConnection.O pen(); SqlDataReader dr = objCommand.Exec uteReader(); while (dr.Read()) { Response.Write( GetDataFor(dr, "meanTemp ").ToString () + "<br>"); } dr.Close(); objConnection.C lose(); }
public double GetDataFor(SqlD ataReader dr, string column) { double result = (dr.IsDBNull(dr .GetOrdinal(col umn))) ? 9999 : double.Parse(dr[colum].ToString()); return result; }
"Web learner" <be******@learn ing.edu> wrote in message news:ei******** *****@TK2MSFTNG P04.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.
Here is an example with my suggestions.
protected void Page_Load(objec t sender, EventArgs e)
{
string strConnection =
ConfigurationMa nager.Connectio nStrings["strConnMyD b"].ConnectionStri ng;
// using means you do not have to explicitly close or dispose the connection
using (SqlConnection objConnection = new SqlConnection(s trConnection) )
{
string strSQL = "SELECT meanTemp FROM tblWeather WHERE
(Year(obsDate)= @TargetYear) ORDER BY obsDate;";
SqlCommand objCommand = new SqlCommand(strS QL, objConnection);
// @TargetYear value could be obtained from a form field or querystring
// Validation of parameters should also be performed here.
objCommand.Para meters.AddWithV alue(@TargetYea r, "2004");
// CommandBehavior .CloseConnectio n means you don't have to remember to close
the Reader
SqlDataReader dr =
objCommand.Exec uteReader(Comma ndBehavior.Clos eConnection);
while (dr.Read()) {
Response.Write( GetDataFor(dr, "meanTemp ").ToString () + "<br>");
}
}
}
"Web learner" <be******@learn ing.edu> wrote in message
news:uK******** ******@TK2MSFTN GP03.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*********@Ho tmail.com> wrote in message news:%2******** ********@TK2MSF TNGP04.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 .CloseConnectio n and parameterized queries (regarding hard coding 2004).
the entire code should read:
protected void Page_Load(objec t sender, EventArgs e) { string strConnection = ConfigurationMa nager.Connectio nStrings["strConnMyD b"].ConnectionStri ng; SqlConnection objConnection = new SqlConnection(s trConnection); string strSQL = "SELECT meanTemp FROM tblWeather WHERE (Year(obsDate)= 2004) ORDER BY obsDate;"; SqlCommand objCommand = new SqlCommand(strS QL, objConnection); objConnection.O pen(); SqlDataReader dr = objCommand.Exec uteReader(); while (dr.Read()) { Response.Write( GetDataFor(dr, "meanTemp ").ToString () + "<br>"); } dr.Close(); objConnection.C lose(); }
public double GetDataFor(SqlD ataReader dr, string column) { double result = (dr.IsDBNull(dr .GetOrdinal(col umn))) ? 9999 : double.Parse(dr[colum].ToString()); return result; }
"Web learner" <be******@learn ing.edu> wrote in message news:ei******** *****@TK2MSFTNG P04.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.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 instance
(in this case 'myDog'). Of course it would be better if I could
somehow know from within write() that the name of the object instance
was 'myDog' without having to pass it as a parameter.
////////////////////////////////
function...
|
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:
string Total_Dollar_Amount = GetTotal(sql,"test");
namespace SalesReport
|
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" statement). Below is my
ASP code:
<%
Set OBJdbConn = Server.CreateObject("ADODB.Connection")
|
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 own but when I have it
as user control I get the error "Object reference not set to an
instance
of an object. " what am I doing wrong?
Moe <><
|
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 present."
BUT when I manually run the Stored Procedure and manually type in '3333' it
works just fine by returning the single row for that particular work
order????????
*****More Clues****
| |
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;
|
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 such a customized dictionary
thingie an instance dictionary, and get some custom
namespace behavior out of that.
... __: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/465748
... __: http://aspn.activestate.com/
|
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 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...
|
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 4 columns in this table as follow:
Column1 | Column2 | Column3 | Column4 |
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |