Click here to Skip to main content
11,584,556 members (64,077 online)
Rate this: bad
good
Please Sign up or sign in to 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..
Posted 26-Apr-13 1:53am
Arifa S508
Edited 26-Apr-13 5:23am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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: http://msdn.microsoft.com/en-us/library/ms187594%28v=sql.90%29.aspx[^].

—SA
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 430
1 Sergey Alexandrovich Kryukov 367
2 Dave Kreskowiak 260
3 Richard Deeming 220
4 virusstorm 194
0 OriginalGriff 1,703
1 Sergey Alexandrovich Kryukov 1,267
2 Abhinav S 711
3 Dave Kreskowiak 687
4 Suvendu Shekhar Giri 568


Advertise | Privacy | Mobile
Web03 | 2.8.150707.1 | Last Updated 26 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100