Vishal Pandey
Vishal Pandey

Reputation: 460

get single element from list in C#

I have a list in which there are more than one items. Now I want to get first element's value of tRecordCount. I am trying, but getting an error System.FormatException: Input string was not in a correct format.

Can anyone tell me what is wrong in this code?

 ((HiddenField)GridViewPagingControl.FindControl("TotalRows")).Value = Convert.ToString(List.First(item => item.tRecordCount == Convert.ToInt32("tRecordCount")));

I have tried FirstOrDefault and Single too, but none is working. The return type of tRecordCount is int.

Thanks in Advance

Upvotes: 0

Views: 295

Answers (3)

Devraj Gadhavi
Devraj Gadhavi

Reputation: 3611

May be what you want is this.

((HiddenField)GridViewPagingControl.FindControl("TotalRows")).Value = Convert.ToString(List.First().tRecordCount);

Upvotes: 1

Cris
Cris

Reputation: 13351

you have problem at statement

Convert.ToInt32("tRecordCount")

correct syntax in

Convert.ToInt32("/*valid integer value*/")

if tRecordCount is variable then this statement should be

Convert.ToInt32(tRecordCount)

Upvotes: 1

Jon P
Jon P

Reputation: 821

What's wrong is:

Convert.ToInt32("tRecordCount")

You cannot convert string to int

Upvotes: 0

Related Questions