classic asp - add,edit,delete a record (What you want to do model)


This asp code is a single form using which you can add,edit and delete a record. For example, from a select box, you have to select an item like : Add a dept or Edit a Dept or Delete a Dept. based on your selection, the rest of the form will change.

<%



'security


if session("user_group")<>"ADMN" then


response.redirect("../INDEX.ASP?result=Session Timed out. Please login again..")


end if


'end of security


%>


<%


'database connection for oracle


Set MyConn =Server.CreateObject("ADODB.Connection")


MyConn.open="provider=MSDAORA;data source=xe; user id=scott;password=tiger"


' here xe is the instance name


'end of database connection






job_type=Request.QueryString("job_type")


THIS_FILE_NAME="add_edit_delete_view.asp"


'this table emp contains the following fields


'start_date,end_date,emp_name,record_id


%>






<!--CODE FOR ALERT POSTBACK MESSAGE-->


<script>


window.onload=function(){


alert_result_msg();}


</script>


<script language="javascript">


function alert_result_msg(){


if (FormResultMsg.resultMsg.value !=""){


alert(FormResultMsg.resultMsg.value); } }


</script>


<form name="FormResultMsg">


<input type="hidden" name="resultMsg" value="<%=request("result")%>">


</form>


<!--end of CODE FOR ALERT POSTBACK MESSAGE-->






<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


<html>


<head>


<title>Here goes the Title</title>


<link href="../style_sheet_module/current_style.css" rel="stylesheet">


<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">


</head>


<body>


<%


BACK_FILE_NAME=Request("back_file_name")


If back_file_name <>"" then%>


<a href="<%=back_file_name%>">Back</a><%


End If%>


<!--#include file="../includes/alert_result_msg.inc"-->


<%


sql="select * from emp where section='"& session("section") &"' order by start_date"


set objrs=myconn.execute(sql)


if objrs.eof then


is_eof="yes"


else


is_eof="no"


end if


%>


<center>


<p><font color="#6699FF" size="2" face="Arial, Helvetica, sans-serif"><br>


</font></p>


</center>


<form action="add_edit_delete_view1.asp" method="post">


<input type="hidden" name="section" value="<%=session("section")%>">


<table border=1 cellspacing=0 align=center width="709">






<TR bgcolor=#e7f9fe>


<TD width="106">What you want to do?<td>


<td width="156"> <font color="#E7F9FE">


<select name="job_type" onChange="location.href='add_edit_delete_view.asp?job_type='+this.value">


<option style="font:bold 24px arial" value="">--select--</option>


<%


if job_type="VIEW" then%>


<option style="font:bold 24px arial" selected value="VIEW">VIEW EXISTING ENTRIES</option>


<%


else


%>


<option style="font:bold 24px arial" value="VIEW">VIEW EXISTING ENTRIES</option>


<%


end if


%>






<%


if job_type="ADDNEW" then%>


<option style="font:bold 24px arial" selected value="ADDNEW">ADD A NEW ENTRY</option>


<%


else


%>


<option style="font:bold 24px arial" value="ADDNEW">ADD NEW ENTRY</option>


<%


end if


%>


<%


if job_type="EDIT" then%>


<option style="font:bold 24px arial" selected value="EDIT">EDIT AN ENTRY</option>


<%


else


%>


<option style="font:bold 24px arial" value="EDIT">EDIT AN ENTRY</option>


<%


end if


%>


<%


if job_type="DELETE" then%>


<option style="font:bold 24px arial" selected value="DELETE">DELETE AN ENTRY</option>


<%


else


%>


<option style="font:bold 24px arial" value="DELETE">DELETE AN ENTRY</option>


<%


end if


%>


</select>


</font><td>


</tr>


<%if job_type="EDIT" or job_type="DELETE" or job_type="VIEW" then%>


<tr bgcolor=#e7f9fe>


<td width="106"> Select the Record<td>


<td width="156"> <select style="font:bold 24px arial" name="current_record_id" onChange="location.href='add_edit_delete_view.asp?job_type=<%=request.Querystring("job_type")%>¤t_record_id='+this.value">


<option style="font:bold 24px arial" value="">--Select the Record--</option>


<%


if is_eof="no" then


current_record_id=request.QueryString("current_record_id")


while not objrs.eof


if cint(current_record_id)=cint(objrs("record_id")) then


%>


<option style="font:bold 24px arial" selected value="<%=objrs("record_id")%>"><%=objrs("emp_name")%>(<%=ddmmyy(ObjRs("start_date"))%>)</option>


<%


else


%>


<option style="font:bold 24px arial" value="<%=objrs("record_id")%>"><%=objrs("emp_name")%>(<%=ddmmyy(ObjRs("start_date"))%></option>


<%


end if


objrs.movenext


wend


else


%>


<option value="">--List is Empty--</option>


<%


end if


%>


</select>


<%


if current_record_id <>"" then


set objRsIns=myconn.execute("select * from emp where record_id='"& current_record_id &"'")


if not objRsIns.eof then


start_date=ddmmyy(objRsIns("start_date"))


end_date=ddmmyy(objRsIns("end_date"))


emp_name=objRsIns("emp_name")


record_id=objRsIns("record_id")


objRsIns.close


set objRsIns=nothing


end if


end if


%>


<td>


</tr>


<%


end if


if job_type="VIEW" or job_type="ADDNEW" or job_type="EDIT" or job_type="DELETE" then


%>


<tr>


<td colspan=2 height="8" bgcolor=#1985bd> <td>


</tr>


<tr>


<TD width="106" > 1.Employee Name*<td>


<%


if job_type="VIEW" then%>


<td width="156"> <%=emp_name%><td>


<%else%>


<td width="433"> <input style="font:bold 13px arial;background-color:#ffd0d0" type=text name="emp_name" value="<%=emp_name%>" size="60">


<span style="color:#ff0000;font:bold 20px arial">*</span><td>


<%


end if%>


</tr>


<tr>


<TD width="106">2.Duration*<td>


<%if job_type="VIEW" then%>


<td width="156"><%=start_date%> To <%=end_date%><td>


<%else%>


<td width="433">


From: <input type=text name="start_date" value="<%=start_date%>" size="10" >


To: <input type=text name="end_date" value="<%=end_date%>" size="10" >


<%end if%>


<td>


</tr>


<tr>


<TD colspan=3 align=center>


<%if current_record_id <>"" and job_type="EDIT" then%>


  <input style="background-color:red;color:white;font-weight:bold" type=submit name="submit" value="Save Above Changes">


<%elseif current_record_id <>"" and job_type="DELETE" then%>


  <input style="background-color:red;color:white;font-weight:bold" type=submit name="submit" value="Delete The Above Record">


<%elseif current_record_id="" and job_type="ADDNEW" then%>


  <input style="background-color:red;color:white;font-weight:bold" type=submit name="submit" value="Save The Above Record">


<%elseif current_record_id="" and job_type="" then%>


<%end if%>


<td>


</tr>


<%End If%>


</table>


</form>


</body>


</html>
Thank you spending your time here.
If possible you; can leave some comments or suggestions.
Thanks a lot once again.
Google Search
Disclaimer and Copy Right