Hi
I am getting error - Unable to cast object of type 'System.Web.UI.WebControls.HiddenField' to type 'System.IConvertible'.
C#
HiddenField hdfDate = ((HiddenField)e.Item.FindControl("hdfDate"));Code
' />Thanks
Hi
I am getting error - Unable to cast object of type 'System.Web.UI.WebControls.HiddenField' to type 'System.IConvertible'.
HiddenField hdfDate = ((HiddenField)e.Item.FindControl("hdfDate"));' />Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Mohamed Azarudeen ZRank #297 · 8.5K reputationPosted May 28, 2023, 12:49 PM EDT
Hi ramco
You can modify your code as follows:
HiddenField hdfDate = e.Item.FindControl("hdfDate") as HiddenField;By using the
askeyword, you attempt to perform the cast, and if the cast fails, the variablehdfDatewill be assigned a null value instead of throwing an exception.Make sure to update your code accordingly and try running it again. This should resolve the casting issue you're experiencing.
Ramco RamcoRank #462 · 5.4K reputationPosted May 28, 2023, 12:58 PM EDT
Hi Mohammed
I am getting error on below line. Valie in hdfDate is "03/06/2023 00:00:00"
Value in GrpSessionStartDate is 20/05/2023 00:00:00
if (Convert.ToDateTime(hdfDate) < objGrpResult.GrpSessionStartDate)When i try below line then also gets error -
DateTime sDate = DateTime.ParseExact(hdfDate.Value, "yyyy/MM/dd HH:mm:ss",
CultureInfo.InvariantCulture);
Thanks
Vishal JoshiRank #377 · 6.7K reputationPosted May 30, 2023, 1:31 AM EDT
Hello Ramco
Please try DateTime.Compare() to get the expected result.
int res = DateTime.Compare(hdfDate, objGrpResult.GrpSessionStartDate);