TypeInitializationException: The type initializer for 'clGeneral' threw an exception.

Okay Here is what I did, I have done this 2 previous projects and it worked flawlessly.  It works on my local machine, but when I deploy it throws the above exception.  this project and the last project are hosted by the same company and are set-up the same (I think - default)

I have a seperate class called clGeneral located in teh app_code directory. the clSecured is also located there.  The first class is nothing more than defining common variables like database connections and things like that.  The second I use to encode and decode items that I want to store.  It is like this,

1    Imports Microsoft.VisualBasic
2    Imports System.Configuration.ConfigurationManager
3    Imports System.Data
4    Imports System.Data.SqlClient
5   
6   
7    Public Class clGeneral
8   
9        Public Shared MailServerNamae As String = "localhost"
10       Public Shared wrapper As New clSecured("Key")
11       Public Shared DBConn As SqlConnection = New SqlConnection(ConnectionStrings("RealEstate").ConnectionString)
12  
13   End Class
14  
15  

Then I import this class and use the variables.   What is happening this time is that it throws the exception above.  But when I bring the declaration down to the page level it works fine.  2 questions, Why does it work locally and not remotely.  I have even wiped the app_code clean and reloaded.  Why does it work when I move the DIM down to page level code behind as in DIM wrapper as new clSecured("Key").  I guess 3 questions, why is the other working flawless and this one is a pain in the back side.

 
0
grbourque
4/29/2008 1:22:35 AM
asp.net.visual-studio-2008 3408 articles. 0 followers. Follow

5 Replies
557 Views

Similar Articles

[PageSpeed] 13

 Put a breakpoint in the constructor of clGeneral.

Also, are you aware of the .NET naming conventions?  http://msdn2.microsoft.com/en-gb/library/ms229002.aspx


Richard
0
rjcox
4/29/2008 7:00:37 AM

Actually I changed the name when I posted it here.  Don't know just not comfortable posting things like that in the open.  I do like the article.  Come from a different back ground and when learning .net I read naming like txtName, and btnSubmit and I have carried that over. 

What I did is took the declaration out of the shared class down to the individual page.  Even though I have seen it work before I decide to declare it each time.  Now one of things where the light switch went on last night was, the way that I had it, sqlconnection was being shared by everbody because that class was only initalized once.  So that could probably cause me problems with multiple persons are trying to access the site.  Now there will be a NEW sqlconnection that would be created, opened and disposed of at the page level.

 

 

 

0
grbourque
4/29/2008 2:06:12 PM

 

grbourque:
sqlconnection was being shared by everbody because that class was only initalized once.  So that could probably cause me problems with multiple persons are trying to access the site.

It certainly would create problems.

grbourque:
Now there will be a NEW sqlconnection that would be created, opened and disposed of at the page level.
 

That is the right approach.

ADO.NET connection pooling will ensure that connections are reused rather than being totally destroyed and re-created (expensive) in a safe manner.

Ensure you are using using blocks, or calling Dispose once you have finished with the connect to help this happen. 


Richard
0
rjcox
4/29/2008 2:38:40 PM

Web applications are new to me and present a new learning curve that has been frustrating at times.  can you point me to ADO.NET connection pooling article.  Is this the preferred manner, using sqlserver? 

What I am presently doing, declare the connection with a DIM in the button click event for example.  Open the connection, close the connection.  I was under the understanding that the connection was disposed of when the event finished.  I do dispose of the dataset, dataadaptors and was under the understanding that a datareader went away when the connection was closed. Am I totally out to lunch here? 

0
grbourque
4/29/2008 4:35:51 PM

grbourque:
can you point me to ADO.NET connection pooling article
 

It should be covered on MSDN, but I don't have a link to hand.

grbourque:
Open the connection, close the connection.  I was under the understanding that the connection was disposed of when the event finished.
 

Correct.

The Dispose (or Close) method of the connect returns it to the pool (with a reset, in case you have changed its settings). 


Richard
0
rjcox
4/30/2008 6:57:28 AM
Reply:

Similar Artilces:

'Sybase.Data.AseClient.AseConnection.DriverVersion' threw an exception of type 'System.TypeInitializationException'
'Sybase.Data.AseClient.AseConnection.DriverVersion' threw an exception of type 'System.TypeInitializationException' This is an error which I am getting while instantiating AseConnection. Can I get help on this exception. I am using C# in Visual Studio 2005. I checked the version in the C:\Program Files\Sybase\OC12_5_1\ADO.NET\dll folder of Sybase.Data.AseClient.dll and sybdrvado11.dll and copied these dlls to my application's bin folder. The versions are the same. Still I am getting the above exception. Whats this code look like? Are you sure both DLLs are sa...

Cast from type ''''DBNull'''' to type ''''String'''' is not valid.
How do I avoid getting this error: Cast from type ''''DBNull'''' to type ''''String'''' is not valid. I am trying to get values from the database into a form for updating using a Datareader, SQL is set to allow nulls for certain fields as they are not required but the only way I can get the form to display is by adding a space in SQL. Is there another way around this? Check if it is equal to DBNull.Value first and if it is, don't do the cast.Stanley Tan theSpoke Blog Where in the code does it need to go? I am using the following and it's the profile that isn't always requi...

