Travis Heeter
Travis Heeter

Reputation: 14084

WebConfigurationManager is unrecognized

I'm a C#/MVC 4 noob in setting up connections. Until now, it's always been in place prior to me working on it, so I've never had to think about it. Now, however, I need to set up a secure connection to a database, and don't have any working examples to use as a guide.

I am currently trying to get the example found in the SqlCredential documentation to work, to no avail. They seem to have left a lot out in this implementation.

Is there perhaps a more complete guide to this that I should be aware of?

Here's my code:

// Web.config:
<connectionStrings>
    <add name="Con" connectionString="Initial Catalog=myDB;Server=\\Server_name" providerName="System.Data.SqlClient" />
</connectionStrings>

// SearchController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.SqlClient; // for SqlConnection & SqlCommand
using System.Configuration;
using System.Web.Configuration;
using System.Security;

Configuration config = Configuration.WebConfigurationManager.OpenWebConfiguration(Null);
ConnectionStringSettings connString = config.ConnectionStrings.ConnectionString[“Con”];

using (SqlConnection conn = new SqlConnection(connString.ConnectionString))
{
    SecureString pwd = new SecureString();
    pwd.Equals("PASSW0rd");
    pwd.MakeReadOnly();
    SqlCredential cred = new SqlCredential("my_user_id", pwd);

    SqlCommand comm = new SqlCommand("SELECT * FROM Search", conn);
    conn.Credential = cred;
    conn.Open();
    comm.ExecuteNonQuery();
    conn.Close();
}

There is an error on WebConfigurationManager:

'System.Configuration.Configuration' does not contain a definition for 'WebConfigurationManager'

There is an error on Null:

The name 'Null' does not exist in the current context

And an error on ConnectionString["Con"]:

'System.Configuration.ConnectionStringsSection' does not contain a definition for 'ConnectionString' and no extension accepting a first argument...

Also, is there a better place to put my password and username? In the example it uses text boxes to accept these as inputs, but in my situation they are already known and won't change.

Upvotes: 0

Views: 1860

Answers (2)

Travis Heeter
Travis Heeter

Reputation: 14084

I just wanted to add my final, working outcome in case someone else had this same problem:

After watching the first 3 videos of this great series on setting up .NET db connections, I was able to get a connection working. I'm not sure why the MS Documentation makes it overly-complex, but kudvenkat straightened me out:

// web.config   
<add name="MyDataConnection" 
     connectionString="data source=server_name; database=myDB; user id=my.user.iDB password=PASSW0rd"        
     providerName="System.Data.SqlClient" 
/>

// SearchController.cs
string CS = ConfigurationManager.ConnectionStrings["MyDataConnection"].ConnectionString;
using (SqlConnection connection = new SqlConnection(CS))
    {
        SqlCommand command = new SqlCommand("SELECT * FROM Search", connection);
        connection.Open();
        SqlDataReader rdr = command.ExecuteReader();
    }

Pointing out some important learning points:

  • the connection string doesn't have to be in that elaborate ConfigurationStringsSettings object, it can just be a string.
  • the password can be defined in the connection string in web.config
  • you don't need to use System.Web.Configuration to get the Configuration String from Web.config

Upvotes: 0

Patrick Hofman
Patrick Hofman

Reputation: 157098

It thinks you are looking for the WebConfigurationManager inside the System.Configuration.Configuration class, not the System.Configuration namespace, try giving the full namespace:

Configuration config = System.WebConfiguration.WebConfigurationManager.OpenWebConfiguration(null);

Also the Null is a typo, it should be null.

Upvotes: 1

Related Questions