Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi All,
I m trying to execute the following code in text box enter event

int id;
id = Convert.ToInt32(txtRollNumber.Text);
SqlConnection Con = new SqlConnection(@"Data Source=HORTON-2A5CB16E\SQLEXPRESS;Initial Catalog=Temp;Integrated Security=SSPI;Max Pool Size=100;Connect Timeout=1000");
Con.Open();

but in run time it is showing id = Convert.ToInt32(txtRollNumber.Text); input string is not in correct format
Please help

Thanks & Regards
Indrajit

[added code block for readability]
Posted
Updated 12-Apr-11 20:29pm
v2
Comments
walterhevedeich 13-Apr-11 2:30am    
if your textbox has no value, it will definitely error out.

This is what it is. The text in the text box txtRollNumber should be in integer format: all digits, optional minus, withing the valid range, etc. You need to validate it before converting. Instead of Convert, use int.TryParse(txtRollNumber.Text, out id); this will validate, assign right valued to id in case of success and return Boolean success status.

Use debugger to inspect txtRollNumber.Text before conversion is something is going wrong.

—SA
 
Share this answer
 
v2
Comments
m@dhu 13-Apr-11 2:38am    
This is it. My 5.
Sergey Alexandrovich Kryukov 13-Apr-11 2:41am    
Thank you.
--SA
Tarun.K.S 13-Apr-11 2:52am    
Совершенный! 5d!
Sergey Alexandrovich Kryukov 13-Apr-11 3:05am    
Wow! They finally put charset=utf-8 for a code; I was asking about it so long ago!
The site is certainly improving!
(Recently tried to write something in Chinese but it failed. :-)

Thank you very much for the vote, but...

The word is correct, but the usage is wrong. This word (="perfect", thank you very much) has tens of forms, this is a singular, male gender (matches male gender of the word "Answer" but not "Solution" (middle gender) on the base case (nominative)). Almost there, but in this gender the usage of single word is style stylistically wrong, sounds like Indian accent :-). I suspect it is impossible to learn all that. I can explain and fix each and every case (most Russian people cannot), but would not know how to teach it all.

By the way, the quality of automated translations from/to Russian is a source of permanent jokes. There are collections of the hilarious cases, especially Word "corrections". For example, the Russian word for "multichannel" was not found in the dictionary, the "correction" suggested by Word was: "anal animation". (I can explain why, the Cyrillic spelling of it is really close.) This is not a joke but a real story. When I read about that I checked it up -- exactly so.

--SA
Tarun.K.S 13-Apr-11 3:43am    
Ohkay, I think I will learn Russian now!
Lol conversions are quite tough to make considering Russian language.
Hi use this instead,

MIDL
int id = 0;
Int32.TryParse(txtRollNumber.Text, out id);
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Apr-11 2:36am    
Didn't I just said that? Maybe we did at the same time, my 5.
--SA
m@dhu 13-Apr-11 2:37am    
How does your answer make any difference with SA's?
Pong D. Panda 13-Apr-11 2:42am    
I posted my answer a little too late. Both our solution has the same principle.
Sergey Alexandrovich Kryukov 13-Apr-11 2:42am    
Less letters :-) May be it was at the same time?
--SA
IndrajitDasgupat 13-Apr-11 2:41am    
Now it is showing "implicitly convert type bool to int"
- check what value u have get on txtRollNumber.Text, it should not be empty

- Use TRIM() to omit the spaces
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 13-Apr-11 2:35am    
Trim (not TRIM()) is a good point.
--SA
Tarun.K.S 13-Apr-11 2:53am    
Indeed needed but won't solve the problem.

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