<%

%>
<!--#include file="te_config.asp"-->
<!--#include file="te_header.asp"-->

<%
	if len(request("q"))>0 then bQuery = True else bQuery = False
%>

<table border=0 cellspacing=1 cellpadding=2 bgcolor = "#ffe4b5" width=100%>
	<tr>
		<td class="smallertext">
			<a href="index.asp">管理首页</a> <a href="te_admin.asp">编辑页面</a> <a href="te_listtables.asp?cid=<%=request("cid")%>"><%=arrDesc(request("cid"))%></a> <a href="te_showtable.asp?cid=<%=request("cid")%>&tablename=<%=server.urlencode(request("tablename"))%>&ipage=<%response.write(request("ipage"))%><% if bQuery then response.write("&q=1") end if %>">表格 [<%=request("tablename")%>]</a> <%if request("add") then response.write "增加" else response.write "编辑"%> 记录
		</td>
		<td class="smallerheader" width=130 align=right>
		<%
		if bProtected then 
			response.write session("teFullName")
			response.write "<a href=""te_logout.asp""> (退出)</a>" 
		end if
		%>
		</td>
	</tr>
</table>
<br>

<%

	lConnID = request("cid")
	sTableName = request("tablename")
	sFieldName = request("fld")
	sFieldValue = request("val")
	lFieldType = CLng(request("fldtype"))
	iPage = request("ipage")
	sQuery = request("q")
	if request("add") then bAdd = True else bAdd = False

    sParentName = server.urlencode(sTableName)

	OpenRS arrConn(lConnID)
	
	if not bAdd then
		select case lFieldType
			case adDate, adDBDate, adDBTime, adDBTimeStamp
				sWhere = " WHERE [" & sFieldName & "]=#" & sFieldValue & "#"
			case adTinyInt, adSmallInt, adInteger, adBigInt, adUnsignedTinyInt, adUnsignedSmallInt, adUnsignedInt, adUnsignedBigInt, adSingle, adDouble, adCurrency, adDecimal, adNumeric, adBoolean
				sWhere = " WHERE [" & sFieldName & "]=" & sFieldValue
			case else
				sWhere = " WHERE [" & sFieldName & "]='" & sFieldValue & "'"
		end select
	else
		sWhere = ""
	end if
	
	iPlace = instr(1,sTableName, " wHeRe ", 1)
	if iPlace then
		sTableName = left(sTableName, iPlace)
	end if
	sSQL = sTableName & sWhere

        rs.Open sSQL, , , adCmdTable
		
		if request("cmdSave") <> "" and bRecEdit = True then
			if bAdd then
				rs.AddNew
			end if
			on error resume next
			for each fld in rs.fields
				iField = iField + 1
				'If field is AutoIncrement, just skip it.
				if fld.properties("isAutoIncrement") = false then
					select case fld.type
						case adBoolean
							if len(request(fld.name)) then rs(fld.name) = True else rs(fld.name) = False
						case adLongVarBinary
							'noop
						case adDate
                     if ( request(fld.name) = "" or request(fld.name) = "0" ) and (fld.attributes and adFldIsNullable) = adFldIsNullable and bConvertDateNull then
                        rs(fld.name) = NULL
                     else
    							if isDate(request(fld.name)) then 
    							   rs(fld.name) = request(fld.name)
    							end if
    						end if
						case adSmallInt, adInteger, adCurrency, adUnsignedTinyInt
                     if ( request(fld.name) = "" or request(fld.name) = "0" ) and (fld.attributes and adFldIsNullable) = adFldIsNullable and bConvertNumericNull then
                        rs(fld.name) = NULL
                     else
    							if isNumeric(request(fld.name)) then 
                           rs(fld.name) = request(fld.name)
                        end if						
							end if
						case else
                     if request(fld.name) = "" and (fld.attributes and adFldIsNullable) = adFldIsNullable and bConvertNull then
                         rs(fld.name) = NULL
                     else
                         rs(fld.name) = request(fld.name)
                     end if						
					end select
					if Err <> 0 then
						response.write "<strong>Error</strong> while updating field '" & fld.name & "'<br>"
						response.write "错误说明: " & err.description & "<br>"
						select case err
							case -2147352571	'Type mismatch
								response.write "如果这是一个 <strong>自动增量</strong>区域, 你可以 <strong>忽视</strong>这个错误.<br><br>"
							case -2147217887	'区域未升级
								response.write "如果这是一个 <strong>自动增量</strong> 区域, 你可以 <strong>忽视</strong>这个错误.<br><br>"
							case else
								response.write "<br>"
								bError = True
						end select
						err = 0
					else
						'Added to enable editing of first field.
						if iField = 1 then
							sFieldValue = request(fld.name)
						end if
					end if
				end if
			next
			rs.update
			if err <> 0 then
				response.write "<strong>错误</strong> 在升级时.<br>"
				response.write "错误说明: " & err.description & "<br>"
				bError = True
				err = 0
			end if
			if not bError then response.write "Record saved"
		end if
		on error goto 0
%>
			<form action="te_showrecord.asp?cid=<%=lConnID%>&tablename=<%=sParentName%>&fld=<%=sFieldName%>&val=<%=sFieldValue%>&fldtype=<%=lFieldType%>&ipage=<%=iPage%>&add=<%=bAdd%>&cmdsave=1<% if bQuery then response.Write("&q=1") %>" method="post" name="frm">
			<table border=0 cellspacing=2 cellpadding=3 bgcolor="#ffe4b5" width=500>
		<%
			lFields = rs.Fields.count
			for each fld in rs.fields
				if not bAdd then
					sValue = rs(fld.name)
				end if

				response.write "<tr>"
				response.write "<td class=""smallerheader"">" & fld.name & "</td>"
				select case fld.type
					case adLongVarWChar	'memo
						response.write "<td><textarea class=""tbflat"" cols=60 rows=6 name=""" & fld.name & """>" & sValue & "</textarea></td>"
					case adLongVarBinary	'ole
						'response.write "<td><img src=""te_getole.asp?cid=" & lConnID & "&tablename=" & sTableName & "&fld=" & fld.name & """></td>"
						response.write "<td></td>"
					case adBoolean
						response.write "<td><input type=""checkbox"" name=""" & fld.name & """"
						if sValue = true then
							response.write " checked>"
						else
							response.write "></td>"
						end if
					case else
						'Replace quotation marks with html &quot;
                        if sValue <> "" then sValue = replace(sValue, """", "&quot;")
						select case fld.type
							case adSmallInt, adInteger, adCurrency, adUnsignedTinyInt, adDate, adDBDate, adDBTime, adDBTimeStamp
								response.write "<td><input class=""tbflat"" size=64 type=""text"" name=""" & fld.name &  """ value=""" & sValue & """></td>"
							case else
								response.write "<td><input class=""tbflat"" size=64 type=""text"" name=""" & fld.name &  """ value=""" & sValue & """ maxlength=""" & fld.definedsize & """></td>"
						end select
				end select
				response.write "</tr>"
			next
			if bRecEdit then
		%>
			<tr>
				<td></td>
				<td>
					<input type="submit" name="cmdSave" value="  增加  " class="cmdflat">
				</td>
			</tr>
		<%
			end if
		%>
			</table>
		</form>
<!--#include file="te_footer.asp"-->	
 

