. Advertisement .
..3..
. Advertisement .
..4..
I’m new to C#, and I know the basic knowledge of Java, but I can’t get this code to work.
After running the page on a browser, I get the ”system.formatexception: ‘input string was not in a correct format.”’ error.
The following is what I tried:
MarkUP:
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Label ID="Label1" runat="server" AssociatedControlID="TextBox2" Text="Label"></asp:Label>
<asp:SliderExtender ID="SliderExtender1" TargetControlID="TextBox2" BoundControlID="Label1" Maximum="200" Minimum="100" runat="server">
</asp:SliderExtender>
Code Behind:
protected void setImageWidth()
{
int imageWidth;
if (Label1.Text != null)
{
imageWidth = 1 * Convert.ToInt32(Label1.Text);
Image1.Width = imageWidth;
}
}
I still get this error. Can someone give me some solutions.
The cause: The error indicates that there isn’t a valid integer in the string you’re attempting to parse for an integer.
And the problem is with line
Label1.Text
could either be int or not.Solution: You can fix this error by using
Int32.TryParse(value, out number)
instead.