LifeOf0sAnd1s
LifeOf0sAnd1s

Reputation: 113

Keyword not supported: '"data source'. in MVC

I'm running into this error

([ArgumentException: Keyword not supported: ''data source'.])

in my MVC app as I try to run a standard SQL Query in my controller without using EF. I've read other articles which talk about escaping quotes but that has not been of any avail thus far. My Connection code is as follows:

userDataQry = -->Long SQL Query contained in this variable <---;
connString ="\"Data Source = data.testdomain.com; Initial Catalog = DashboardData; IntegratedSecurity = True; Application Name = DMetricsApp; \"providerName=\"System.Data.SqlClient\""; 

C# Sql Connection Code:

using(SqlConnection conn = new SqlConnection(connString))
{
    using(SqlCommand objCommand = new SqlCommand(userDataQry, conn))
    {
        objCommand.CommandType = CommandType.Text;
        DataTable dt = new DataTable();
        SqlDataAdapter sdp = new SqlDataAdapter(objCommand);
        conn.Open();
        sdp.Fill(dt);

        if (dt != null)
        {
            list = dt.AsEnumerable().ToList();
        }//End if
    }//End using
}//End using

Upvotes: 2

Views: 947

Answers (2)

Mad Myche
Mad Myche

Reputation: 1075

When you are passing a Connection String it is basically shorthand for what the SqlConnectionStringBuilder class would create; but a problem can occur as it may do a split when you are using spaces both between the properties and within the property name when you use the @ for a string literal

You could always do it the hard way:

string connstring = (new SqlConnectionStringBuilder() {
    DataSource = "data.testdomain.com;"
    , InitialCatalog = "DashboardData"
    , IntegratedSecurity = true
    , ApplicationName = "DMetricsApp"
    }).ToString();

Upvotes: 0

Steve
Steve

Reputation: 216293

Don't put double quotes at the beginning and at the end of the connection string

connString =@"Data Source=data.testdomain.com;
              Initial Catalog=DashboardData;
              IntegratedSecurity = True; 
              Application Name = DMetricsApp;";

Also remove the provider name part. It is not needed when you use the classes in System.Data.SqlClient

Upvotes: 1

Related Questions