C# Hashtable "object reference not set" - c#

I have an interesting problem that I am trying to solve. I have the following code below which is basically a function for merging values with an html file and giving me the result set. I am using a hashtable for those purposes.
The function is as follows (Please bear in mind that I inherited this functionality and cannot be changed at present)
public static string ParseTemplate(string _FileName, int _NumberofSomething)
{
Hashtable templateVars = new Hashtable();
templateVars.Add("NameOfFile", _FileName);
templateVars.Add("NumberOfSomething", _NumberofSomething);
TemplateParser.Parser parser =
new TemplateParser.Parser(
System.Web.HttpContext
.Current.Server.MapPath("~/docs/Templatenr.htm"), templateVars);
return parser.Parse();
}
On our dev and live servers it is working perfectly. However I am trying to deploy the app to another production server and get an "object reference not set to an instance of object". It breaks exactly on the "Hashtable templateVars = new Hashtable();" line. So I am a bit puzzled. If it was a coding problem it should not work everywhere surely?
The only differences between the different production servers are the OS and IIS that is running. ie. Server 2005 and IIS7 vs Server2003 and IIS6 (which is the environment that it breaks on). Both have .net framework up to 3.5 installed. Could the older OS and ISS be the problem? Is it a maybe permission/memory thing? though that sounds a bit implausible since all the other .net functionality I am using on the new production server is working perfectly.
All the issues regarding the hashtable that I found, relates to it not being instantiated. However my error happens on the line that is trying to instantiate a Hashtable.
Has anyone had an error like this before and if so how did you solve it? I'd even appreciate suggestions on what to look for and I'll give it a try. I'll post back the outcome too, if anyone else is experiencing this error in future.

I never really got to the bottom of this error. Suggestions for using remote debugging with Reflector Pro did not work either, pointing to some issues with the virtual server the website was running in. Checking log files showed other errors that the .net framework threw out.
The website was moved to a new virtual server using IIS7 and windows server 2008. It worked as is on the new environment.
This is maybe not the ideal solution for everybody and maybe a framework re-install could have solved our problem, but since we were in the process of changing over to a new environment (for other reasons) this solution suited us.

Related

HttpContext.Current.Server.MapPath() returning inconsistent results

I'm hitting a strange problem with HttpContext.Current.Server.MapPath().
If I call
HttpContext.Current.Server.MapPath("/")
or
HttpContext.Current.Server.MapPath("~")
I get back "C:\GitHub\MyWebApp", which is what I'd expect. But if I call
HttpContext.Current.Server.MapPath("/folder")
or
HttpContext.Current.Server.MapPath("~/folder")
I'm getting "C:\folder". I would expect to get "C:\GitHub\MyWebApp\folder"
I don't understand how this could be going wrong.
I'm running .Net 4.6.1 and debugging on Windows using Visual Studio 2019, in case it makes any difference.
Afraid this one is a little embarrassing - a script had added a virtual folder in my IIS config and this was redirecting "folder" to "C:\folder". The responses were correct, they just confused me and I didn't spot the configuration before asking the question.
Thanks to anyone who spent time thinking about this one.

ExecutionEngineException with SQL and LINQ

My code is trying to load from a database using LINQ to SQL. At the moment as soon as i seem to touch what a query returns i get a crash with the error ExecutionExceptionError occured:
Currently my code is very simple, i have a standard dbml class that is meant to load a single table MC:
private void RibbonButton_Click(object sender, RoutedEventArgs e)
{
MultipleChoiceDataContext t = new MultipleChoiceDataContext();
var test =
from a in t.GetTable<MC>()
select a;
testbox.Content = test.First().question;
}
The error takes place on the line of
test.First().question;
I cannot find any help in the area or someone who has experienced this problem before, so i'm hoping someone may have an idea of how to fix this
ExecutionEngineException indicates an internal problem to the CLR. It means one of the following:
You used unsafe code or PInvoke to cause memory corruption. The CLR's data structures are now corrupt.
Some other library did this (a bug).
A bug in the CLR itself.
In all cases the solution is to work around the issue, maybe by changing things randomly until the application unbreaks. Try refactoring your code, comment out stuff until the error disappears. Factor things out into new methods. Think whether a library you are using might be doing unsafe stuff. Maybe the stack trace can tell you what functionality to avoid.
This is a deeply unmethodical approach but the best that I know of. Very hard to work around because you did nothing specific wrong. In particular, the line of code that you showed cannot normally cause memory corruption.
So turns out the problem was due to the installation of the preview of Visual Studio 2013, upon uninstalling that, the solution worked perfectly again on Visual Studio 2012
Thanks for all the help!

Exception from HRESULT: 0x80040013: when Integrating R with C#:

I am trying to interface R with C# web application.I tried many solutions being provided but couldn't get it through. Error I am getting is: Exception from HRESULT: 0x80040013
Steps I followed were:
Installing R 2.15.1 for windows xp (32 bit)
Installed R_Scilab_DCOM3.0-1B5
Loaded rscproxy_1.3-1 package in the R/R.15.1/library
Set the System Variable path to C:/Program Files/R/R.15.1/bin
Added 3 COM references-STATCONNECTORCLNTLib,StatConnectorCommonLib,STATCONNECTORSRVLib
Code:
private StatConnectorClass _statconnector = new STATCONNECTORSRVLib.StatConnectorClass();
_statconnector.Init("R");--> Line shows the Exception from HRESULT: 0x80040013
I have used the COM method to interface to c# some years ago, but I gave up because on each version change there were problems similar to that you mentioned. In addition, the licensing scheme of R(D)COM is a bit obscure to me, and the .NET/COM/R tour has too many corners.
My current choice is Rserve, which is well maintained and has never let me down. I had written my own test bed for c#, but I consider it obsolete because RserveCLI is more complete.
R.NET in theory is a better concept, but for strange reasons it is considered "stable" by the author, which is highly euphemistic because of the many bugs mainly in memory management. Too bad it never took off.
I believe R.NET is a much better way to access R functionality from C#. It all runs in-process and so doesn't require an external R process to be running.
If you install the NuGet package, you will get version 1.5. This incorporates some memory management fixes I made that address most of the crashes people have reported. As Dieter points out, we use R.NET extensively via the F# RProvider, and it is extremely stable with the latest version. The RProvider also comes with a wrapper generator that generates wrapper functions for R functions which can be used from C#. So for example you could call the plot function from C# as R.plot(...params...). And you get Intellisense.

