Solved

compilation error - I was only trying to do the following

Posted on 2011-02-17
16
888 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 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

15 Experts available now in Live!

Get 1:1 Help Now