Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update/Delete a particular row 1

Status
Not open for further replies.

craiogram

MIS
Feb 20, 2004
126
US
I have an asp page which lists data from a db thru a loop. With each row 2 links are created to the left(update/delete).
Example:
(row1) Update/Delete - Sentence1 - Sentence2.
(row2) Update/Delete - Sentence3 - Sentence4.
(row3) Update/Delete - Sentence5 - Sentence6.

I want to be able to click on any of these update or delete links and go to another asp page where I would put the sentences of whichever row was picked into some textbox so that it can be updated. Alternatively if I click on delete I want that particular row to be deleted.

Can someone throw me an idea. I'm a little new at this and can't get a plan of action.
Thank you in advance for any assistance.
 
here is the sample code...should give you a start...

page1.asp
Code:
<html>
<body>
Set rs=server.createobject("adodb.recordset")
Set conn=server.createobject("adodb.connection")

'set your own connectionstring to the database

conn.open connectionstring

'your sql statement

sql= "SELECT recordid, field1, field2 FROM mytable"

rs.open sql,conn

'now here is the display of your data...

if rs.EOF AND rs.BOF then
response.write "sorry, no records found"
else

'we will display the data
%>

<table>
<tr>
<td>update record</td>
<td>delete record</td>
<td>field1</td>
<td>field2</td>
</tr>
<%
do until rs.eof
%>
<tr>
<td><a href="#" onclick='updaterecord("<%=rs("recordid")%>")'>update</a></td>
<td><a href="#" onclick='deleterecord("<%=rs("recordid")%>")'>delete</a></td>
<td><%=rs("field1")%></td>
<td><%=rs("field2")%></td>
</tr>
</table>
<%
rs.movenext
loop
end if
'set all the conn and recorset objects to nothing
</body>
'now lets write our functions here
<script>
Sub updaterecord(recordid)
Window.Open "updaterecord.asp?recordid="&recordid 
End Sub
Sub deleterecord(recordid)
Window.Open "deleterecord.asp?recordid="&recordid 
End Sub
</script>
</html>

our first page is done....now lets look for updaterecord.asp page

Code:
<html>
<body>
<%
recid=request.querystring("recordid")
Set rs=server.createobject("adodb.recordset")
Set conn=server.createobject("adodb.connection")

'set your own connectionstring to the database

conn.open connectionstring

'your sql statement

sql= "SELECT field1, field2 FROM mytable where recordid="&recid

rs.open sql,conn

%>
<form method="post" action="processupdate.asp">
<input type="text" name=myfield1 value="<%=rs("field1")%>">
<input type="text" name=myfield2 value="<%=rs("field2")%>">
<input type="hidden name=myrecid value="<%=recid%>">
<input type="submit" name=submit value="submit">
</form>
'set all the conn and recorset objects to nothing
</body>
</html>

processupdate.asp

Code:
<%


Set conn=server.createobject("adodb.connection")

'set your own connectionstring to the database

conn.open connectionstring

sql = "UPDATE mytable SET field1='"&request.form("myfield1")&"',"&_
      " field2='"&request.form("myfield2")&"'"
      " WHERE recordid="&myrecid

conn.execute(sql)

response.write "record updated"

'you can do a response.redirect here to redirect to a particular page
'set all the conn and recorset objects to nothing
%>
and now deleterecord.asp page

Code:
here we just grab the recordid and process the delete...

<%
recid=request.querystring("recordid")
Set conn=server.createobject("adodb.connection")
conn.open connectionstring
sql = "DELETE FROM mytable where recordid="&recid

conn.execute(sql)

response.write "record deleted"

'you can do a response.redirect here to redirect to a particular page
'set all the conn and recorset objects to nothing

%>

i made this code simple for you writing in this way...there are other good
methods of doing this...i hope you get the idea...please post back if you get
any errors or doubts...

-DNG
 
i just coded the above code at one go...so not tested...make sure you tweak it...

post back if you get any errors...

-DNG
 
craiogram,

did the code work for you...post back if you have any questions...

-DNG
 
DotNetGnat

It works beautiful so far!! I have only done the delete part so far. The fact that you didn't put the "*" threw me off for the delete sql but I figured it out after a little while. I really appreciate the input.

On a side note I am having a problem with apostrophies in the text fields when I'm trying to write back to the DB.

Any suggestions?

Thanks again...A LOT!
 
when you are having apostrophe's just use replace function to replace a single apostrophe with two apostrophes...something like this...

Code:
myfield= Replace(myfield,"'","''")

what we are doing here is ....suppressing the effect of a single quote with two quotes...

-DNG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top