calling new SqlConnection() hangs program

This one has me stumped. I'm not even trying to connect to a database. When this code gets to the line where I instantiate a new SqlConnection object, it just hangs there, not throwing an exception or anything. I've tried compiling it for 2.0. 3.5 and 4.0, and they all hang. Of course it works on my machine and yours, too. But I'm trying to run this code on a Windows Server 2008 x64 server, and it won't budge.
// example.cs
using System;
using System.Data;
using System.Data.SqlClient;
public class MainClass {
public static void Main(string[] args) {
Console.WriteLine("start");
SqlConnection conn = new SqlConnection(); // hangs here
Console.WriteLine("finish"); // never makes it here.
}
}
compilation (2.0):
c:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe example.cs
It's probably a broken performance counter which is the issue. I had this problem and I used Procmon to detect what happened. My .NET application hanged when it came to loading the registry keys for the performance monitor ".NET Data Provider for SqlServer"
I unloaded the counter with the following command to get it working:
unlodctr ".NET Data Provider for SqlServer"
Your installation have to be broken. The code doesn't really do anything vital at all, so there is no reason to hang there.
The SqlConnection constructor does this:
public SqlConnection() {
this.ObjectID = Interlocked.Increment(ref SqlConnection._objectTypeCount);
base();
GC.SuppressFinalize(this);
this._innerConnection = DbConnectionClosedNeverOpened.SingletonInstance;
}
So, it increases a variable, copies it into a property, calls the base constructor, removes the object from the finaliser queue, and copies a reference. That's all.
The base (DbConnection) constructor does this:
protected DbConnection() {
}
So, there is nothing in here that actually does anything at all related to an actual database connection. All that is done when you actually open a connection.
Your program might just as well be hanging after the first Console.WriteLine call, and not even get as far as creating the SqlConnection object.
Suggest 2 steps:
reset IIS to clear any connection pools. (Perhaps restart Windows?)
change the code to have a using statement:
public static void Main(string[] args) {
Console.WriteLine("start");
using (SqlConnection conn = new SqlConnection())
{
Console.WriteLine("middle");
}
Console.WriteLine("finish");
}
Can any other app from that machine make any other SqlConnection objects?
It's obviously an environmental problem, as your posted code will work on any other machine. Suspect that it's gone beyond some tipping point, and the using will help defend against this in the future.
I had the same problem and it began to work after I 1. Changed the target framework from 4.0 to 3.5, and 2. changed the debug settings to x64 in visual studio.
I had the exact same issue after upgrading a number of nuget packages. For me, the solution was:
In Visual Studio go to "Tools" --> "Options"
Search for "Web Projects" (under Projects and Solutions)
Check the "Use the 64 bit version of IIS Express for web sites and projects
I tried to unload the performance counter by executing the following command.
unlodctr ".NET Data Provider for SqlServer"
However, I got a message that the performance counter was not installed.
Therefore, I ran the following command to reinstall the performance counter.
lodctr "C:\Windows\INF\.NET Data Provider for SqlServer\_dataperfcounters_shared12_neutral.ini"
This resolved the application hang-up.
Solution found! I had the same problem on many PCs. Framework 4.5.2
Run this command as admin in CMD:
unlodctr ".NET Data Provider for SqlServer"
You may need to type it by hand as copy pasta doesn't work well with quotes.
Source:
http://askproblem.com/question/calling-new-sqlconnection-hangs-program/
I know this thread is old, but maybe someone else will get this error. It’s
probably a broken performance counter which is the issue. I had this problem
and I used Procmon to detect what happened. My .NET application hanged when it >came to loading the registry keys for the performance monitor “.NET Data
Provider for SqlServer” I unloaded the counter with the following command to
get it working: C: Windowsinf>unlodctr “.NET Data Provider for SqlServer”

The RPC server is unavailable while using EnvDTE Project

Firstly: This is an OFFLINE application.
OS: Windows 7 Professional N x64
Facts: Its a code generator. And after creating and referencing projects inside solution, i had to write all the classes out (based on what i read from database) to generate a DTO (for now) layer (still building DAL/BLL parts).
On my job it just runs fine, no problems at all, i can compile and debug, using Win 7 32 bits.
Well, but when i came back home today (with the project to resume my work) and tried to debug it here, what happen is:
this piece of code
myProj.ProjectItems.AddFromFileCopy(fPath);
myProj.Save(project.FullPath + "\\" + project.ProjectName+ ".csproj");
is the current prject, inside the created solution, and is responsible for adding the next recently created class to the project (its inside a foreach loop).
But suddenly, after having added a RANDOM number of classes, it throws an exception saying
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
Im totally lost. myProj is an EnvDTE.Project variable, that never has its own value changed. It'll always be the same value.
So, randomly it "crashes" throwing that exception. I did read something about it's a bug of VS 2008/2010, even cause fPath always has a value (the path of the file to be added within the project)
Any ideas, am i missing something or what?

Categories