?
Solved

compilation error - I was only trying to do the following

Posted on 2011-02-17
16
Medium Priority
?
902 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 200 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 200 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 1600 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

764 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