ADO Update Record

We can use SQL's UPDATE to update a specific record in the database table.

Update records in the database table

We hope to update a record in the Customers table in the Northwind database. First, we need to create a table to list all records in Customers.



<%


conn.Open '

rs.open "SELECT * FROM customers",conn
%>
<h2>List Database</h2>
<table border="1" width="100%">
<tr>
<%
for each x in rs.Fields
  response.write("<th>" & ucase(x.name) & "</th>")
next
%>
</tr>
<% do until rs.EOF %>
<tr>
<form method="post" action="demo_update.asp">
<%
for each x in rs.Fields
  if lcase(x.name)="customerid" then>
    <td>
    <input type="submit" name="customerID" value="<%=x.value%>">
    </td>
  <%else%>
    <td><%Response.Write(x.value)%></td>
  <%end if
next
%>
</form>
<%rs.MoveNext%>
</tr>
<%
loop
conn.close
%>
</table>
</body>
</html>




<%


conn.Open '
cid = Request.Form("customerID")
if Request.form("companyname") = "" then
  
  rs.open '
  %>
  <form method="post" action="demo_update.asp">
  <table>
  <%for each x in rs.Fields%>
  <tr>
  <td><%=x.name%></td>
  <td><input name="<%=x.name%>" value="<%=x.value%>"></td>
  <%next%>
  </tr>
  </table>
  <br /><br />
  <input type="submit" value="Update record">
  </form>
<%
else
  
  
  
  sql = sql & '
  sql=sql & " city='" & Request.Form("city") & "',"
  sql=sql & " postalcode='" & Request.Form("postalcode") & "',"
  sql=sql & " country='" & Request.Form("country") & "'"
  sql=sql & " WHERE customerID='" & cid & "'"
  on error resume next
  conn.Execute sql
  if err<>0 then
    response.write("No update permissions!")
  else 
    response.write("Record " & cid & " was updated!")
  end if 
end if
conn.close
%>
</body>
</html>