Id Name farmdetailsdata
1 Test1 {"Product1":"A","Product2":"B"}
2 Test2
3 Test3 {"Product1":"C","Product2":"D"}
i am validating dict value is empty using below code
- int columnindex = 43;
- List<string> lst = new List<string>();
- foreach (DataRow row in dtFarmerFarmReports.Rows)
- {
- var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(row["farm_detailsdata"].ToString());
- if (dict != null)
- {
- lst.AddRange(dict.Keys);
- }
- }
- var distinctList = lst.Distinct().ToList();
- List<ExcelHeader> excelheaderlist = new List<ExcelHeader>();
- foreach (var data in distinctList)
- {
- columnindex++;
- worksheet.Cells[2, columnindex].Value = data;
- ExcelHeader excelHeader = new ExcelHeader
- {
- colIndex = columnindex,
- colName = data
- };
- excelheaderlist.Add(excelHeader);
- }
- foreach (var dicdata in dict)
- {
- int colval = excelheaderlist.Where(x => x.colName == dicdata.Key).Select(y => y.colIndex).FirstOrDefault();
- if (colval > 0)
- {
- worksheet.Cells[(j1), colval].Value = dicdata.Value;
- }
- }
from the above validating null value using dict. when i run the above i get error as follows
object reference not set to an instance of object. This error shows in below line as follows
foreach (var dicdata in dict).
how to fix this error. from my above code what changes i have to make.