Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
?xml version="1.0"encoding="utf";
<configuration>
<startup>
<supportedruntime version="v4.0" sku=".NETFramework,Version=v4.5">

<configsections>


<connectionstring>
<add name="ConnectionString" providername="System.Data.SqlClient" connectionstring="server=CTSINTBMC;database=One995;Integrated Security=SSPI;Persist Security Info=False;Max pool size = 20;Min pool size = 0">



in console application code as follows

SqlConnection sqlConnection = new SqlConnection("ConnectionString");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
cmd.commandtext = "select * from customers";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection;
sqlConnection.Open();
reader = cmd.ExecuteReader();
sqlConnection.Close();


when i execute the above code shows error as follows

The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception.

the following error shows in below line as follows

SqlConnection sqlConnection = new SqlConnection("ConnectionString");

please help me what is the problem in my above code

What I have tried:

?xml version="1.0"encoding="utf";
<configuration>
<startup>
<supportedruntime version="v4.0" sku=".NETFramework,Version=v4.5">

<configsections>


<connectionstring>
<add name="ConnectionString" providername="System.Data.SqlClient" connectionstring="server=CTSINTBMC;database=One995;Integrated Security=SSPI;Persist Security Info=False;Max pool size = 20;Min pool size = 0">



in console application code as follows

SqlConnection sqlConnection = new SqlConnection("ConnectionString");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
cmd.commandtext = "select * from customers";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection;
sqlConnection.Open();
reader = cmd.ExecuteReader();
sqlConnection.Close();


when i execute the above code shows error as follows

The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception.

the following error shows in below line as follows

SqlConnection sqlConnection = new SqlConnection("ConnectionString");

please help me what is the problem in my above code
Posted
Updated 12-Aug-16 2:17am
Comments
Nathan Minier 12-Aug-16 7:48am    
So....are you passing a real connection string to the constructor?

Also, please dispose of things properly!

Have a look at:
http://www.codeproject.com/Articles/837599/Using-Csharp-to-connect-to-and-query-from-a-SQL-da
Rob Philpott 12-Aug-16 8:06am    
This is an extremely strange error to receive. The type initializer is the static constructor of the class which suggests something has gone wrong internally within System.Data.

Can you post the full exception, including any inner exceptions to reveal any more info?
ZurdoDev 12-Aug-16 8:18am    
There should be more to the error that will tell you exactly what the problem is.

1 solution

C#
hey it may be the problem with app.config settings.

u hv to code like this

 

<appSettings>

<add key ="ConnectionString" value="Data Source=server name;Initial Catalog=db name;User ID= sa;Password= sa"/>

</appSettings>

 

if u code this wrongly ,typeintializer error w'll throw.

Also if the key name is used different from app.config also exception will throw.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900