473,700 Members | 2,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_SC HEMA.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 2128
"Dan Manes" <da******@cox.n et> wrote in message
news:11******** **************@ v46g2000cwv.goo glegroups.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_SC HEMA.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
7452
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 "nnn" that was used in the varchar(...) statement in the field definition. i.e. Field1 varchar(25) NOT NULL and in the .aspx file: <asp.textbox id=Field1 runat="server" />
8
1399
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 maxMudLvl;
5
1263
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 modify its members as users make more selections. IT'S NOT WORKING DAMN IT! The following is my asp.net behind code for the web form, and then my class code. I'm getting an error when I try to access the class instance that I previously...
0
1394
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 item is selected the listbox disappears. In other words thelistbox should disappear when the item is selected and thelistbox of items should appear when the length of the text inthe textbox is >= 3. I have seen this on many sites and even insearch...
3
2657
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 on the subject? (and yes, I have already googled it at length, but still no strong decision) ============= After a long stint of pure-desktop / pure-server applications, I'm currently working on a number of smart-client projects in C# using...
2
12592
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 first column is a textbox(TemplateColumn), the other 3 columns are just display(BoundColumn). (1) if the value of the textbox is 0, then i'd like to change it null, so the box is empty
1
15043
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 to add controls dynamically as I am trying to write a reusable , general , spreadsheet like display control that can take advantage of the built- in update and delete features of the SqlDataSource/Gridview controls. This user control can get its...
2
12925
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 textboxid in javascript .my code is function validation() { alert("hi")
0
8644
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8970
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8924
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7807
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6560
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5902
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4403
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4656
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2392
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.