Click to See Complete Forum and Search --> : [RESOLVED] Need some help with error handling.


DanInMA
10-26-2010, 09:52 AM
I am getting an ASP error on line 170 when the user enters an Id number that doesnt exist in the recordset. Can someone guide through handling the error? Id like to use response.write to show my cutom error response if the ID doesnt exist. I tried a few tutorials and am having a hard time understanding how to do so.



<%@ LANGUAGE="VBSCRIPT" %>
<!--#include virtual = "/Subwebs/Information Services/HelpDesk/rz/HDeditconn.asp" -->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<TITLE>Update HD redzone records</TITLE>

</HEAD>
<BODY><%
' -- Declare Variables
Dim strID, intPass ' variables for ID and PASS

' -- Get values from the Form
strID= Request("id")
intPass = Request("PASS")

' -- Take Action based on how we came into this page
Select Case Trim(intPass)
Case Trim("1")
' -- Repeat Visit, Update the Data
UpdateData
Case Else
' -- First Time Visit, display HTML Form
DisplayHTMLForm
End Select

' -- Make sure nothing else gets processed
Response.End

'----------------------------------------------------------------------
' All ASP post processing code goes here, as well as
' sub routines and functions
'----------------------------------------------------------------------
'----------------------------------------------------------------------
' Sub Routine: DisplayHTMLForm
' Displays an HTML Form
'----------------------------------------------------------------------
Sub DisplayHTMLForm()
' -- Declare Variables
Dim objConn ' Our Connection Object
Dim objRS ' Our Recordset Object
Dim strSQL ' Our SQL String to access the database
Dim strConnection ' Our Connection string to access the database
Dim i ' a counter variable
Dim strWhereState ' Where clause for State
Dim strWhereCity ' Where clause for City
Dim strFilter ' Our Filter String for Display

' -- no id, we cannot proceed
if strID = "" Then
strID = "2"

end if


' -- Create objects
Set objConn = Server.CreateObject("ADODB.Connection")
Set objRS = Server.CreateObject("ADODB.Recordset")

' -- Connection String Value
strConnection = MyDsn

' -- Open the Connection
objConn.Open strConnection

' - Build the SQL Statement to fetch the single record for this ID
strSQL = "SELECT * FROM redzone_msgs WHERE id = '" & strID & "' "
' -- Populate our Recordset with data
set objRS = objConn.Execute (strSQL)


%>

<!--#include virtual = "/Subwebs/Information Services/HelpDesk/rz/rz-menubar.asp" -->

<div id="main">
<h2>Update Redzone Record</h2>
<form name="form1" id="form1"FORM ACTION="HDedit1.asp" METHOD="POST">
<label>
<input type="text" name="id" id="id" class="shorterfield">
</label>
<INPUT TYPE="SUBMIT" VALUE="Choose a new record by ID Number" id="UPDATEID">
<EM>&nbsp;&nbsp;Record ID # 2 is the example record</EM>
</form>
</div>
<FORM ACTION="HDedit1.asp" METHOD="POST" id="#updateform">
<INPUT TYPE="hidden" NAME="PASS" VALUE="1">
<TABLE BORDER="0" CELLPADDING="3" CELLSPACING="0" WIDTH="80%" id="table1" >
<%
'following line is line 170
objRS.MoveFirst


