1

This actually worked before but now, not.

string sqlConnString ="Server=server1;Database=Production;Trusted_Connection=true;User ID=User;Password=pwd"; 

        try {
            conn = new SqlConnection(sqlConnString );

And I get the exception. (I know the connection string should be in app.config but that didn't work, either.)

Ron
  • 43
  • 1
  • 7
  • 1
    What is the exception message _exactly_? Can you please be more specific? – Soner Gönül Mar 10 '15 at 13:00
  • Is the connection string still correct? – Drew Kennedy Mar 10 '15 at 13:03
  • type initializer for System.Data.SqlClient.SqlConnection threw an exception. If I change server to Data Source and Database to Initial Catalog, same error. – Ron Mar 10 '15 at 13:05
  • Use Namespace: `System.Data.SqlClient` for `SqlConnection` and add a reference to assembly: `System.Data` (in System.Data.dll) – andrei.ciprian Mar 10 '15 at 13:07
  • I did both: using System.Data.SqlClient; using System.Configuration; and there is a reference to System.Data; When I try to retrieve the connection string from app.config via this: ConfigurationManager.ConnectionStrings["Database"].ConnectionString; I get the error "Configuration System failed to initialize" which is suspiciously similar to the other error. – Ron Mar 10 '15 at 13:08
  • @Ron It seems there is error in you app.config post code for app.config. – Jenish Rabadiya Mar 10 '15 at 13:15
  • I had many errors; I used trusted connection and also a userID and pwd; in app.config, I had ConnectionString rather than connectionString. Now it all works. – Ron Mar 10 '15 at 13:25
  • possible duplicate of [Exception: type initializer for 'system.data.sqlclient.sqlconnection'?](http://stackoverflow.com/questions/6922879/exception-type-initializer-for-system-data-sqlclient-sqlconnection) – Jared Moore Mar 11 '15 at 20:16

2 Answers2

0

This most likely means that there is an error in your app.config file, e.g. badly formed XML or unexpected elements. The error happens because the static fields inside SqlConnection read from app.config to determine trace detail level as described here: http://msdn.microsoft.com/en-us/library/ms254503.aspx

(this question is a duplicate of Exception: type initializer for 'system.data.sqlclient.sqlconnection'?)

Community
  • 1
  • 1
Jared Moore
  • 3,765
  • 26
  • 31
-1

Remove the underscore between Trusted and Connection.

Doug Johnson
  • 558
  • 3
  • 9