下标越界的处理方法。

ewde43 2014-11-22 02:08:00
有3个字段

strA="A,B,C"

strB="X,Y,Z"

strC="" 这个参数可能是“1”也可能是“1,2”也可能是“1,2,3”,也可能是空

在这样的情况下做循环插入,

当strC的值长度跟strA,strB不一样的时候,就会提示下标越界。

这样的情况应该怎么处理。
...全文
698 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 6 楼 ewde43 的回复:
[quote=引用 5 楼 dogfish 的回复:]
使用的时候,检测一下边界就可以。
for i=0 to UBound(a)
rs.addnew
rs("a") = a(i)
rs("b") = b(i)
if i<=ubound(c) then
rs("c") = c(i)
else
rs("c") = ""
end
rs.update
rs.movenext
next


谢谢,dogfish,测试了一下,下界月标的问题是控制了,但是插入的时候,c(i)没有对应a,b的值,是顺序插入的,

比如,写了5行数据,在第2,4行写了c,插入数据时,c是按顺序插到了1,2行,而不是2,4行。
[/quote]
你的想法思路有点问题

你的目的是:每行的a,b,c值是对应的,你应该是这么实现的:

for i=0 to Request.Form("a").count
rs.addnew
rs("a") = Request.Form("a")(i)
rs("b") = Request.Form("b")(i)
rs("c") = Request.Form("c")(i)
rs.update
rs.movenext
next
ewde43 2014-11-23
  • 打赏
  • 举报
回复
引用 5 楼 dogfish 的回复:
使用的时候,检测一下边界就可以。
for i=0 to UBound(a)
rs.addnew
rs("a")    = a(i)
rs("b")    = b(i)
if i<=ubound(c) then
     rs("c")    = c(i)
else
     rs("c")    = ""
end
rs.update
rs.movenext
 next
谢谢,dogfish,测试了一下,下界月标的问题是控制了,但是插入的时候,c(i)没有对应a,b的值,是顺序插入的, 比如,写了5行数据,在第2,4行写了c,插入数据时,c是按顺序插到了1,2行,而不是2,4行。
Dogfish 2014-11-23
  • 打赏
  • 举报
回复
使用的时候,检测一下边界就可以。
for i=0 to UBound(a)
rs.addnew
rs("a")    = a(i)
rs("b")    = b(i)
if i<=ubound(c) then
     rs("c")    = c(i)
else
     rs("c")    = ""
end
rs.update
rs.movenext
 next
ewde43 2014-11-22
  • 打赏
  • 举报
回复
引用 1 楼 chinmo 的回复:
这个要看你是针对哪个循环做插入的 可以进行判断是否大于C的长度
版主,你好,具体的我贴了实例代码,请看看原因。
ewde43 2014-11-22
  • 打赏
  • 举报
回复
引用 2 楼 slwsss 的回复:
贴下代码..

<head>
<title>test</title></head>
<body>
<form name="form1" method="post" action="test.asp?action=add">
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td>A</td>
      <td>B</td>
      <td>C</td>
    </tr>
    <tr>
      <td><input type="text" name="a" id="a"></td>
      <td><input type="text" name="b" id="b"></td>
      <td><input type="text" name="c" id="c"></td>
    </tr>
    <tr>
      <td><input type="text" name="a" id="a"></td>
      <td><input type="text" name="b" id="b"></td>
      <td><input type="text" name="c" id="c"></td>
    </tr>
    <tr>
     <td><input type="text" name="a" id="a"></td>
      <td><input type="text" name="b" id="b"></td>
      <td><input type="text" name="c" id="c"></td>
    </tr>
    <tr>
     <td><input type="text" name="a" id="a"></td>
      <td><input type="text" name="b" id="b"></td>
      <td><input type="text" name="c" id="c"></td>
    </tr>
    <tr>
     <td><input type="text" name="a" id="a"></td>
      <td><input type="text" name="b" id="b"></td>
      <td><input type="text" name="c" id="c"></td>
    </tr>
  </table>
<input type="submit" name="button" id="button" value="提交">
</form>
</body>

</html>

test.asp

<% 
Dim a
Dim b
Dim c
Dim action

action  = Request.QueryString("action")
a       = Trim(Request.Form("a"))
b       = Trim(Request.Form("b"))
c       = Trim(Request.Form("c"))

'把字符串中有空值的去掉
a      = replace(replace(a," ",""),",,","")
b      = replace(replace(b," ",""),",,","")
c      = replace(replace(c," ",""),",,","")


select case action
case  "add"

set rs=server.CreateObject("adodb.recordset")
rs.open "select *  FROM test ",conn,1,3

a     = Split(a,",")
b     = Split(b,",")
c     = Split(c,",")

for i=0 to UBound(a)
rs.addnew
rs("a")    = a
rs("b")    = b
rs("c")    = c

rs.update
rs.movenext

next

response.Write "<script language='javascript'>alert('添加成功');</script>"
rs.close
set rs=nothing

end select
conn.close
set conn=nothing
%>
slwsss,你好,具体代码如上: 网页中每行有3个文本框:a,b,c,一共5行。 其中a,b是必填项目,c不是必填,比如 填写了2行a,就肯定有2行b,c有可能没有,也有可能有1行,也有可能2行。同样 填写了5行a,就肯定有5行b,c有可能没有,也有可能有1行,也有可能2行。 插入的循环行数参考a,现在就是当c跟a,b等长的时候,插入正常,当c和a,b长度不同的时候,就提示“下标越界” (还有一个问题,文本框post过来的字符串,为什么都是"A, B, C, , ,"这样的格式,每个字符串前面都有一个空格,而不是"A,B,C,,,",所以没办法,在接收的时候用replace过滤了2次才正常)
slwsss 2014-11-22
  • 打赏
  • 举报
回复
贴下代码..
  • 打赏
  • 举报
回复
这个要看你是针对哪个循环做插入的 可以进行判断是否大于C的长度

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