0

I'm trying to connect to a database, but it seems like my connection is not going through. I am using C# MVC for the webpage I'm creating. How can I fix the following error:

System.ArgumentException: 'Keyword not supported: 'metadata'.'.

The error is occuring on the line using (Sqlconnection con = new Sqlconnection(conStr)). What am I doing wrong on this line and is this how you call your SQL query in C# MVC?

string conStr = ConfigurationManager.ConnectionStrings["Training_DatabaseEntities"].ConnectionString;
List<FisYear> YerFis = new List<FisYear>();
using (SqlConnection con = new SqlConnection(conStr))
{
    SqlCommand cmd = new SqlCommand("select * from [dbo].[FiscalYear]", con);
    con.Open();
    SqlDataReader rdr = cmd.ExecuteReader();
    while(rdr.Read())
    {
        FisYear fy = new FisYear();
        fy.FisDate = rdr["ST_FI"].ToString();
        YerFis.Add(fy);
    }

    SelectList list = new SelectList(YerFis, "ST_FI", "FisDate");
    ViewBag.DropdownList = list;
}
Rob
  • 45,296
  • 24
  • 122
  • 150
Madonado
  • 63
  • 7

1 Answers1

1

You're almost certainly trying to use an entity framework connection string to open a connection via new SqlConnection, which won't work.

If you look in your web.config file you'll probably see something similar to:

<connectionStrings>
    <add name="Training_DatabaseEntities" connectionString="metadata=res://*/Entity.csdl|res://*.............provider=System.Data.SqlClient;provider connection string=............." />
</connectionStrings>

You could try parsing the connection string by hand to retrieve the bit you actually want, a brief web search suggests that the EntityConnectionStringBuilder may be of use to retrieve it programmatically, here's an example of doing that in a console app:

var connectionString = ConfigurationManager.ConnectionStrings["Training_DatabaseEntities"]
                       .ConnectionString;

var entityConnectionStringBuilder = new EntityConnectionStringBuilder(connectionString);
var sqlConnectionConnectionString = entityConnectionStringBuilder.ProviderConnectionString;

Console.WriteLine($"EF Connection String: {connectionString}");
Console.WriteLine($"SqlConnection Connection String: {sqlConnectionConnectionString}");

This gives the output (my emphasis):

EF Connection String: metadata=res:///Models.Model1.csdl|res:///Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=cntrra02-sql-rs;initial catalog=Training_Database;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"

SqlConnection Connection String: data source=cntrra02-sql-rs;initial catalog=Training_Database;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework

Which shows a connection string that can be passed into a SqlConnection instance.

Community
  • 1
  • 1
Rob
  • 45,296
  • 24
  • 122
  • 150
  • Am having a problem when it comes to this line, SqlCommand cmd = new SqlCommand("select * from [dbo].[FiscalYear]", con); it says can't convert system.data.core.entityclient to system.data.sqlclient. how can i fix that.. using (EntityConnection con = new EntityConnection(entityBuilder.ToString())) { con.Open(); SqlCommand cmd = new SqlCommand("select * from [dbo].[FiscalYear]", con); – Madonado Feb 26 '19 at 08:08
  • the link you provided it gives me a problem of converting entityclient to sqlclient, how can i solve that – Madonado Feb 26 '19 at 09:05
  • @Madonado, Short answer: You can't mix EntityConnection and SqlConnection. Long answer: You need to use something like the referenced `EntityConnectionStringBuilder` to extract the "plain" connection string and create a `SQLConnection`. If I can, I'll add an example of this to the answer later. – Rob Feb 28 '19 at 06:08
  • @Madonado, example/sample added :) – Rob Feb 28 '19 at 06:19