PDA

View Full Version : سوال: Update



bahar_f
جمعه 20 شهریور 1388, 10:04 صبح
سلام
من مشکلی که دارم اینه که سایت کتابخانه ای را طراحی کردم که در قسمت به روز کردن ها از یه خط کدم ایراد ( خطی که کنارش *** است) می گیره و نمی دونم چی کار کن میشه راهنمایی بفرمایید.

صفحه به روز کردن

<%
'Check if user is logged in
if Session("name") = "" or Session("Group")=2 then
'If not, go to login page
Response.Redirect("Menu3_management.asp")
else %>
<%
Response.Charset="utf-8"
strFile = "BookEdit.asp"
strTable ="Book" 'The table you wish to access
strKey = "Book_Id" 'Key field in Database
bgHeaderColor = "Navy" 'Color of the "Show All" header
fontHeaderColor = "White" 'Font Color of the "Show All" header
bgColor1 = "Silver" 'Background Color of the second and every other record
fontColor1 = "Black" 'Font Color of the second and every other record
bgColor2 = "White" 'Background Color of the first and every other record
fontColor2 = "Black" 'Font Color of the first and every other record
'**************************************
'Set your database -
' change the MapPath to your
' database folder
'**************************************
Dim conn
Set MyConn= Server.CreateObject("ADODB.Connection")
MyConn.Open "driver=SQL Server;server=M4MLUX-M4F14;uid=sa;pwd=;database=Library;"


'**************************************
'Select what sub you want to run
'**************************************
Select Case Request("mode")
Case "AddItem"
AddItem
Case "AddItemAction"
AddItemAction
Case "EditItem"
EditItem
Case "EditItemAction"
EditItemAction
Case "DelItem"
delItem
Case Else
ShowAll
End Select


'**************************************
'Show all records in table
'**************************************
Sub ShowAll()
MySQL="Select * from " & strTable
Set MyRs=MyConn.Execute(MySQL)

Response.write ("<h2>" & strTable & " List</h2>")
'Response.write ("<p><a href=""" & strFile & "?mode=AddItem""><font size=""2"">Add New Catagory</font></a></p>")
Response.write ("<table border=""1"" cellpadding=""4"" cellspacing=""1"">")
Response.write ("<tr>")

howmanyfields=MyRs.fields.count -1

Response.write ("<td bgcolor=""" & bgHeaderColor & """ align=""Center"" nowrap><font color=""" & fontHeaderColor & """ size=""2"">X</font></td>")
Response.write ("<td bgcolor=""" & bgHeaderColor & """ align=""Center"" nowrap><font color=""" & fontHeaderColor & """ size=""2"">E</font></td>")

for i=0 to howmanyfields
if Not MyRs(i).name = "ID" then
Response.write ("<td bgcolor=""" & bgHeaderColor & """ align=""Center"" nowrap><font color=""" & fontHeaderColor & """ size=""2"">" & MyRs(i).name & "</font>&nbsp;</td>")
end if
next

Response.write ("</tr>")

do while not MyRs.eof

if bgColor = bgColor2 then
bgColor = bgColor1
fontColor = fontColor1
else
bgColor = bgColor2
fontColor = fontColor2
end if

Response.write ("<tr>")
Response.write ("<td align=""Center"" valign=""Top"" bgcolor=""" & bgColor & """>")
Response.write ("<font color=""" & fontColor & """><A HREF=""" & strFile & "?mode=DelItem&which=" & MyRs(strKey) & """></A></font></td>")
Response.write ("<td align=""Center"" valign=""Top"" bgcolor=""" & bgColor & """>")
Response.write ("<font color=""" & fontColor & """><A HREF=""" & strFile & "?mode=EditItem&which=" & MyRs(strKey) & """>Edit</A></font></td>")

for i = 0 to howmanyfields
if Not MyRs(i).name = "ID" then
if MyRs(i).name = "PRICE" then
ThisRecord = FormatCurrency(MyRs(i))
strAlign = "right"
else
ThisRecord = MyRs(i)
strAlign = ""
end if
If IsNull(ThisRecord) or ThisRecord = "" Then
ThisRecord = "&nbsp;"
end if
ThisRecord = Replace(ThisRecord,vbCrLf,"<BR>")
Response.write ("<td align=""" & strAlign & """ valign=top bgcolor=""" & bgColor & """><font color=""" & fontColor & """>" & Thisrecord & "</font>&nbsp;</td>")
end if
next

Response.write ("</tr>")

MyRs.movenext
loop

Response.write ("</table>")

MyRs.close
Set MyRs= Nothing

end sub

'**************************************
'Add new Item Form
'**************************************
Sub AddItem()
MySQL="Select * from " & strTable
Set MyRs=MyConn.Execute(MySQL)
Response.write ("<h2>Add New Record to " & strTable & "</h2>")

Response.write ("<form action=""" & strFile & "?mode=AddItemAction"" method=""post"" id=form1 name=form1>")
Response.write ("<table>")

howmanyfields=MyRs.fields.count -1

for i=0 to howmanyfields

