% '******************************************************* '* ASP 101 Sample Code - http://www.asp101.com/ * '* * '* This code is made available as a service to our * '* visitors and is provided strictly for the * '* purpose of illustration. * '* * '* http://www.asp101.com/samples/license.asp * '* * '* Please direct all inquiries to webmaster@asp101.com * '******************************************************* %> <% ' Just in case you're not up on my acronyms: ' DB = database, RS=recordset, CONN=connection ' o/w = otherwise ' Include the VBScript ADO constants file %> <% ' *** Begin DB Setup *** Dim strConnString ' Sample access OLEDB CONN String. strConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _ Server.MapPath("db_scratch.mdb") & ";" ' Override with our site-wide CONN string. strConnString = "Provider=SQLOLEDB;Data Source=10.2.2.133;" _ & "Initial Catalog=samples;User Id=samples;Password=password;" _ & "Connect Timeout=15;Network Library=dbmssocn;" ' Access likes #, but SQL Server expects ' so you'll need to change ' this based on the DB you're using. Const DATE_DELIMITER = "'" ' *** End DB Setup *** Dim cnnFormToDB ' CONN object Dim strSQL ' String in which to build our SQL command Dim lngRecsAffected ' # of records affected... just informational ' Vars for the fields read in from the form. All fields are read ' in as strings so I need to covert them to the appropriate data ' types to be sure they're appropriate for the DB fields. These ' variables give me some working space to do this easily. Dim strTextField ' text field Dim intIntegerField ' integer field Dim datDateTimeField ' date field Dim strErrorMsg ' Holds error message if we catch any problems. ' See if we have any info to process. ' If we don't (ie. the first time through) we just show ' the form. If we do we proceed with the insert. If Request.Form("action") <> "Save Form Data" Then ' Show the form %>
<% Else ' Do our DB insert! ' Retrieve the 3 strings to be entered into the DB strTextField = Request.Form("text_field") intIntegerField = Request.Form("integer_field") datDateTimeField = Request.Form("date_time_field") ' Start error handling... I'm too lazy to check all the criteria ' on my own so I use VBScript to do it for me. I simply do a ' conversion the the expected type and if it fails I catch the ' error, abort the insert, and display a message. On Error Resume Next strErrorMsg = "" ' String (text) field: ' Nothing should really go wrong here. It's already a string so ' I don't bother with a CStr. I do replace ' with '' for the ' validity our SQL statement and also check to make sure it's ' not an empty string. If it is an empty string ("") then I ' throw a fake error since I've already got this type of error ' handling in place... hey I already admitted I was lazy! strTextField = Trim(strTextField) If Len(strTextField) = 0 Or Len(strTextField) > 10 Then Err.Raise 1 strTextField = Replace(strTextField, "'", "''") intIntegerField = Replace(intIntegerField, "'", "''") datDateTimeField = Replace(datDateTimeField, "'", "''") If Err.number <> 0 Then strErrorMsg = strErrorMsg & "Your entry for string_field is " & _ "inappropriate!The resulting SQL statement was:
<%= strSQL %>
Number of records affected: <%= lngRecsAffected %>
<% End If End If %>