' -- Output data
For i = 0 to objRS.Fields.Count - 1
Response.write "<TR>"
Response.Write "<TH>" & objRS.Fields(i).Name & "</TH>"
' - output value as a textbox, except for ID field
Select Case objRS.Fields(i).Name
Case "id"
Response.Write "<TD>" & objRS.Fields(i) & "<INPUT TYPE=""hidden"" NAME=""ID"" VALUE=""" & objRS.Fields(i) & """></TD>"

Case "scheduling"
Select Case objRS("scheduling")
case "unscheduled"
Response.Write "<TD><label><input name=""scheduling"" type=""radio"" id=""scheduling_0"" value=""scheduled"">Scheduled event</label><label><input type=""radio"" name=""scheduling"" value=""unscheduled"" id=""scheduling_1"" checked>Unscheduled event</label></td>"
case "scheduled"
Response.Write "<TD><label><input name=""scheduling"" type=""radio"" id=""scheduling_0"" value=""scheduled"" checked>Scheduled event</label><label><input type=""radio"" name=""scheduling"" value=""unscheduled"" id=""scheduling_1"" >Unscheduled event</label></td>"
end select
Case "approval"
Select Case objRS("approval")
case True
Response.Write "<TD><label><input name=""approval"" type=""radio"" id=""approv_0"" value=""1"" checked>Approved for posting</label><label><input type=""radio"" name=""approval"" value=""0"" id=""approv_1"" >Unapproved for posting</label></td>"
case False
Response.Write "<TD><label><input name=""approval"" type=""radio"" id=""approv_0"" value=""1"" >Approved for posting</label><label><input type=""radio"" name=""approval"" value=""0"" id=""approv_1"" checked>Unapproved for posting</label></td>"
end select
Case "shortmsg"
Response.Write "<TD><textarea rows=""3"" cols=""48"" id=""shortmsg"" NAME=""" & objRS.Fields(i).Name & """>"&objRS.Fields(i)&"</textarea>Field length currently set to a maximum of 120 characters.</TD>"
Case "longmsg"
Response.Write "<TD><textarea rows=""10"" cols=""48"" id=""longmsg"" NAME=""" & objRS.Fields(i).Name & """>"&objRS.Fields(i)&"</textarea>Field length currently set to a maximum of 350 characters.</TD>"
Case "notes"
Response.Write "<TD><textarea rows=""10"" cols=""48"" id=""notes"" NAME=""" & objRS.Fields(i).Name & """readonly>"&objRS.Fields(i)&"</textarea><br><em>Notes field set to Read Only</em></TD>"
Case Else
' - output a text box
Response.Write "<TD><INPUT TYPE=""TEXT"" NAME=""" & objRS.Fields(i).Name & """ VALUE=""" & objRS.Fields(i) & """ SIZE=""60"" ></TD>"
End Select
Response.write "</TR>"

Next
%>
<TR><TD>&nbsp;</TD><TD><INPUT TYPE="SUBMIT" VALUE="Update Redzone record" id="updatebutton"></TD></TR>
</TABLE>
</FORM>

</BODY>
<script type="text/javascript" src="/Subwebs/Information Services/HelpDesk/javascript/jquery.textareaCounter.plugin.js"></script>
<script language="JavaScript">
<!--
var options2 = {
'maxCharacterSize': 120,
'originalStyle': 'originalDisplayInfo',
'warningStyle': 'warningDisplayInfo',
'warningNumber': 30,
'displayFormat': '#left of #max Characters Left' };
var options3 = {
'maxCharacterSize': 350,
'originalStyle': 'originalDisplayInfo',
'warningStyle': 'warningDisplayInfo',
'warningNumber': 50,
'displayFormat': '#left of #max Characters Left' };

$('#shortmsg').textareaCount(options2);
$('#longmsg').textareaCount(options3);

//-->


</HTML>
<%
End Sub
'----------------------------------------------------------------------
' Sub Routine: UpdateData
' Updates Data in database
'----------------------------------------------------------------------
Sub UpdateData()

' -- Declare Variables
Dim objConn ' Our Connection Object
Dim strSQL ' Our SQL String to access the database
Dim strConnection ' Our Connection string to access the database
' -- our modifiable fields
Dim strApproval, strScheduling, strEventDate, strStartDate, strEndDate, strReqDate
Dim strReqUsername, strReqname, strSystemName, strShortMsg, strLongMsg, strNotes

' -- Get values from our Form
strApproval = Request("approval")
strScheduling = Request("scheduling")
strEventDate = Request("eventdate")
strStartDate = Request("startdate")
strEndDate = Request("enddate")
strReqDate = Request("reqdate")
strReqUsername = Request("requsername")
strReqname = Request("reqname")
strShortMsg = Request("shortmsg")
strLongMsg = Request("longmsg")
strNotes = Request("notes")

' -- Build our SQL Statement
strSQL = "UPDATE redzone_msgs " & _
" SET approval = " & CheckString(strApproval, ",") & _
" scheduling = " & CheckString(strScheduling, ",") & _
" eventdate = " & CheckString(strEventDate, ",") & _
" startdate = " & CheckString(strStartDate, ",") & _
" enddate = " & CheckString(strEndDate, ",") & _
" reqdate = " & CheckString(strReqDate, ",") & _
" requsername = " & CheckString(strReqUsername, ",") & _
" reqname = " & CheckString(strReqname, ",") & _
" shortmsg = " & CheckString(strShortMsg, ",") & _
" notes = " & CheckString(strNotes, ",") & _
" longmsg = " & CheckString(strLongMsg, " ") & _
" WHERE id = '" & strID & "' "

' -- Create objects
Set objConn = Server.CreateObject("ADODB.Connection")

' -- Connection String Value
strConnection = MyDsn

' -- Open the Connection
objConn.Open strConnection


' -- Execute It
objConn.Execute (strSQL)

objConn.Close
set objConn = Nothing

' -- send user back to grid
'Response.write ("record Updated")
' -- or alternatively, send user back to this page with modified values
Response.Redirect "HDedit1.asp?id=" & strID

'----------------------------------------------------------------------
' End HTML Output
'----------------------------------------------------------------------
End Sub
Function CheckString(byval strText, byval strEndChar)
strText = Replace(strText, "'", "''")
If strText = "" Then
CheckString = "Null" & strEndChar
Else
CheckString = "'" & strText & "'" & strEndChar
End if
End Function

%>