W
Web learner
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;
}
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;
}