Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi friends i have to store text box value like 4 digits with check availability . i done some values like stared with 1..like 1000,1154,1001,etc but i can not store the values started with 0's like 0000,0120,0010,0001,etc...please help me..how to store these type's of values..i am waiting for your reply...thank you please help me............
Posted
Updated 30-Dec-15 18:20pm
v2
Comments
aarif moh shaikh 31-Dec-15 0:22am    
when you store 0000 than what store in your DB Table?
Member 10575434 31-Dec-15 0:30am    
i havet to store 4 digits of entered text box code..
Member 10575434 31-Dec-15 0:30am    
it will shows regular expression message (my message)
Member 10575434 31-Dec-15 0:31am    
regular expression is:validationexpression="^[1-9]{4}$"
Prasad Avunoori 31-Dec-15 1:47am    
What is the datatype of the column in the database?

1 solution

You could store the values in the database as a VARCHAR, then you will keep your formatting. However, this solution will probably come back and bite in the rear later on.
For example, what if you want to change to 5 or 6 digits?

You are confusing how values are stored with how values are presented on the screen for a user.

It is not a problem to use the datatype INT and store values like 1, 2, 3 etc.

The thing is how you present the value to a user. In this case you format the value to always show for digits left padded with zeros.

C#
value = 1;
Console.WriteLine(value.ToString("0000"));

or
C#
Console.WriteLine(String.Format("{0:0000}", value));

Both versions will result in the output '0001'
And in the future if you like to change from 4 to 5 digits you don't have to bother the data layer, just the presentation layer.

See Custom Numeric Format Strings[^] for more information.
 
Share this answer
 
Comments
_Asif_ 31-Dec-15 3:11am    
+5

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