if Not MyRs(i).name = "ID" then
ThisRecord = MyRs(i)
ThisRecordName = MyRs(i).name
If IsNull(ThisRecord) or ThisRecord = "" Then
ThisRecord = "&nbsp;"
end if
Response.write ("<tr>")
Response.write ("<td align=""right"">" & ThisRecordName & ": </td>")
Response.write ("<td> <input name=""" & ThisRecordName & """ type=""text""></td>")
Response.write ("</tr>")
end if
next

Response.write ("<tr>")
Response.write ("<td align=""right""><input name=""Submit"" type=submit value=""Submit""></td>")
Response.write ("<td><input name=""reset"" type=reset value=""Reset""></td>")
Response.write ("</tr>")
Response.write ("</table>")
Response.write ("</form>")
Response.write ("<a href=""" & strFile & """>Back to list</a>")

End Sub


'**************************************
'Add new Item Action
'**************************************
Sub AddItemAction()
MySQL="Select * from " & strTable
Set MyRs=MyConn.Execute(MySQL)
howmanyfields=MyRs.fields.count -1
for i=0 to howmanyfields
if Not MyRs(i).name = "ID" then
str = MyRs(i).name
if not i = howmanyfields then
str1 = str1 & "[" & str & "], "
else
str1 = str1 & "[" & str & "]"
end if

strNames = Request(str)
if not i = howmanyfields then
sqlNames1 = sqlNames1 & "'" & strNames & "', "
else
sqlNames1 = sqlNames1 & "'" & strNames & "'"
end if
end if
next
MySQL1="Insert INTO " & strTable & " (" & str1 & ") VALUES (" & sqlNames1 & ")"
Set MyRs1=MyConn.Execute(MySQL1)

MyConn.Close
set MyConn=nothing

Response.Redirect strFile
End Sub


'**************************************
'Edit existing Item
'**************************************
Sub EditItem()
Response.write ("<h2>Edit Record in " & strTable & "</h2>")

which=request("which")

if isNumeric(which) then
MySQL="SELECT * FROM " & strTable & " Where " & strKey & " = " & which
else
MySQL="SELECT * FROM " & strTable & " Where " & strKey & " = '" & which & "'"
end if
Set MyRs=MyConn.Execute(MySQL)
Response.write ("<FORM ACTION=""" & strFile & "?mode=EditItemAction"" METHOD=POST>")
Response.write ("<input name=""ID"" type=""hidden"" value=""" & MyRs(strKey) & """>")
Response.write ("<table>")
howmanyfields=MyRs.fields.count -1

for i=0 to howmanyfields

if Not MyRs(i).name = "ID" then
ThisRecord = MyRs(i)
ThisRecordName = MyRs(i).name
If IsNull(ThisRecord) or ThisRecord = "" Then
ThisRecord = "&nbsp;"
end if
Response.write ("<tr>")
Response.write ("<td align=""right"">" & ThisRecordName & ": </td>")
Response.write ("<td> <input name=""" & ThisRecordName & """ type=""text"" value=""" & MyRs(i) & """></td>")
Response.write ("</tr>")
end if
next
Response.write ("<tr>")
Response.write ("<td align=""right""><INPUT NAME=""Submit"" TYPE=Submit Value=""Update""></td>")
Response.write ("<td><INPUT NAME=""Reset"" TYPE=Reset Value=""Reset""></td>")
Response.write ("</tr>")
Response.write ("</table>")
Response.write ("</FORM>")
Response.write ("<a href=""" & strFile & """>Back to list</a>")

MyRs.close
Set MyRs= Nothing
End Sub

'**************************************
'Edit existing Item Action
'**************************************
Sub EditItemAction()
which = Request(strKey)

if isNumeric(which) then
MySQL="SELECT * FROM " & strTable & " Where " & strKey & " = " & which
else
MySQL="SELECT * FROM " & strTable & " Where " & strKey & " = '" & which & "'"
end if
'MySQL="Select * from " & strTable
Set MyRs=MyConn.Execute(MySQL)***
howmanyfields=MyRs.fields.count -1
for i=0 to howmanyfields
if Not MyRs(i).name = "ID" then
str = MyRs(i).name
strNames = Request(str)
if not i = howmanyfields then
str1 = str1 & "[" & str & "] = '" & strNames & "', "
else
str1 = str1 & "[" & str & "] = '" & strNames & "'"
end if
end if
next
if isNumeric(which) then
MySQL1="UPDATE " & strTable & " SET " & str1 & " Where " & strKey & " = " & which
else
MySQL1="UPDATE " & strTable & " SET " & str1 & " Where " & strKey & " = '" & which & "'"
end if
'MySQL1="UPDATE " & strTable & " SET " & str1 & " Where ID=" & which
Set MyRs1=MyConn.Execute(MySQL1)

MyConn.Close
set MyConn=nothing

Response.Redirect strFile
End Sub


'**************************************
'Delete selected item in list
'**************************************
Sub DelItem()
which=request("which")
if isNumeric(which) then
MySQL="delete * from " & strTable & " Where " & strKey & " = " & which
else
MySQL="delete * from " & strTable & " Where " & strKey & " = '" & which & "'"
end if

Set MyRs=MyConn.Execute(MySQL)

MyConn.Close
set MyConn=nothing

Response.Redirect strFile
End Sub
'_________________________________________________ ______________________________
%>
<%end if%>