Reputation:
I need help solving this error.
This is the error {"The type initializer for 'System.Transactions.Diagnostics.DiagnosticTrace' threw an exception."}
I am using Access database and Platform for build configuration is Any CPU.
Sometimes the code runs well and sometimes it suddenly throws this exception. There are many similar questions asked here but none of them is solving my error.
This is my connection string
DbPath = @"d:\Ek.mdb";
ConnectionString =@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+DbPath+";User Id=admin;Password=;";
This is my function to open the connection
I am getting the error on Con.Open()
public bool OpenConnection()
{
if (Con == null)
{
Con = new OleDbConnection(ConnectionString);
}
else if ((Con.State == ConnectionState.Broken || Con.State == ConnectionState.Closed) && (Con.State!=ConnectionState.Open))
{
Con.Open();
Tx = Con.BeginTransaction(IsolationLevel.ReadCommitted);
return true;
}
else if (IsConnectionBusy())
{
throw new DataException("Connection Busy");
}
return false;
}
This how I Close the connection after executing queries
public Boolean CloseConnection()
{
if(IsConnectionBusy())
throw new DataException("Connection Busy");
if (Con.State==ConnectionState.Open)
{
Tx.Commit();
Con.Close();
return true;
}
return false;
}
public bool IsConnectionBusy()
{
switch (Con.State)
{
case ConnectionState.Connecting:
case ConnectionState.Executing:
case ConnectionState.Fetching:
return true;
}
return false;
}
I cannot find that why does it runs sometimes perfectly and then sometimes throws this exception.
Any help will be greatly appreciated. I have tried too much for solving this error but couldn't.
What I have noticed is at this line.
else if ((Con.State == ConnectionState.Broken || Con.State == ConnectionState.Closed) && (Con.State!=ConnectionState.Open))
The connection is in closed state
. When it is executing the Con.Open()
statement the Connection
state changes to Open
and after that this error is thrown.
Also while debugging the error is not caused. It is only caused when I don't put a breakpoint
at that place.
At Con.Open()
when I put a breakpoint and stop there for a few seconds only for the first time then there is no error thrown. After that if I disable the breakpoint and no error is thrown...!!!
Upvotes: 1
Views: 14494
Reputation: 31
I started getting this error on adding appSettings under configuration node which already had configSections..
InnerException stated that "Only one configSections
element allowed per config file and if present must be the first child of the root element." So I just changed my config node sequence. My config looks like this now.
<configSections>
<sectionGroup >
<section name />
</sectionGroup>
</configSections>
<appSettings>
<add key="xxx" value="true"/>
</appSettings>
appSettings has been moved after configSections. Although scenario seems to be different than the issue faced by the creator of this thread, Hope it helps someone..
Upvotes: 2
Reputation: 2005
I updated my NuGet packages which seemed to solve the issue for me connecting to a local SQLite database.
Upvotes: 0
Reputation: 8043
I recommend checking out this exception's InnerException
property.
I had this same error and, even though I was under unusual conditions, I thought it would still be useful to document the solution.
Context: simple console app needing to connect to MySql through the standard .NET connector (MySql.Data)
Context: the connectionStrings
in my app.config
was pointing to an external file by means of its configSource
attribute. Now, in .NET you can point to an external config file only if that file is in the same directory or lower in the hierarchy, not higher. The point is, my external file was of course in a directory higher in the hierarchy, so I chose an alternate way to read the connection string I was interested in, and having resumed working on that app after a few days, I didn't remember that my app.config
was broken.
That was the problem, apparently the connector (or some other ADO machinery) parses the app.config
even when you don't access it programmatically, hence this error. I was able to figure it out because I looked at the InnerException, that's why I suggested inspecting it.
Upvotes: 2
Reputation: 296
remove empty space from app.config .....its working fine for me...
Upvotes: 0
Reputation: 14899
There was something wrong with my App.Config file.
I deleted it and added a new one from a new project and it fixed it for me.
Upvotes: 4
Reputation: 1797
Could you please use below two method to open and close the Access db connection and try to build the program in x86 mode and see if the problem arises:
OleDbConnection _oleCon;
public Form1()
{
InitializeComponent();
_oleCon = new OleDbConnection();
}
bool OpenConnection()
{
try
{
string DbPath = @"D:\Ek.mdb";
_oleCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" + DbPath + ";";//ConfigurationSettings.AppSettings["dbConnectionString"];
_oleCon.Open();
return true;
}
catch
{
return false;
}
}
bool CloseConnection()
{
try
{
_oleCon.Close();
return true;
}
catch
{
return false;
}
}
Upvotes: 0
Reputation:
Changed the database to sqlite and everything is working very smoothly...
I think that in Access db there are some problems with the transactions.
It got resolved by changing the database. Still I am not sure what was causing that weird error.
Upvotes: 0