TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Narasiman nar
NA
64
22.1k
Validate dict value is empty in c#
May 28 2018 11:36 PM
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.
Reply
Answers (
1
)
how to validate dict in c#
Set up of win forms application with mysql database