The type initializer for 'tags' threw an exception??
Hello I get the following error...  The type initializer for 'tags' threw an exception. Does anyone who what this mean, or what might be causing this? Heres the code on the page giving the error...protected void Page_Load(object sender, EventArgs e) { WebUsers user = new WebUsers(); if (!this.IsPostBack) { if (Session["user"] != null) { user = (WebUsers)Session["user"]; } else { Response.Redirect("Login.aspx"); ...

'ds.SqlCacheDependency' threw an exception of type 'System.NotSupportedException'
Hi guys, As a follow up on my previous question: http://forums.asp.net/t/1336003.aspx I would like to know if any of you have an answer to the problem with the SqlCacheDependency throwing an System.NotSupportedException when trying to update using an AccessDataSource? Thanks :) /Ole   Since the SqlCacheDependency is not supported by Access, any attempt to make use of it through an AccessDataSource control will result in a NotSupportedException: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.accessdatasource.sqlcachedependency.aspx.  The mystery is why ...

The type initializer for 'BLL.Customer' threw an exception.
What does this error mean??? No clue.........   In .NET, you can have instance constructors and you can also have a static constructor. The static constructor is used to initialize static variables or do other one-time setup for all instances of a class, usually. Static constructors are also called type initializers, and will be called before the first instance of the class is constructed (it appears to usually happen when you first call a method that needs to know about the type). To see the code that is throwing an exception, look at the code for the class mentioned. In C#, look f...

Length = 'requestStream.Length' threw an exception of type 'System.NotSupportedException'
 I cannot figure out why when I expand the requestStream variable that I see that error.  Here's my code:   ... request.Timeout = 30000; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; // write data to stream byte[] requestBytes = Encoding.UTF8.GetBytes(request.ToString()); using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(reque...

Unit test causes exception "The type initializer for 'XXX' threw an exception" when running code which accesses appSettings of the webconfig
I have a method which I want to unit test, the code inside that method accesses the appSetting to get the connectionString : System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"].ToString(); this piece of code works fine , but when  I m doing the unit test to test the method, the above line gives the below exception :MyMethod threw exception:  System.TypeInitializationException: The type initializer for 'XXX' threw an exception. --->  System.NullReferenceException: Object reference not set to an instance of an object..  ...

The type initializer for 'System.Drawing.ToolboxBitmapAttribute' threw an exception
Hi,All of a sudden I started getting this nasty exception all over the place:The type initializer for 'System.Drawing.ToolboxBitmapAttribute' threw an exception. Some relevant observations:* The error started occuring a few hours after the last deployment to the live server, not immediately after. So it basically started occuring of its own accord.* We have recently moved to a new host (a VPS). The error only occurs on that server, not our local server nor at the old host's (which was a shared environment).* The problem is basically the same as this guys:http://w...

The type initializer for 'System.Drawing.ToolboxBitmapAttribute' threw an exception.
I get this stacktrace at my ExceptionManager. It is caught in the Global.asax, so the exception is unhandled by my code :( We have many visitors and customers. This error occurs at least once a week, and all websites in the same pool are down. Only solution for now is to reset the pool when the error occurs. I know there are more topics on this, and I know MicroSoft is aware of the bug but does not know what to do about it. The bug will still be in the 3.5 release. If anybody has a work around for the fix, or a solution which is acceptable and avoids my customers and visitors ...

Cannot convert type 'type' to 'type'
I am learning all of this, so I am still fresh, so please bear with me :)I have a page that has some txtboxes in it, simple registration form, and I have a button that will save those txt boxes to the db. I am using:- I am using Visual Web Developer 2008 Express Edition- C# When I try to compile the project I get error: Cannot convert type 'System.Web.UI.WebControls' to 'String' This is content of my web.config    <profile> <properties> <add name="FirstName"/> <add name="LasttName"/>...

The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception.
 Hi all,When i run my project on my own computer, it works fine. But when i deploy it to server, it gives me this error. I can run my other dts and sp, but i get this error on this.. The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception. I googled it but could not find a solution. How can i solve this problem?  Hello sourvil, I'm not sure what the problem is. The following thread explains how you can find out more about the error that is reported: http://blogs.infosupport.com/martijnb/archive/2006/01/27/3582.aspx Jeroen Molenaar.----...

The type initializer for 'CrystalDecisions.CrystalReports.Engine.ReportDocument' threw an exception.
Hi, We have an application developed using VS2002 and .NET 2002 Crystal reports. when this application is deployed on Win 2003 32 bit machine  it worked fine, but when deployed on windows 2003 64 bit machine, crystal reports are not opened. Replaced .NET 2002 Crystal reports with crystal reports 11. Problem still persists. It is giving exception as "The type initializer for 'CrystalDecisions.CrystalReports.Engine.ReportDocument' threw an exception.".   Can anyone help me on this.   Hi there, Me too, I encounter the same problem....

The type initializer for 'IBM.Data.DB2.DB2Connection' threw an exception.
Hello,I am using UDB  Database ( IBM DB2 9.1) with ASP.Net. I am inserting data in DB2 database!When I run my web site in Visual Studio 2008, I can connect with db2 database and successfully insert data!But when I deploy my web site locally in my IIS(After creating Virtual Directory) I receive following Error!   The type initializer for 'IBM.Data.DB2.DB2Connection' threw an exception. I uninstall/install Db2 Connect server 9.1, so many time but I am unable to find error!Please if you can help me!Thanks! EME Make sure you shut off VS.NET (no devenv.exe in t...

The type initializer for 'Oracle.DataAccess.Client.OracleConnection' threw an exception
I have a VB .Net 2.0 web application which accesses oracle 8i database, the database was upgraded to 9i later on. The application worked fine just before the upgrade was made, but after the upgrade the application gives the following error. The type initializer for 'Oracle.DataAccess.Client.OracleConnection' threw an exception. I did a few searches in the oracle forums and downloaded and installed the Oracle Data Provider for .NET  version 9.2.0.4 form orcale website but the application still throws out the above exception. following is the stack trace of the application...

Web resources about - TypeInitializationException: The type initializer for 'clGeneral' threw an exception. - asp.net.visual-studio-2008

Resources last updated: 1/10/2016 3:47:30 AM