System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary

Last Reply 6 months ago By pandeyism

Posted 6 months ago

 

The given key was not present in the dictionary.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

Source Error: 


Line 12:             dic.Add(item.Split(":"c)(0), item.Split(":"c)(1))
Line 13:         Next
Line 14:         Dim customerId As String = dic("CustomerId")
Line 15:         Dim comments As String = dic("Comments")
Line 16:         Dim clas As String = dic("Class")

Source File: D:\Generic_Handler_JSON\simplesender.aspx.vb    Line: 14 

This is my code

Dim json As String = New System.Net.WebClient().DownloadString("http://192.168.1.239/Generic_Handler_JSON/HandlerVBEvents.ashx")
Dim count As String = json.Split(":"c)(1).Replace("}", "")
'  Dim json As String = File.ReadAllText(Server.MapPath("~/JSon.txt"))
Dim values As String() = json.Replace("""[{", "").Replace("}]""", "").Replace("""", "").Split(","c)
Dim dic As Dictionary(Of String, String) = New Dictionary(Of String, String)()
For Each item In values
    dic.Add(item.Split(":"c)(0), item.Split(":"c)(1))
Next
Dim customerId As String = dic("CustomerId")
Dim comments As String = dic("Comments")
Dim clas As String = dic("Class")

 This is the Json Data i am trying to get 

"[{""CustomerId"":""05-00001"",""Comments"":""Jana Sports Day Please Come Early"",""Dates"":""\/Date(1543525200000)\/"",""Status"":null,""Class"":""P1"",""Stream"":null,""Term"":null,""Years"":null}]"

 

Posted 6 months ago

I will get back soon.


Posted 6 months ago

Hey makumbi,

Please refer below sample.

Namespaces

C#

using System.IO;

VB.Net

Imports System.IO

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    string json = File.ReadAllText(Server.MapPath("~/JSon.txt"));
    string[] count = json.Replace("\"[{", "").Replace("}]\"", "").Replace("\"", "").Split(','); ;
    Dictionary<string, string> dic = new Dictionary<string, string>();
    foreach (var item in count)
    {
        dic.Add(item.Split(':')[0], item.Split(':')[1]);
    }
    string customerId = dic["CustomerId"];
    string comments = dic["Comments"];
    string clas = dic["Class"];
    Response.Write("CustomerId is <b>" + customerId + "</b><br/>comments is <b>" + comments + "</b>" + "</b><br/>comments is <b>" + clas + "</b>");
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim json As String = File.ReadAllText(Server.MapPath("~/JSon.txt"))
    Dim count As String() = json.Replace("""[{", "").Replace("}]""", "").Replace("""", "").Split(","c)
    Dim dic As Dictionary(Of String, String) = New Dictionary(Of String, String)()
    For Each item In count
        dic.Add(item.Split(":"c)(0), item.Split(":"c)(1))
    Next
    Dim customerId As String = dic("CustomerId")
    Dim comments As String = dic("Comments")
    Dim clas As String = dic("Class")
    Response.Write("CustomerId is <b>" & customerId & "</b><br/>comments is <b>" & comments & "</b>" & "</b><br/>comments is <b>" & clas & "</b>")
End Sub

Output

CustomerId is 05-00001 comments is Jana Sports Day Please Come Early comments is P1