Click here to Skip to main content
15,888,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
dd.Id = Convert.ToInt32(txtid.Text);

if i execute the form in internet explorer,it is executing. but if enter the values for field given above it shows the error as below

Input string was not in a correct format.
Posted
Comments
thatraja 4-Dec-13 2:13am    
What values you're entering?

That means the String you are entering is not a valid integer.

You should go with int.TryParse[^].

This will not throw you exception. Rather it will try to parse the String, else it will not do anything.

So, do like...
C#
int num = 0;

bool res = int.TryParse(txtid.Text, out num);

if (!res)
{
    // String is not a number.
}
 
Share this answer
 
v2
Comments
Thanks7872 4-Dec-13 2:15am    
Clear enough. +5
Thanks a lot Rohan Leuva... :)
Member 10443519 7-Sep-16 7:12am    
Thank You Rohan
It looks like your 'txtid.Text' is a nullable value (or empty).You cannot convert a null value to an Int32. You need to ensure that each value is not null (or not empty).

Check below checking before the conversion.

C#
if (!string.IsNullOrEmpty(txtid.Text))
{
dd.Id = Convert.ToInt32(txtid.Text);
}
 
Share this answer
 
Comments
Member 10443519 7-Sep-16 7:13am    
Thank you Sampath
Might be you have added space at the end of the text,
Its always better to trim the value of TextBox

Instead of Convert.ToInt32 you can use TryParse

C#
int value = 0;
bool isInteger =   int.TryParse(btnshow.Text.Trim(), out value);
if (isInteger)
{
    // converted successfully
    // value contains the data which u entered in text box.
}
else
{
    // invalid entry, but it will not throw error.
    // here the value will be 0
}
 
Share this answer
 
Comments
Member 10443519 7-Sep-16 7:13am    
Thank You Karthik
Karthik_Mahalingam 7-Sep-16 7:53am    
"Thank you" after 3 years :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900