-- 作者:cnsjroom
-- 发布时间:2022/12/15 9:02:00
-- System.IndexOutOfRangeException: 索引超出了数组界限
System.Reflection.TargetInvocationException: 调用的目标发生了异常。 ---> System.IndexOutOfRangeException: 索引超出了数组界限。 麻烦老师帮忙看看 主要是在台账中如果没有编号 就会出现这个……如果已经有了类似bazbdy001的编号就不会出现
当前代码:
Dim lb As String = dr("分类代码") Dim idx As Integer Dim cmd As New S QLC ommand cmd.Con nect io nNa me = "主数据源" cmd.CommandText = "Se lect * From 台账 where 分类代码 = \'" & lb & "\' And [_Identify] <>\'\'" Dim dt As DataTable = cmd.ExecuteReader() Dim dr1 As DataRow = dt.DataRows(dt.DataRows.Count - 1) \'获得最后一行 output.show(dr1("_Identify")) If dr1("编号").StartsWith("BZZB" & lb) = False \'如果单据编号前缀不符 cmd.CommandText = "sele ct Max(编号) from 台账 where 分类代码 = \'" & lb & "\' And [_Identify] <> " & dr1("_Identify") Dim max As String max = cmd.ExecuteScalar() output.show(max) If max > "" Then \'如果存在最大编号 idx = CInt(max.Substring(6,3)) + 1 \'获得最大编号的后三位顺序号,并加1 Else idx = 1 \'否则顺序号等于1 End If dr("编号")=("BAZB" & lb & Format(idx,"000")) dr.Save End If
运行提示:
错误的事件名称项目,HttpRequest System.Reflection.TargetInvocationException: 调用的目标发生了异常。 ---> System.IndexOutOfRangeException: 索引超出了数组界限。 在 eWebSer.form673_save_Ajax(Request rq) --- 内部异常堆栈跟踪的结尾 --- 在 Microsoft.VisualBasic.CompilerServices.Symbols.Container.InvokeMethod(Method TargetProcedure, Object[] Arguments, Boolean[] CopyBack, BindingFlags Flags) 在 Microsoft.VisualBasic.CompilerServices.NewLateBinding.CallMethod(Container BaseReference, String MethodName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, BindingFlags InvocationFlags, Boolean ReportErrors, ResolutionFailure& Failure) 在 Microsoft.VisualBasic.CompilerServices.NewLateBinding.ObjectLateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn) 在 Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn) 在 UserCode.HttpRequest(RequestEventArgs e) 2022-12-15 09:00:37
|
-- 作者:cnsjroom
-- 发布时间:2022/12/15 10:30:00
-- 回复:(有点蓝)idx = CInt(max.Substring(max.lengt...
Dim lb As String = dr("分类代码") Dim idx As Integer Dim cmd As New SQLCommand cmd.Conn ecti onNa me = "主数据源" cmd.CommandText = "Se lect * From 台账 where 分类代码 = \'" & lb & "\' And [_Identify] <>\'\'" Dim dt As DataTable = cmd.ExecuteReader() Dim dr1 As DataRow If dr1 IsNot Nothing Then 如果找到行 那就获取最大编号 dr1= dt.DataRows(dt.DataRows.Count - 1) \'获得最后一行 output.show(dr1("_Identify")) cmd.CommandText = "sel ect Max(编号) from 台账 where 分类代码 = \'" & lb & "\' And [_Identify] <> " & dr1("_Identify") Dim max As String max = cmd.ExecuteScalar() If max > "" Then \'如果存在最大编号 idx = CInt(max.Substring(max.length - 3)) + 1 \'获得最大编号的后三位顺序号,并加1 Else idx = 1 \'否则顺序号等于1 End If dr("编号")=(lb & Format(idx,"000")) Else 如果找不到行 那就直接定义为1 并开始编号 idx = 1 \'否则顺序号等于1 dr("编号")=(lb & Format(idx,"000")) End If
继续麻烦老师一下 我当前得到的编号永远都是001
|
-- 作者:cnsjroom
-- 发布时间:2022/12/15 11:01:00
-- 回复:(有点蓝)新编号没有保存,SQL只能获取后台已保...
Dim dr As DataRow = rq.result \'返回当前保存的数据行,可再处理数据 If dr IsNot Nothing Then Dim lb As String = dr("分类代码") Dim idx As Integer Dim cmd As New SQ LC ommand cmd.Conn ecti onN ame = "主数据源" cmd.CommandText = "Se lect * From 台账 where 分类代码 = \'" & lb & "\' And [_Identify] <>\'\'" Dim dt As DataTable = cmd.ExecuteReader() Dim dr1 As DataRow If dr1 IsNot Nothing Then dr1= dt.DataRows(dt.DataRows.Count - 1) \'获得最后一行 output.show(dr1("_Identify")) cmd.CommandText = "sele ct Max(编号) from 台账 where 分类代码 = \'" & lb & "\' And [_Identify] <> " & dr1("_Identify") Dim max As String max = cmd.ExecuteScalar() If max > "" Then \'如果存在最大编号 idx = CInt(max.Substring(max.length - 3)) + 1 \'获得最大编号的后三位顺序号,并加1 Else idx = 1 \'否则顺序号等于1 End If dr("编号")=lb & Format(idx,"000") Else idx = 1 \'否则顺序号等于1 dr("编号")=lb & Format(idx,"000") End If dr.Save End If
老师保存了的 也是一样的不递增……
[此贴子已经被作者于2022/12/15 11:05:10编辑过]
|
-- 作者:cnsjroom
-- 发布时间:2022/12/15 11:40:00
-- 回复:(有点蓝)到数据库里看看最新的编号保存后有没...
经核查 代码运行后 对应的第一个编号是保存到了数据库中的编号字段的
还得麻烦老师 指导下:或者是将下述代码 用其他方式实现类似效果
Dim dr As DataRow = rq.result \'返回当前保存的数据行,可再处理数据 If dr IsNot Nothing Then Dim lb As String = dr("分类代码") Dim idx As Integer Dim cmd As New SQ LC ommand cmd.Conn ecti onN ame = "主数据源" cmd.CommandText = "Se lect * From 台账 where 分类代码 = \'" & lb & "\' And [_Identify] <>\'\'" Dim dt As DataTable = cmd.ExecuteReader() Dim dr1 As DataRow If dr1 IsNot Nothing Then dr1= dt.DataRows(dt.DataRows.Count - 1) \'获得最后一行 output.show(dr1("_Identify")) cmd.CommandText = "sele ct Max(编号) from 台账 where 分类代码 = \'" & lb & "\' And [_Identify] <> " & dr1("_Identify") Dim max As String max = cmd.ExecuteScalar() If max > "" Then \'如果存在最大编号 idx = CInt(max.Substring(max.length - 3)) + 1 \'获得最大编号的后三位顺序号,并加1 Else idx = 1 \'否则顺序号等于1 End If dr("编号")=lb & Format(idx,"000") Else idx = 1 \'否则顺序号等于1 dr("编号")=lb & Format(idx,"000") End If dr.Save End If
|
-- 作者:cnsjroom
-- 发布时间:2022/12/15 12:41:00
-- 回复:(有点蓝)学会调试 If dr1 IsNot Nothing...
当前代码如下:
Public Sub form673_save_Ajax(rq As Request) Me.tablesave(rq) \'保存数据表 Dim dr As DataRow = rq.result \'返回当前保存的数据行,可再处理数据 If dr IsNot Nothing Then Dim lb As String = dr("分类代码") Dim idx As Integer Dim cmd As New SQLCommand cmd.Conn ect ion Name = "主数据源" cmd.CommandText = "Se lect * From 办案装备_管理台账 where 分类代码 = \'" & lb & "\' And [_Identify] <>\'\'" Dim dt As DataTable = cmd.ExecuteReader() If dt.DataRows.Count>1 Then Dim dr1 As DataRow dr1= dt.DataRows(dt.DataRows.Count - 1) \'获得最后一行 If dr1 IsNot Nothing Then output.show(dr1("_Identify")) cmd.CommandText = "sele ct Max(编号) from 办案装备_管理台账 where 分类代码 = \'" & lb & "\' And [_Identify] <> " & dr1("_Identify") Dim max As String max = cmd.ExecuteScalar() If max > "" Then \'如果存在最大编号 idx = CInt(max.Substring(max.length - 3)) + 1 \'获得最大编号的后三位顺序号,并加1 Else idx = 1 \'否则顺序号等于1 End If Else idx = 1 \'否则顺序号等于1 End If dr("编号")=lb & Format(idx,"000") dr.Save End If End If rq.e.WriteString(rq.msg.ToString) End Sub
运行后出现两个重号后会新增一个号
此主题相关图片如下:11.png
上述问题有没有办法解决呢?或者有没有直接SQL语句可以直接实现对应的编号按类别不同递增呢?
|
-- 作者:cnsjroom
-- 发布时间:2022/12/15 12:56:00
-- 回复:(有点蓝)学会调试 If dr1 IsNot Nothing...
代码二:
Public Sub form673_save_Ajax(rq As Request) Me.tablesave(rq) \'保存数据表 Dim dr As DataRow = rq.result \'返回当前保存的数据行,可再处理数据 If dr IsNot Nothing Then Dim lb As String = dr("分类代码") Dim idx As Integer Dim cmd As N ew S QLCommand cmd.Conne ctio nName = "主数据源" cmd.CommandText = "Sele ct * From 办案装备_管理台账 where 分类代码 = \'" & lb & "\' " Dim dt As DataTable = cmd.ExecuteReader() If dt.DataRows.Count>1 Then cmd.CommandText = "sel ect Max(编号) from 办案装备_管理台账 where 分类代码 = \'" & lb & "\'" Dim max As String max = cmd.ExecuteScalar() If max > "" Then \'如果存在最大编号 idx = CInt(max.Substring(max.length - 3)) + 1 \'获得最大编号的后三位顺序号,并加1 Else idx = 1 \'否则顺序号等于1 End If Else idx = 1 \'否则顺序号等于1 End If If dr("编号")<> Nothing Then Else dr("编号")=lb & Format(idx,"000") End If dr.Save End If rq.e.WriteString(rq.msg.ToString) End Sub
|