Click here to Skip to main content
14,693,263 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi To All!! I want Text Box to accept Only Integer value so made one dll of text box like costume control.Now my question is that is there any need to parse the value of text box.
Because when i write like this
cmd.Parameters.Add(new SqlParameter("@PRollno", int.Parse(txtrolnum.Text)));

it gives error "Input String not in a correct format"

so i did this
cmd.Parameters.Add(new SqlParameter("@PRollno", txtrolnum.Text));

now program is working properly,I just want to know what is the reason behind this. I am fresher in c#.
thanks in advaance.
But earlier the same code were run perfectly.In backend i used int datatype and allow not null value..
Updated 26-Apr-13 6:23am

If Int.Parse is returning "Input String not in a correct format", then there is a character (or characters) that are not correct for the current culture on the PC running the Paste operation. For example, the number in the textbox may use UK numbering: "1,234,567.89" where the PC culture is set to German or Greek: "1.234.567,89"

The computer running the SQL server may well be set to a different locale and interpret it correctly.

But why not have your custom control return a numeric value instead of a string? Just like the NumericUpDown control does? Then there is no need for conversion either way and confusion is removed. (Or use a NumericUpDown control instead)
Of course you should parse properly and take care about proper string format, take care of the culture you use, all of that.

But I think the problem is deeper and simpler. How come you store a string in your database, where the semantic requires to have a numeric type? I don't know why, but it looks like one problem really haunts the beginners these days: trying to work with strings representing data, instead of data itself. If you wan to work with numbers use the types like numeric, real, smallint, smallmoney and the like.

Please see:[^].


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