473,395 Members | 1,341 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Smart or stupid? Tying textbox length to database column length

Wondering what other people do about this issue...

You're writing a web app in asp.net that requires user input. Data will
be stored in SQL Server Express database. You want to make sure data
does not exceed maximum length for database column.

For example, "User Name" column in database is set to varchar(30), so
you would like to also set your textbox to a MaxLength of 30. To guard
against hackers, etc., you also want to set up a validator that checks
on submit to make sure User Name does not exceed 30 characters.

So, what is the "best practice" for this situation?

Seems like most people would just hard code the number 30 into their
..aspx page. This seems like a programming no-no, though. What if you
later decide to allow a User Name to be 50 characters long? Now you
have to change all the 30's to 50's. You might forget, you might miss
something, you might make a mistake, etc.

Another way to go: Write a stored procedure in SQL that returns the
max_length of a column given the name of the database, table, and
column (using the system view, "INFORMATION_SCHEMA.COLUMNS"). Then, you
place an algorithm in your code-behind that queries the database for
this information and automatically sets parameters for the textbox and
validation. Some problems with this: (1) takes some work to set up and
debug, (2) increases burden on database server.

Maybe there are other ways to do this I haven't even thought of. What
do you do?

Thanks,

-Dan

Apr 23 '06 #1
1 2110
"Dan Manes" <da******@cox.net> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
Wondering what other people do about this issue...

You're writing a web app in asp.net that requires user input. Data will
be stored in SQL Server Express database. You want to make sure data
does not exceed maximum length for database column.

For example, "User Name" column in database is set to varchar(30), so
you would like to also set your textbox to a MaxLength of 30. To guard
against hackers, etc., you also want to set up a validator that checks
on submit to make sure User Name does not exceed 30 characters.

So, what is the "best practice" for this situation?

Seems like most people would just hard code the number 30 into their
.aspx page. This seems like a programming no-no, though. What if you
later decide to allow a User Name to be 50 characters long? Now you
have to change all the 30's to 50's. You might forget, you might miss
something, you might make a mistake, etc.
No that's still the best practice. Look at it this way: your relational
design is like source code, and changes to source code often require you to
propagate a change throught the applciation. There are many mechanisms
available to help you manage this process. For instance, the typed
DataSet's in .NET are easy to generate and hold a ton of useful metadata.
Alternatively you could add an attribute to a business entity indicating the
maximum length for strings, precision and scale for decimals etc. Point is,
this information should be "hard coded" in your application, but not
necessarilly in the definition of a textbox. It should be part of the
metadata that your front-end can query. You should definitely automate the
the generation of the metadata somehow, however, to streamline the process
for schema changes.

Another way to go: Write a stored procedure in SQL that returns the
max_length of a column given the name of the database, table, and
column (using the system view, "INFORMATION_SCHEMA.COLUMNS"). Then, you
place an algorithm in your code-behind that queries the database for
this information and automatically sets parameters for the textbox and
validation. Some problems with this: (1) takes some work to set up and
debug, (2) increases burden on database server.


That's OK, and caching could easilly eliminate teh performance impact, but I
don't really like doing this a run time. It really seems like something
that should require recompiling your applciation.

David
Apr 23 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Edward Mitchell | last post by:
I have a database with text string fields defined by varchar(nnn). When I request from the user the text from a textbox, I'd like to set the maximum number of characters in the textbox to the...
8
by: StillStuckOnJava | last post by:
I'm using visual studio to create a web application, but I'm having trouble using a few classes that I've defined: public class tank { public tank() { } public float depth; public float...
5
by: StillStuckOnJava | last post by:
I'm having a stupid problem with V.S./C#. I've created a class and two subclasses, and I want to instantiate either of hte subclasses after users make a selection. Then I want to use that class to...
0
by: Rajen Suchede via .NET 247 | last post by:
Hi, I am looking to create a textbox such that when a user types afew charecters(say 3 charecters) a listbox appears with items inthe database starting with the charecters of the textbox. Afterthe...
3
by: Marc Gravell | last post by:
Kind of an open question on best-practice for smart-client design. I'd really appreciate anyones views (preferably with reasoning, but I'll take what I get...). Or if anybody has any useful links...
2
by: simon | last post by:
hello, new to vb.net, have a few questions about DataGrid. I have a dataGrid that is working pulling a dataset back from a stored proc and binding to the datagrid for display the datagrid's...
1
by: savajx1 | last post by:
I need to dynamically create a set of bound fields contained in a GridView control. I also have a single static CommandField that I can declare in the Columns <tagof the GridView control. I have...
2
by: mrutyunjaya | last post by:
hello, i have two textbox column and one button control in gridvie when i click button it will ask plese enter price in first textbox . when enter values it is asking again. how to pass...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.