web 2.0

How to use ASP & SQL Server Add/Insert Multiple Rows Record

How to use ASP & SQL Server Add/Insert Multiple Rows Record This is tutorial asp developers how to using ASP add insert multiple record to SQL Server table.

ShotDev Focus:
- ASP & SQL Server add insert multiple record.

Example

asp_sqlserver_multiple_insert1.asp


<% Option Explicit %>
<html>
<head>
<title>ShotDev.Com Tutorial</title>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
//-->
</script>
</head>
<body>
<form action="asp_sqlserver_multiple_insert2.asp" name="frmAdd" method="post">
Select Line :
<select name="menu1" onChange="MM_jumpMenu('parent',this,0)">
<%
Dim i , sel , line
For i = 1 To 50
If CInt(Request.QueryString("Line")) = i Then
sel = "selected"
Else
sel = ""
End IF
%>
<option value="<%=Request.ServerVariables("SCRIPT_NAME")%>?Line=<%=i%>" <%=sel%>><%=i%></option>
<%
Next
%>
</select>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="160"> <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="70"> <div align="center">Budget </div></th>
<th width="70"> <div align="center">Used </div></th>
</tr>
<%
line = Request.QueryString("line")

IF line = "" Then
line = 1
End IF

For i = 1 To line
%>
<tr>
<td><div align="center"><input type="text" name="txtCustomerID<%=i%>" size="5"></div></td>
<td><input type="text" name="txtName<%=i%>" size="20"></td>
<td><input type="text" name="txtEmail<%=i%>" size="20"></td>
<td><div align="center"><input type="text" name="txtCountryCode<%=i%>" size="2"></div></td>
<td align="right"><input type="text" name="txtBudget<%=i%>" size="5"></td>
<td align="right"><input type="text" name="txtUsed<%=i%>" size="5"></td>
</tr>
<%
Next
%>
</table>
<input type="submit" name="submit" value="submit">
<input type="hidden" name="hdnLine" value="<%=i-1%>">
</form>
</body>
</html>

asp_sqlserver_multiple_insert2.asp


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

For  i = 1 To Request.Form("hdnLine")
If Trim(Request.Form("txtCustomerID" & i)) <> "" Then
strSQL = ""
strSQL = strSQL &"INSERT INTO customer "
strSQL = strSQL &"(CustomerID,Name,Email,CountryCode,Budget,Used) "
strSQL = strSQL &"VALUES "
strSQL = strSQL &"('"&Request.Form("txtCustomerID" & i)&"','"&Request.Form("txtName" & i)&"', '"&Request.Form("txtEmail" & i)&"' "
strSQL = strSQL &",'"&Request.Form("txtCountryCode" & i)&"','"&Request.Form("txtBudget" & i)&"', '"&Request.Form("txtUsed" & i)&"') "
Set objExec = Conn.Execute(strSQL)
End IF
Next

Response.write("Save completed. Click <a href='asp_sqlserver_multiple_insert3.asp'>here</a> to view.")

Conn.Close()
Set objExec = Nothing
Set Conn = Nothing
%>
</body>
</html>

asp_sqlserver_multiple_insert3.asp


<% Option Explicit %>
<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<%
Dim Conn,strSQL,objRec
Set Conn = Server.Createobject("ADODB.Connection")
Conn.Open "Driver={SQL Server};Server=localhost;Database=mydatabase;UID=sa;PWD=;"
strSQL = "SELECT * FROM customer "
Set objRec = Server.CreateObject("ADODB.Recordset")
objRec.Open strSQL, Conn, 1,3
%>
<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>
</tr>
<%
While Not objRec.EOF
%>
<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>
</tr>
<%
objRec.MoveNext
Wend
%>
</table>
<%
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_multiple_insert1.asp

Screenshot

ASP & SQL Server (Add/Insert Multiple Record)
.
.
.
Download this script.
Download

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

Leave a Reply

You must be logged in to post a comment.