0

UPDATE: Error was caused by a class named the OleDbConnection hiding the OleDbConnection constructors and therefore not presenting the overload method.

I am in the early stages of learning how to use ole database connections however I have run into a probem straight away. Despite there being an overload method to create an OleDbConnection connection it simply wont allow me to pass a connection string in.

Heres what I am trying.

private OleDbConnection _myConnection = null;

public bool CreateConnection()
{
    try
    {
        _myConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings["OracleDefault"].ConnectionString);
    }
    catch (Exception e)
    {
        throw new Exception(e.Message);
    }

    return true;
}

Now there doesn't seem to be anything wrong with that however I get an error saying that the method has 0 parameters but is invoked with 1 argument. I have seen countless examples showing it done in this way but for some reason it just won't let me do it. Any ideas?

UPDATE: Removed the semi colon as it wasnt supposed to be in the example I posted however this is not the issue. It simply won't accept any form of string.

CSharpened
  • 11,674
  • 14
  • 52
  • 86
  • Odd, that works for me. Please take a look at http://stackoverflow.com/questions/178456/what-is-the-proper-way-to-re-throw-an-exception-in-c . The "throw new Exception(e.Message);" is very bad practice as your will lose your stack trace (amongst other things). – DaveShaw Dec 01 '11 at 10:08
  • Thanks for the comment and link dave. The throw new exception would not be staying there it was just a quick type up to paste here. – CSharpened Dec 01 '11 at 10:10

3 Answers3

1

Just remove the ; after ["OracleDefault"].ConnectionString.

Shai
  • 25,159
  • 9
  • 44
  • 67
  • The ; was not supposed to be there. It was just a mistake in the example I typed into here. Even with this removed it still does not accept any parameters. – CSharpened Dec 01 '11 at 10:09
0

Use

_myConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings.["OracleDefault"].ConnectionString);

instead of

_myConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings.["OracleDefault"].ConnectionString;);

as the constructor contains an overload that require string.

FIre Panda
  • 6,537
  • 2
  • 25
  • 38
  • The ; was not supposed to be there. It was just a mistake in the example I typed into here. Even with this removed it still does not accept any parameters – CSharpened Dec 01 '11 at 10:09
0

UPDATE: Error was caused by a class named the OleDbConnection hiding the OleDbConnection constructors and therefore not presenting the overload method.

CSharpened
  • 11,674
  • 14
  • 52
  • 86