CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2009
    Posts
    7

    Cannot create connection object - SqlConnection type initializer throws an exception

    After a 4 year absence during which I was coding C# I've returned to a C++ application in order to add code to access a SQL Server 2008 R2 database. In order to get started using managed code in C++ I created a simple managed class and am trying to connect to the database. Any attempt to create a new connection object fails with the same error: type initializer for System.Data.SqlClient.SqlConnection threw an exception. I've simplified the code as much as I can and it still fails. I can connection to the database through VS2012's server explorer and I copied the connection string from the connection properties it creates but the program doesn't even get to the point of using the connection string. Here's the managed class:
    Code:
    using namespace std;
    using namespace System::Data;
    using namespace System::Data::SqlClient;
    using namespace System::Xml;
    namespace MySQLns
    {
    	public ref class MySQL
    {
    	public:
    	SqlConnection^ Connection;
    	SqlCommand^ Command;
    
    	bool MySQL::CreateConnection(System::String^ pConn)
    	{
    		SqlConnection^ conn = gcnew SqlConnection();
    		conn->ConnectionString = "<Valid Connection String>";
    		Connection = gcnew SqlConnection(pConn);
    		Connection->Open();
    		if (Connection->State == ConnectionState::Open)
    			return true;
    		else
    			return false;
    	}
    };
    }
    
    and the main (in another project):
    
    #using "MySQL.obj"
    using namespace MySQLns;
    int main()
    {
    	bool rslt;
    	MySQL sql;
    	System::String^ str("Valid Connection String");
    	rslt=sql.CreateConnection(str);
    	return 0;
    }
    I originally simply called the CreateConnection method with the connection string but when that failed I added a local connection with the default constructor just to make sure there weren't issues with the connection string. Even the simple statement

    SqlConnection^ conn = gcnew SqlConnection();

    fails with the same type initializer error. I've been searching the internet all day for a solution but can't find anything that applies to this basic circumstance. Most posts suggest problems with the connection string or the app.config file. There is no app.config file and there's no connection string involved so I'm hoping someone here can point me in the right direction.

    Thanks in advance!

  2. #2
    Join Date
    Dec 2009
    Posts
    7

    Re: Cannot create connection object - SqlConnection type initializer throws an except

    While I was waiting for someone to offer a solution I kept at it and after much searching of InnerExceptions finally stumbled upon the answer. Deep in the InnerExceptions was one that said "String cannot be of zero length. Parameter name TargetFrameworkAttribute." After digging into this I found a post somewhere that mentioned a file called .NETFramework,Version=v4.5.AssemblyAttributes.cpp as being created in C:\Users\<my user>\AppData\local\Temp. I looked at the file and, sure enough, the TargetFrameworkAttribute and FrameworkDisplayName were empty strings. I changed them to .NETFramework,Version=v4.5.AssemblyAttributes.cpp and the program now works correctly.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured