This a bit of a weird one since I have never seen this before in C#. Today a user called that he couldn't run the program we are talking about here. The program in this case is a port from VB6 to .Net 4.6.1 and has been running flawlessly for more than a year. The user in question was a new colleague of us so some initial setup was added to the database.
The problem he got was in the routine below:
protected void Initialiseer()
{
//_logCallback("Initialiseer");
// Unieke code voor PC/process bepalen
CPcpid = Environment.MachineName + "_" + Process.GetCurrentProcess().Id;
LogMessage($"Unique identifier: {CPcpid}");
// De persoonlijke gegevens ophalen
var rst = SltlUserRepo.GetUserByPpUser(CUser);
var user = Ep3UserRepo.GetUserByPpUser(CUser);
// Failsafe for testing purposes only, cPrfId is 0 for 'koepelproef'
if (CPrfId != 0 && user.PRF_ID != CPrfId)
{
throw new InvalidOperationException($"Default proefId komt niet overeen met parameter {user.PRF_ID} != {CPrfId}");
}
var mSchijf = $@"\\WURNET.NL\Homes\{Environment.UserName}\";
CStartDir = Nz(user.STARTDIR, "").Replace(@"M:\", mSchijf).Replace(@"m:\", mSchijf);
CUserNaam = Nz(rst.NAAM, "");
CGewCode = Nz(rst.GEW_CODE, "");
CTltCode = Nz(rst.TLT_CODE, "");
CRegCode = Nz(rst.REG_CODE, "");
CJaar = Convert.ToInt16(Nz(rst.JAAR, ""));
...
The stacktrace for this error is the following:
2021-07-06 13:36:33.2309 Wur.Epros.ImportWaarnemingen.WinApp.Program.Main Error Unhandled Exception:
Error while building type Wur.Epros.Core.Models.ImportWaarnemingenModel. See the inner exception for details
1.) new ImportWaarnemingenModel()
2.) Wur.Epros.Core.Models.ImportWaarnemingenModel
3.) Instance of Wur.Epros.Core.Models.ImportWaarnemingenModel
4.) Container.GetInstance<Wur.Epros.Core.Models.ImportWaarnemingenModel>()
Stacktrace:
at lambda_method(Closure , IBuildSession , IContext )
at StructureMap.Building.BuildPlan.Build(IBuildSession session, IContext context)
at StructureMap.BuildSession.BuildNewInSession(Type pluginType, Instance instance)
at StructureMap.Pipeline.NulloTransientCache.Get(Type pluginType, Instance instance, IBuildSession session)
at StructureMap.BuildSession.ResolveFromLifecycle(Type pluginType, Instance instance)
at StructureMap.SessionCache.GetObject(Type pluginType, Instance instance, ILifecycle lifecycle)
at StructureMap.SessionCache.GetDefault(Type pluginType, IPipelineGraph pipelineGraph)
at StructureMap.Container.GetInstance[T]()
at Wur.Epros.ImportWaarnemingen.WinApp.FrmImportWaarnemingen..ctor() in E:\Jenkins\workspace\EPROS Nightly (.Net)\Wur.Epros\Wur.Epros.ImportWaarnemingen.WinApp\FrmImportWaarnemingen.cs:line 25
at Wur.Epros.ImportWaarnemingen.WinApp.Program.Main() in E:\Jenkins\workspace\EPROS Nightly (.Net)\Wur.Epros\Wur.Epros.ImportWaarnemingen.WinApp\Program.cs:line 34
Inner Exception
Input string was not in a correct format.
Stacktrace:
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Int16.Parse(String s, NumberStyles style, NumberFormatInfo info)
at System.Convert.ToInt16(String value)
at Wur.Epros.Core.Workers.WorkerBase.Initialiseer() in E:\Jenkins\workspace\EPROS Nightly (.Net)\Wur.Epros\Wur.Epros.Core\Workers\WorkerBase.cs:line 178
at Wur.Epros.Core.Models.ImportWaarnemingenModel..ctor() in E:\Jenkins\workspace\EPROS Nightly (.Net)\Wur.Epros\Wur.Epros.Core\Models\ImportWaarnemingenModel.cs:line 45
at lambda_method(Closure , IBuildSession , IContext )
The highlights are
In code at line 178:
CPcpid = Environment.MachineName + "_" + Process.GetCurrentProcess().Id;
In stacktrace:
at Wur.Epros.Core.Workers.WorkerBase.Initialiseer()
in E:\Jenkins\workspace\EPROS Nightly (.Net)\Wur.Epros\Wur.Epros.Core\Workers\WorkerBase.cs:line 178
From the first moment we saw this we did not believe the information given. It could never be that line. After some debugging and running on production (time consuming) we found the line below to be the problem:
CJaar = Convert.ToInt16(Nz(rst.JAAR, ""));
The routine 'Nz' which is an equivalent to the VB6 Nz routine returns 'vervanging' when 'obj' == null and that was exactly the case. In the DB the column JAAR held the value NULL. Of course empty string cannot be parsed to int.
public string Nz(object obj, string vervanging)
{
if (obj == null)
{
return vervanging;
}
return obj.ToString();
}
What I would have expected to see in the stacktrace is not line 178 but 198 which is the line of the bad code. It threw us off for a while. Can anyone explain why the stacktrace looks the way it does?