Solved

compilation error - I was only trying to do the following

Posted on 2011-02-17
16
894 Views
Last Modified: 2012-08-13
      if (xConnName == "msSql")
        {
            SqlConnection conn = new SqlConnection(AceClasses.GetConnString());
            SqlCommand cmd = new SqlCommand(xSQL, conn);
            SqlDataReader myReader = cmd.ExecuteReader();
       }
        else
        {

            OleDbConnection conn = new OleDbConnection(AceClasses.GetConnString());
            OleDbCommand cmd = new OleDbCommand(xSQL, conn);
            conn.Open();
            OleDbDataReader myReader = cmd.ExecuteReader();
        }
        string strgWhere = " Nothing ";
        while (myReader.Read())
....
0
Comment
Question by:goodk
  • 7
  • 6
  • 2
  • +1
16 Comments
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 34920648
What is your error? What language are you using? Looks like C# do you have:

Using System.Configuration;
Using System.Data;
Using System.Data.SqlClient;

Open in new window

At the top of your source file?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34920897
At first glance you have a problem with this section:
            SqlConnection conn = new SqlConnection(AceClasses.GetConnString());
            SqlCommand cmd = new SqlCommand(xSQL, conn);
            SqlDataReader myReader = cmd.ExecuteReader();

Open in new window

As you are trying to execute the command before opening the connection.
0
 

Author Comment

by:goodk
ID: 34921505
well, the error is by poor design.  It is asking to declare myReader.  Apparently, thinking that the value will not be set.

In case of If... Else...

It would get set in either case.

Please try it on your VWD 2008 and let me know how to fix it?

thanks,
 
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34921596
You're declaring the reader inside the "if" block, so it doesn't exist outside of the block. Try changing your code to:
      System.Data.IDataReader myReader = null;

      if (xConnName == "msSql")
        {
            SqlConnection conn = new SqlConnection(AceClasses.GetConnString());
            SqlCommand cmd = new SqlCommand(xSQL, conn);
            myReader = cmd.ExecuteReader();
       }
        else
        {

            OleDbConnection conn = new OleDbConnection(AceClasses.GetConnString());
            OleDbCommand cmd = new OleDbCommand(xSQL, conn);
            conn.Open();
            myReader = cmd.ExecuteReader();
        }
        string strgWhere = " Nothing ";
        while (myReader.Read())

Open in new window

The poor design you'll have to sort yourself :)
0
 

Author Comment

by:goodk
ID: 34921754
still getting error,

ok, also
I have,  thanks

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Web.Caching;
using System.Web.SessionState;
using System.Web.Profile;
using System.Reflection;
using System.Web.Administration;
using System.Data.OleDb;



Compiler Error Message: CS0136: A local variable named 'myReader' cannot be declared in this scope because it would give a different meaning to 'myReader', which is already used in a 'parent or current' scope to denote something else

Source Error:

 

Line 190:            SqlConnection conn = new SqlConnection(AceClasses.GetConnString());
Line 191:            SqlCommand cmd = new SqlCommand(xSQL, conn);
Line 192:            SqlDataReader myReader = cmd.ExecuteReader();
Line 193:       }
Line 194:        else
 
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34921767
If you look at the code in my previous post, you will see that the line:
SqlDataReader myReader = cmd.ExecuteReader();

Open in new window

Should say:
myReader = cmd.ExecuteReader();

Open in new window

0
 

Author Comment

by:goodk
ID: 34922892
thanks, now I get this error -

Line 219:        if (xConnName != "msSql")
Line 220:            conn.Close();

I even tried this,
        if (conn.Open())
            conn.Close();
0
 
LVL 8

Assisted Solution

by:Anurag Agarwal
Anurag Agarwal earned 50 total points
ID: 34923721
xConnName is a name of connection name as i think which cannot directly be compared to a string type "mysql" ...........
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34923875
Where is xConnName delcared? Only the comaprison is shown in your code, not the actual declaration. Also, if oyu have an error you need to tell us what the error actually is, just the fact that there is an error doesn't tell us anything.
0
 
LVL 9

Assisted Solution

by:rawinnlnx9
rawinnlnx9 earned 50 total points
ID: 34927186
You are using myReader in more than one place. Try renaming the other instance of myreader to datareader or something. The "...different meaning" error means it's being used in 2 different places and 2 different ways. I have a feeling you might have just copied the suggested line of code up higher in your application and you should have MOVED it.

Just some ideas.
0
 

Author Comment

