web 2.0

How to use ASP & SQL Server Add/Insert/Edit/Delete On Same Form

How to use ASP & SQL Server Add/Insert/Edit/Delete On Same Form This is tutorial asp developers ASP manage SQL Server table on Add/Insert/Update/Delete on same form.

ShotDev Focus:
- ASP & SQL Server add/insert/update/delete on same form..

Example

asp_sqlserver_command.asp


<% Option Explicit %>
<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<%
Dim Conn,strSQL,objRec,objExec,i
Set Conn = Server.Createobject("ADODB.Connection")
Conn.Open "Driver={SQL Server};Server=localhost;Database=mydatabase;UID=sa;PWD=;"

'*** Add Condition ***'
If Request.Form("hdnCmd") = "Add" Then
strSQL = ""
strSQL = strSQL &"INSERT INTO customer "
strSQL = strSQL &"(CustomerID,Name,Email,CountryCode,Budget,Used) "
strSQL = strSQL &"VALUES "
strSQL = strSQL &"('"&Request.Form("txtAddCustomerID")&"','"&Request.Form("txtAddName")&"', '"&Request.Form("txtAddEmail")&"' "
strSQL = strSQL &",'"&Request.Form("txtAddCountryCode")&"','"&Request.Form("txtAddBudget")&"', '"&Request.Form("txtAddUsed")&"') "
Set objExec = Conn.Execute(strSQL)
End IF

'*** Update Condition ***'
If Request.Form("hdnCmd") = "Update" Then
strSQL = "UPDATE customer SET "
strSQL = strSQL&"CustomerID = '"&Request.Form("txtEditCustomerID")&"' "
strSQL = strSQL&",Name = '"&Request.Form("txtEditName")&"' "
strSQL = strSQL&",Email = '"&Request.Form("txtEditEmail")&"' "
strSQL = strSQL&",CountryCode = '"&Request.Form("txtEditCountryCode")&"' "
strSQL = strSQL&",Budget = '"&Request.Form("txtEditBudget")&"' "
strSQL = strSQL&",Used = '"&Request.Form("txtEditUsed")&"' "
strSQL = strSQL&"WHERE CustomerID = '"&Request.Form("hdnEditCustomerID")&"' "
Set objExec = Conn.Execute(strSQL)
End IF

'*** Delete Condition ***'
If Request.QueryString("Action") = "Del" Then
strSQL = "DELETE FROM customer "
strSQL = strSQL&"WHERE CustomerID = '"&Request.QueryString("CusID")&"' "
Set objExec = Conn.Execute(strSQL)
End IF

strSQL = "SELECT * FROM customer  "
Set objRec = Server.CreateObject("ADODB.Recordset")
objRec.Open strSQL, Conn, 1,3
%>
<form name="frmMain" method="post" action="<%=Request.ServerVariables("SCRIPT_NAME")%>">
<input type="hidden" name="hdnCmd" value="">
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="98"> <div align="center">Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">CountryCode </div></th>
<th width="59"> <div align="center">Budget </div></th>
<th width="71"> <div align="center">Used </div></th>
<th width="30"> <div align="center">Edit </div></th>
<th width="30"> <div align="center">Delete </div></th>
</tr>
<%
While Not objRec.EOF
%>

<%
IF objRec.Fields("CustomerID").Value = Request.QueryString("CusID") and Request.QueryString("Action") = "Edit" Then
%>
<tr>
<td><div align="center">
<input type="text" name="txtEditCustomerID" size="5" value="<%=objRec.Fields("CustomerID").Value%>">
<input type="hidden" name="hdnEditCustomerID" size="5" value="<%=objRec.Fields("CustomerID").Value%>">
</div></td>
<td><input type="text" name="txtEditName" size="20" value="<%=objRec.Fields("Name").Value%>"></td>
<td><input type="text" name="txtEditEmail" size="20" value="<%=objRec.Fields("Email").Value%>"></td>
<td><div align="center"><input type="text" name="txtEditCountryCode" size="2" value="<%=objRec.Fields("CountryCode").Value%>"></div></td>
<td align="right"><input type="text" name="txtEditBudget" size="5" value="<%=objRec.Fields("Budget").Value%>"></td>
<td align="right"><input type="text" name="txtEditUsed" size="5" value="<%=objRec.Fields("Used").Value%>"></td>
<td colspan="2" align="right"><div align="center">
<input name="btnAdd" type="button" id="btnUpdate" value="Update" OnClick="frmMain.hdnCmd.value='Update';frmMain.submit();">
<input name="btnAdd" type="button" id="btnCancel" value="Cancel" OnClick="window.location='<%=Request.ServerVariables("SCRIPT_NAME")%>';">
</div></td>
</tr>
<%
Else
%>
<tr>
<td><div align="center"><%=objRec.Fields("CustomerID").Value%></div></td>
<td><%=objRec.Fields("Name").Value%></td>
<td><%=objRec.Fields("Email").Value%></td>
<td><div align="center"><%=objRec.Fields("CountryCode").Value%></div></td>
<td align="right"><%=objRec.Fields("Budget").Value%></td>
<td align="right"><%=objRec.Fields("Used").Value%></td>
<td align="center"><a href="<%=Request.ServerVariables("SCRIPT_NAME")%>?Action=Edit&CusID=<%=objRec.Fields("CustomerID").Value%>">Edit</a></td>
<td align="center"><a href="JavaScript:if(confirm('Confirm Delete?')==true){window.location='<%=Request.ServerVariables("SCRIPT_NAME")%>?Action=Del&CusID=<%=objRec.Fields("CustomerID").Value%>';}">Delete</a></td>
</tr>
<%
End IF
%>
<%
objRec.MoveNext
Wend
%>
<tr>
<td><div align="center"><input type="text" name="txtAddCustomerID" size="5"></div></td>
<td><input type="text" name="txtAddName" size="20"></td>
<td><input type="text" name="txtAddEmail" size="20"></td>
<td><div align="center"><input type="text" name="txtAddCountryCode" size="2"></div></td>
<td align="right"><input type="text" name="txtAddBudget" size="5"></td>
<td align="right"><input type="text" name="txtAddUsed" size="5"></td>
<td colspan="2" align="right"><div align="center">
<input name="btnAdd" type="button" id="btnAdd" value="Add" OnClick="frmMain.hdnCmd.value='Add';frmMain.submit();">
</div></td>
</tr>
</table>
</form>
<%
objRec.Close()
Conn.Close()
Set objRec = Nothing
Set Conn = Nothing
%>
</body>
</html>

Create a asp file and save to path root-path/myasp/

Run
http://localhost/myasp/asp_sqlserver_command.asp

Screenshot

ASP & SQL Server (Add/Insert/Edit/Delete On Same Form)

ASP & SQL Server (Add/Insert/Edit/Delete On Same Form)
.
.
.
Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (1 votes, average: 1.00 out of 10)
Loading ... Loading ...

One Response to “How to use ASP & SQL Server Add/Insert/Edit/Delete On Same Form”

  1. 3nebuchadnezzar…

Leave a Reply

You must be logged in to post a comment.