onTheInternet
onTheInternet

Reputation: 7253

Invalid parameters in SQLConnection

I have a .NET application that connects to a database.

In my web.config file I have

<connectionStrings>
    <add name="TFOUNDATION" 
         connectionString="Data Source=TAPOLCISQL01;Initial Catalog=TapolciFoundation;Persist Security Info=True;User ID=XXXX;Password=XXXXX" 
         providerName="System.Data.SqlClient"/>
</connectionStrings>

Username and password removed.

In my code behind I open the connection like this

protected void grabData()
{
    SqlCommand cmd = new SqlCommand("SELECT FirstName FROM CauseMarketers", new SqlConnection(ConfigurationManager.ConnectionStrings["TFOUNDATION"]));
}

The error I'm receiving is

The best overload method match for "System.Data.SQLClient.SQLConnection.SQLConnection(string)" has some invalid arguments.

I'm not sure what I'm doing wrong here.

Upvotes: 2

Views: 649

Answers (4)

Joeri
Joeri

Reputation: 225

Why would you want to use 'persist security info' ?

The ConnectionString is similar to an OLE DB connection string, but is not identical. Unlike OLE DB or ADO, the connection string that is returned is the same as the user-set ConnectionString, minus security information if the Persist Security Info value is set to false (default). The .NET Framework Data Provider for SQL Server does not persist or return the password in a connection string unless you set Persist Security Info to true.

Source: MSDN Article about SqlConnection

This website has everything you need to know about connection strings, and which one to use and how connectionstrings.com, and this page is about the provider you're using System.Data.SqlClient.SqlConnection

Standard Security Server=myServerAddress;Database=myDataBase;User Id=myUsername; Password=myPassword;

So, try removing 'persist security info' and change 'initial catalog' to 'database'.

Upvotes: 0

Dreamweaver
Dreamweaver

Reputation: 1346

Just removing "Persist Security Info=True;" may fix the issue. Please try.

Upvotes: 0

Tim
Tim

Reputation: 4101

Whenever I open a database connection, I always get prompted to call ToString() on it.

Have you tried ConfigurationManager.ConnectionStrings["TFOUNDATION"].ToString();

Upvotes: 0

marc_s
marc_s

Reputation: 754388

You need to use the .ConnectionString property on the config you fetch:

protected void grabData()
{
    // use the .ConnectionString property to get the connection string!
    string connStr = ConfigurationManager.ConnectionStrings["TFOUNDATION"].ConnectionString;

    SqlCommand cmd = new SqlCommand("SELECT FirstName FROM CauseMarketers", new SqlConnection(connStr));
}

Upvotes: 7

Related Questions