by:goodk
ID: 34931414
Just like you guys told me how to generalize myReader by using IDataReader.  Same way I need to know how to declare conn so I could use it for both connections.    

 public static string xSetScenario(string strScenario)
    {
        string xConnName = "msSql";
        System.Data.IDataReader myReader = null;


       string xSQL = "SELECT * from Scenario where ScenarioID='" + strScenario + "'";
       if (xConnName == "msSql")
        {
            SqlConnection conn = new SqlConnection(AceClasses.GetConnString());
            SqlCommand cmd = new SqlCommand(xSQL, conn);
//            SqlDataReader myReader = cmd.ExecuteReader();
            conn.Open();
            myReader = cmd.ExecuteReader();
        }
        else
        {

            OleDbConnection conn = new OleDbConnection(AceClasses.GetConnString());
            OleDbCommand cmd = new OleDbCommand(xSQL, conn);
            conn.Open();
            myReader = cmd.ExecuteReader();
//            OleDbDataReader myReader = cmd.ExecuteReader();
        }
        string strgWhere = " Nothing ";
        while (myReader.Read())
        {
            HttpContext.Current.Session["ScenarioID"] = myReader["ScenarioID"];
            HttpContext.Current.Session["BusinessStartYear"]= myReader["BusinessStartYear"];
            HttpContext.Current.Session["ReportingStart"]= myReader["ReportingStart"];
            HttpContext.Current.Session["ReportingEnd"] = myReader["ReportingEnd"];
            HttpContext.Current.Session["FilterString"] = myReader["FilterString"];
            strgWhere = AceClasses.gWhere(myReader["ScenarioID"].ToString(), myReader["BusinessStartYear"].ToString(), myReader["ReportingStart"].ToString(), myReader["ReportingEnd"].ToString(), myReader["FilterString"].ToString());
        }
        // always call Close when done reading.
        myReader.Close();
        // Close the connection when done with it.
     //  if (conn.Open())
            conn.Close();
        return strgWhere;        
    }
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34932629
All of the data objects implement the same interfaces, IDataReader for DataReader, IDbConnection for connection objects:
 public static string xSetScenario(string strScenario)
    {
        string xConnName = "msSql";
        System.Data.IDataReader myReader = null;
        System.Data.IDbConnection conn = null;

       string xSQL = "SELECT * from Scenario where ScenarioID='" + strScenario + "'";
       if (xConnName == "msSql")
        {
            conn = new SqlConnection(AceClasses.GetConnString());
            SqlCommand cmd = new SqlCommand(xSQL, conn);
//            SqlDataReader myReader = cmd.ExecuteReader();
            conn.Open();
            myReader = cmd.ExecuteReader();
        }
        else
        {

            conn = new OleDbConnection(AceClasses.GetConnString());
            OleDbCommand cmd = new OleDbCommand(xSQL, conn);
            conn.Open();
            myReader = cmd.ExecuteReader();
//            OleDbDataReader myReader = cmd.ExecuteReader();
        }
        string strgWhere = " Nothing ";
        while (myReader.Read())
        {
            HttpContext.Current.Session["ScenarioID"] = myReader["ScenarioID"];
            HttpContext.Current.Session["BusinessStartYear"]= myReader["BusinessStartYear"];
            HttpContext.Current.Session["ReportingStart"]= myReader["ReportingStart"];
            HttpContext.Current.Session["ReportingEnd"] = myReader["ReportingEnd"];
            HttpContext.Current.Session["FilterString"] = myReader["FilterString"];
            strgWhere = AceClasses.gWhere(myReader["ScenarioID"].ToString(), myReader["BusinessStartYear"].ToString(), myReader["ReportingStart"].ToString(), myReader["ReportingEnd"].ToString(), myReader["FilterString"].ToString());
        }
        // always call Close when done reading.
        myReader.Close();
        // Close the connection when done with it.
     //  if (conn.Open())
            conn.Close();
        return strgWhere;        
    }

Open in new window

0
 

Author Comment

by:goodk
ID: 34933206
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1502: The best overloaded method match for 'System.Data.SqlClient.SqlCommand.SqlCommand(string, System.Data.SqlClient.SqlConnection)' has some invalid arguments

Source Error:

 

Line 193:        {
Line 194:            conn = new SqlConnection(AceClasses.GetConnString());
Line 195:            SqlCommand cmd = new SqlCommand(xSQL, conn);
Line 196://            SqlDataReader myReader = cmd.ExecuteReader();
Line 197:            conn.Open();
 
0
 

Author Comment

by:goodk
ID: 34933829
realize all I am trying to do is be able to use either MSAccess or MSSQL in the same routine depending on the connection type.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 400 total points
ID: 34937084
ublic static string xSetScenario(string strScenario)
    {
        string xConnName = "msSql";
        System.Data.IDataReader myReader = null;
        System.Data.IDbConnection conn = null;
        System.Data.IDbCommand cmd = null;

       string xSQL = "SELECT * from Scenario where ScenarioID='" + strScenario + "'";
       if (xConnName == "msSql")
        {
            conn = new SqlConnection(AceClasses.GetConnString());
            cmd = new SqlCommand(xSQL);
            cmd.Connection = conn;
//            SqlDataReader myReader = cmd.ExecuteReader();
            conn.Open();
            myReader = cmd.ExecuteReader();
        }
        else
        {

            conn = new OleDbConnection(AceClasses.GetConnString());
            cmd = new OleDbCommand(xSQL);
            cmd.Connection = conn;
            conn.Open();
            myReader = cmd.ExecuteReader();
//            OleDbDataReader myReader = cmd.ExecuteReader();
        }
        string strgWhere = " Nothing ";
        while (myReader.Read())
        {
            HttpContext.Current.Session["ScenarioID"] = myReader["ScenarioID"];
            HttpContext.Current.Session["BusinessStartYear"]= myReader["BusinessStartYear"];
            HttpContext.Current.Session["ReportingStart"]= myReader["ReportingStart"];
            HttpContext.Current.Session["ReportingEnd"] = myReader["ReportingEnd"];
            HttpContext.Current.Session["FilterString"] = myReader["FilterString"];
            strgWhere = AceClasses.gWhere(myReader["ScenarioID"].ToString(), myReader["BusinessStartYear"].ToString(), myReader["ReportingStart"].ToString(), myReader["ReportingEnd"].ToString(), myReader["FilterString"].ToString());
        }
        // always call Close when done reading.
        myReader.Close();
        // Close the connection when done with it.
     //  if (conn.Open())
            conn.Close();
        return strgWhere;        
    }

Open in new window

0
 

Author Closing Comment

by:goodk
ID: 34948644
thanks, guys - it was a great help.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question