Click here to Skip to main content
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 2:53am
Arifa S508
Edited 26-Apr-13 6: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 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 130
4 DamithSL 95
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 26 Apr 2013
Copyright © CodeProject, 1999-2014
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