CONVERSION ISSUE FROM A LABEL TO INT FOR AN ID KEY

Ashish Pandey

Total Post:128

Points:898
Posted by  Ashish Pandey
C# 
 889  View(s)
Ratings:
Rate this:

I got problem converting this car id key from a label. Here is the error I am receiving

Unable to cast object of type 'System.Web.UI.WebControls.HiddenField' to type 'System.Web.UI.WebControls.Label'.

Do you have any suggestion to avoid this error ?

Thanks

Label myCarId = (Label)item.FindControl("carId");

iCarId = Convert.ToInt32(myCarId);

Session["carId"] = iCarId;

  1. Manoj Bhatt

    Post:154

    Points:1086
    Re: conversion issue from a Label to int for an ID key

    It should be:

    HiddenField myCarId = (HiddenField)item.FindControl("carId");

    iCarId = Convert.ToInt32(myCarId.Value);

    Session["carId"] = iCarId;

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!