Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
int rows = Convert.ToInt32(txtrows.Text.ToString());
txtrows is the id of a text box..
when the textbox is empty,the an error accour (your input string is not in correct format ) .. if the text is not empty then is does not show any error..
any solution ??
Posted
Comments
Simon_Whale 23-Sep-14 11:37am    
There is also no need to do txtrows.Text.ToString() as the Text property is always a string

some further reading for you http://msdn.microsoft.com/en-us/library/a19tt6sk(v=vs.110).aspx

Why not use the full power of the 'TryParse operator:
C#
// this code requires:
using System.Globalization;
//
// note: prior to .NET 4 an invalid culture specification string would
// throw an 'ArgumentException. in .NET 4 the exception thrown is
// a 'CultureNotFoundException
CultureInfo cultureProvider = new CultureInfo("en-US");

int rows;

if (Int32.TryParse(txtrows.Text, NumberStyles.Integer, cultureProvider , out rows))
{
    // valid integer now in 'rows
}
else
{
    // handle no content, or invalid content, in the TextBox
    throw new ArgumentException("Invalid entry in 'txtrow");
}
 
Share this answer
 
Comments
Philippe Mori 23-Sep-14 20:31pm    
Not so much usefull in practiceto use TryParse if you throw an exception anyway. You can simply call Parse in that case.
BillWoodruff 24-Sep-14 2:34am    
Thanks, Philippe, my intent was to indicate one possible thing ... throw an exception ... if the result of 'TryParse was 'false. I did not mean to imply that throwing was the only thing one could do :)

cheers, Bill
Raul Iloc 24-Sep-14 1:09am    
You have my 5+ vote, because you solution is managing also for multilanguage context where problems may occur for numbers and date time user input conversion.
BillWoodruff 24-Sep-14 2:34am    
Thanks, Raul !
How else? Did you expect that, if your string is empty, 42 would be returned? Or some other number?

Instead of Convert use either int.Parse (may throw exception as well), or int.TryParse (will return Boolean to indicate success). At least the names of these methods are adequate and tell us what really happens. Please see:
http://msdn.microsoft.com/en-us/library/system.int32.parse%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.int32.tryparse%28v=vs.110%29.aspx[^].

Please learn what all 6 methods do. :-)

—SA
 
Share this answer
 
You should use TryParse [^] and this method will convert the string to 0 for possible exceptions, and in your case for empty string.
 
Share this answer
 
This will use the default value (zero) of Int32 if the parse fails (for whatever reason):
int rows = 0;
Int32.TryParse (txtRows.Text, out rows);
/ravi
 
Share this answer
 
Comments
Philippe Mori 23-Sep-14 20:28pm    
Not necessary to initialize rows to 0 as it is an output parameter and output parameters need to be initialized Inside the function.
Ravi Bhavnani 24-Sep-14 7:59am    
It's just my coding style to perform explicit initialization, but you're right - it's unnecessary.

/ravi
Testing for null or empty is a good start e.g.

C#
if (!string.IsNullOrEmpty(txtRows.Text.ToString())
    rows = Convert.ToInt32(txtrows.Text.ToString())
else
    rows = 0;


You may also want to look at the int.TryParse() method.
 
Share this answer
 
v2

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