KeyNotFoundException (v2021)

This public forum is for user-to-user discussions of ASP.NET Maker. Note that this is not support forum.
Post Reply
lfernandes
User
Posts: 77

KeyNotFoundException (v2021)

Post by lfernandes »

Aspnetmaker generates the edit page from the selected table, however the error occurs when we include the following code in the Row Updated event:

table schema

 TABLE [dbo].[TAHubs](
	[IDHub] [int] IDENTITY(1,1) NOT NULL,
	[Regional] [smallint] NULL,
	[Hub] [nvarchar](30) NULL,
	[Gestor] [smallint] NULL,
	[Responsavel] [smallint] NULL,
 CONSTRAINT [PK_TAHubs] PRIMARY KEY CLUSTERED 
// Row Updated event
public void Row_Updated(Dictionary<string, object> rsold, Dictionary<string, object> rsnew) {
    //Log("Row Updated");
   string sql = "UPDATE TPPolos SET ";
    if (String.IsNullOrEmpty(Convert.ToString(rsnew["Regional"]))) {
    	sql += "IDRegional = NULL";
    } else {
    	sql += "IDRegional = " + Convert.ToString(rsnew["Regional"]);
    }
    if (String.IsNullOrEmpty(Convert.ToString(rsnew["Gestor"]))) {
    	sql += ",Gestor = NULL";
    } else {
    	sql += ",Gestor = " + Convert.ToString(rsnew["Gestor"]);
    }
    if (String.IsNullOrEmpty(Convert.ToString(rsnew["Responsavel"]))) {
    	sql += ",Responsavel = NULL";
    } else {
    	sql += ",Responsavel = " + Convert.ToString(rsnew["Responsavel"]);
    }
    sql += " WHERE IDHub = "+ Convert.ToString(rsnew["IDHub"]);
    Execute(sql);
}

Error:

An unhandled exception occurred while processing the request.
KeyNotFoundException: The given key 'IDHub' was not present in the dictionary.
System.Collections.Generic.Dictionary<TKey, TValue>.get_Item(TKey key)


MichaelG
User
Posts: 1111

Post by MichaelG »

Check if the field exists first. For example:

if (rsnew.ContainsKey("field") && ...) {
}


lfernandes
User
Posts: 77

Post by lfernandes »

The proposed solution only inhibits and avoids the error message, however it does not solve the problem, the issue is in editing the record in the Row_Updated event it does not recognize the primary key of the IDHub table in the Dictionary

Important:

  1. We verified the occurrence of this problem in other forms.
  2. We emphasize that this form was working and stopped working after updating the template.

Is there a possibility to restore an old version of the template? If yes how can we proceed?


MichaelG
User
Posts: 1111

Post by MichaelG »

To debug the codes, you should use VS2019 to open your project, add a break point to your codes and see why the problem field does not exist in the Dictionary.

Is there a possibility to restore an old version of the template? If yes how can we proceed?

There is no option to restore an old version of the template. You can however browse the template folder to select an older version of the template file. However, this is not fool proof and may cause unexpected error. Try at your own risk.


lfernandes
User
Posts: 77

Post by lfernandes »

Michael,

In the Row Updated event, an error occurs when we reference the Dictionary key, however we managed to get around the problem using the following reference taHubsEdit.IDHub.CurrentValue

Thanks in advance for your help and attention.


darkdragon
User
Posts: 150

Post by darkdragon »

If the value of a field is not updated, then is not present in rsnew dictionary. In this case, you should check if its presence.

The right way to do it is like this:

rsnew.ContainsKey("field") ? rsnew["field”] : rsold[“field”]

Post Reply