Imagine adding web forms that will automatically be ready for use with a database. This is not an IDE or simple trick. I think this article will help programmers expand their knowledge of web architecture, as they have not been presented with a different way to handle web forms that present and update a database in ASP.
In many cases, web programmers present a web page and ‘fill’ it with information from the database. If the data is to be updated (inserted or removed), then the data is presented in a form and ‘submitted’ to a server script to be processed. This method is ok, but assumes a 1 to 1 relationship between the each form and server processing script. Our goal is ‘many’ web forms and one processing script.
First, in designing the web architecture for integrating a database, we assume a single data source (Conn).
To keep this straightforward, we have a standard ‘web form’ defining some input variables. Here is our ‘web form’:
- In ALL web forms, take a recordset and output to a form object using HTML Textboxes to display the data. In the name property of the textbox put "name=<%=rs("columnName").name%>". In the value property of the same textbox write "value=<%=rs("columnName").value%>".
- Insert a ‘hidden’ input type in the form. The name property is ‘Name=xSqlCommand’ and value=’INSERT’. This represents the sql command you wish to execute. Examples are INSERT, UPDATE, and DELETE.
- Insert a ‘hidden’ input with the name property ‘name=xTABLE’ and value=’Tablename’. The Tablename is the database table from which the recordset originates. Often, TableName is server-side as it was used in the SQL statement to derive the recordset.
- For UPDATE and DELETE statements, insert another ‘hidden’ input type in the form, ‘name=xPrimaryKey’ and value=’RowPrimaryKeyValue’. Often the FieldName primary key is ID or nameID or something like that.
- For UPDATE and DELETE statements, insert another ‘hidden’ input type in the form, ‘name=xPrimaryKeyFieldName’ and value=’strPrimaryKFieldName’. The string value of the primary key column name.
- Submit all web forms to the same ASP script. I.e. ACTION=udpatedb.asp
Here is the server script. In the method below *NOTE* the little ‘x’ that will tell us that the requested value is not a database field name. The purpose of the ‘x’ is to separate our database fields from other inputs. Other inputs can be security, user, or application information used in script processing.
An additional comment before the code: This script requires you to have a solid understanding of programming and will NOT be a simple ‘cut and paste’. Read and make customizations to fit your software. This script has worked extremely well for me and I hope that you can make good use of it.