compilation error - I was only trying to do the following

      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())
....
goodkAsked:
Who is Participating?
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
rawinnlnx9Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
goodkAuthor Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
goodkAuthor Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
goodkAuthor Commented:
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
 
Anurag AgarwalCommented:
xConnName is a name of connection name as i think which cannot directly be compared to a string type "mysql" ...........
0
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
rawinnlnx9Commented:
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
 
goodkAuthor Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
goodkAuthor Commented:
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
 
goodkAuthor Commented:
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
 
goodkAuthor Commented:
thanks, guys - it was a great help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.