Solved

compilation error - I was only trying to do the following

Posted on 2011-02-17
16
884 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
xConnName is a name of connection name as i think which cannot directly be compared to a string type "mysql" ...........
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks, guys - it was a great help.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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 …
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now