%@ CodePage=65001 %> <% Const EW_PAGE_ID = "add" Const EW_TABLE_NAME = "positions" %> <% Session.Timeout = 100 %> <% Response.Expires = 0 Response.ExpiresAbsolute = Now() - 1 Response.AddHeader "pragma", "no-cache" Response.AddHeader "cache-control", "private, no-cache, no-store, must-revalidate" %> <% ' Open connection to the database Dim conn Set conn = Server.CreateObject("ADODB.Connection") conn.Open EW_DB_CONNECTION_STRING %> <% Dim Security Set Security = New cAdvancedSecurity %> <% If Not Security.IsLoggedIn() Then Call Security.AutoLogin() If Not Security.IsLoggedIn() Then Call Security.SaveLastUrl() Call Page_Terminate("login.asp") End If %> <% ' Common page loading event (in userfn60.asp) Call Page_Loading() %> <% ' Page load event, used in current page Call Page_Load() %> <% Response.Buffer = True ' Load key values from QueryString Dim bCopy bCopy = True If Request.QueryString("ID").Count > 0 Then positions.ID.QueryStringValue = Request.QueryString("ID") Else bCopy = False End If ' Create form object Dim objForm Set objForm = New cFormObj ' Process form if post back If objForm.GetValue("a_add")&"" <> "" Then positions.CurrentAction = objForm.GetValue("a_add") ' Get form action Call LoadFormValues() ' Load form values ' Not post back Else If bCopy Then positions.CurrentAction = "C" ' Copy Record Else positions.CurrentAction = "I" ' Display Blank Record Call LoadDefaultValues() ' Load default values End If End If ' Close form object Set objForm = Nothing ' Perform action based on action code Select Case positions.CurrentAction Case "I" ' Blank record, no action required Case "C" ' Copy an existing record If Not LoadRow() Then ' Load record based on key Session(EW_SESSION_MESSAGE) = "No records found" ' No record found Call Page_Terminate(positions.ReturnUrl) ' Clean up and return End If Case "A" ' Add new record positions.SendEmail = True ' Send email on add success If AddRow() Then ' Add successful Session(EW_SESSION_MESSAGE) = "Add New Record Successful" ' Set up success message Call Page_Terminate(positions.KeyUrl(positions.ReturnUrl, "")) ' Clean up and return Else Call RestoreFormValues() ' Add failed, restore form values End If End Select ' Render row based on row type positions.RowType = EW_ROWTYPE_ADD ' add ' Render add type Call RenderRow() %>
Add to TABLE: Positions
Go Back
<%= Session(EW_SESSION_MESSAGE) %>
<% Session(EW_SESSION_MESSAGE) = "" ' Clear message in Session End If %> <% ' If control is passed here, simply terminate the page without redirect Call Page_Terminate("") ' ----------------------------------------------------------------- ' Subroutine Page_Terminate ' - called when exit page ' - clean up ADO connection and objects ' - if url specified, redirect to url, otherwise end response ' Sub Page_Terminate(url) ' Page unload event, used in current page Call Page_Unload() ' Global page unloaded event (in userfn60.asp) Call Page_Unloaded() conn.Close ' Close Connection Set conn = Nothing Set Security = Nothing Set positions = Nothing ' Go to url if specified If url <> "" Then Response.Clear Response.Redirect url End If ' Terminate response Response.End End Sub ' ' Subroutine Page_Terminate (End) ' ---------------------------------------- %> <% ' Load default values Function LoadDefaultValues() End Function %> <% ' Load form values Function LoadFormValues() ' Load from form positions.Position.FormValue = objForm.GetValue("x_Position") positions.Language_ID.FormValue = objForm.GetValue("x_Language_ID") End Function ' Restore form values Function RestoreFormValues() positions.Position.CurrentValue = positions.Position.FormValue positions.Language_ID.CurrentValue = positions.Language_ID.FormValue End Function %> <% ' Load row based on key values Function LoadRow() Dim rs, sSql, sFilter sFilter = positions.SqlKeyFilter If Not IsNumeric(positions.ID.CurrentValue) Then LoadRow = False ' Invalid key, exit Exit Function End If sFilter = Replace(sFilter, "@ID@", ew_AdjustSql(positions.ID.CurrentValue)) ' Replace key value ' Call Row Selecting event Call positions.Row_Selecting(sFilter) ' Load sql based on filter positions.CurrentFilter = sFilter sSql = positions.SQL Set rs = Server.CreateObject("ADODB.Recordset") rs.Open sSql, conn If rs.Eof Then LoadRow = False Else LoadRow = True rs.MoveFirst Call LoadRowValues(rs) ' Load row values ' Call Row Selected event Call positions.Row_Selected(rs) End If rs.Close Set rs = Nothing End Function ' Load row values from recordset Sub LoadRowValues(rs) positions.ID.DbValue = rs("ID") positions.Position.DbValue = rs("Position") positions.Language_ID.DbValue = rs("Language_ID") End Sub %> <% ' Render row values based on field settings Sub RenderRow() ' Call Row Rendering event Call positions.Row_Rendering() ' Common render codes for all row types ' Position positions.Position.CellCssStyle = "" positions.Position.CellCssClass = "" ' Language_ID positions.Language_ID.CellCssStyle = "" positions.Language_ID.CellCssClass = "" If positions.RowType = EW_ROWTYPE_VIEW Then ' View row ElseIf positions.RowType = EW_ROWTYPE_ADD Then ' Add row ' Position positions.Position.EditCustomAttributes = "" positions.Position.EditValue = ew_HtmlEncode(positions.Position.CurrentValue) ' Language_ID positions.Language_ID.EditCustomAttributes = "" sSqlWrk = "SELECT [id], [Language_Name] FROM [Languages]" Set rswrk = Server.CreateObject("ADODB.Recordset") rswrk.Open sSqlWrk, conn If Not rswrk.Eof Then arwrk = rswrk.GetRows Else arwrk = "" End If rswrk.Close Set rswrk = Nothing arwrk = ew_AddItemToArray(arwrk, 0, Array("", "Please Select")) positions.Language_ID.EditValue = arwrk ElseIf positions.RowType = EW_ROWTYPE_EDIT Then ' Edit row ElseIf positions.RowType = EW_ROWTYPE_SEARCH Then ' Search row End If ' Call Row Rendered event Call positions.Row_Rendered() End Sub %> <% ' Add record Function AddRow() On Error Resume Next Dim rs, sSql, sFilter Dim rsnew Dim bCheckKey, sSqlChk, sWhereChk, rsChk Dim bInsertRow ' Check for duplicate key bCheckKey = True sFilter = positions.SqlKeyFilter If positions.ID.CurrentValue = "" Or IsNull(positions.ID.CurrentValue) Then bCheckKey = False Else sFilter = Replace(sFilter, "@ID@", ew_AdjustSql(positions.ID.CurrentValue)) ' Replace key value End If If Not IsNumeric(positions.ID.CurrentValue) Then bCheckKey = False End If If bCheckKey Then Set rsChk = positions.LoadRs(sFilter) If Not (rsChk Is Nothing) Then Session(EW_SESSION_MESSAGE) = "Duplicate value for primary key" rsChk.Close Set rsChk = Nothing AddRow = False Exit Function End If End If ' Add new record sFilter = "(0 = 1)" positions.CurrentFilter = sFilter sSql = positions.SQL Set rs = Server.CreateObject("ADODB.Recordset") rs.CursorLocation = EW_CURSORLOCATION rs.Open sSql, conn, 1, 2 rs.AddNew If Err.Number <> 0 Then Session(EW_SESSION_MESSAGE) = Err.Description rs.Close Set rs = Nothing AddRow = False Exit Function End If ' Field Position Call positions.Position.SetDbValue(positions.Position.CurrentValue, Null) rs("Position") = positions.Position.DbValue ' Field Language_ID Call positions.Language_ID.SetDbValue(positions.Language_ID.CurrentValue, Null) rs("Language_ID") = positions.Language_ID.DbValue ' Check recordset update error If Err.Number <> 0 Then Session(EW_SESSION_MESSAGE) = Err.Description rs.Close Set rs = Nothing AddRow = False Exit Function End If ' Call Row Inserting event bInsertRow = positions.Row_Inserting(rs) If bInsertRow Then ' Clone new rs object Set rsnew = ew_CloneRs(rs) rs.Update If Err.Number <> 0 Then Session(EW_SESSION_MESSAGE) = Err.Description AddRow = False Else AddRow = True End If Else rs.CancelUpdate If positions.CancelMessage <> "" Then Session(EW_SESSION_MESSAGE) = positions.CancelMessage positions.CancelMessage = "" Else Session(EW_SESSION_MESSAGE) = "Insert cancelled" End If AddRow = False End If rs.Close Set rs = Nothing If AddRow Then positions.ID.DbValue = rsnew("ID") ' Call Row Inserted event Call positions.Row_Inserted(rsnew) End If If IsObject(rsnew) Then rsnew.Close Set rsnew = Nothing End If End Function %> <% ' Page Load event Sub Page_Load() '***Response.Write "Page Load" Server.ScriptTimeout=1000 End Sub ' Page Unload event Sub Page_Unload() '***Response.Write "Page Unload" End Sub %>