以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  自动编号问题  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=74466)

--  作者:cwm591825
--  发布时间:2015/9/11 16:06:00
--  自动编号问题

老师,我用下面代码生成自动编号:

If e.DataCol.Name = "订购日期" Then
    If e.DataRow.IsNull("订购日期") Then
        e.DataRow("订单号") = Nothing
    Else
        Dim bh As String = "DG" & Format(e.DataRow("订购日期"),"yyMMdd")
        If e.DataRow("订单号").StartsWith(bh) = False
            Dim max As String
            Dim idx As Integer
            max = e.DataTable.Compute("Max(订单号)","订购日期 = #" & e.DataRow("订购日期") & "# And [_Identify] <> " & e.DataRow("_Identify"))
            If max > "" Then
                idx = CInt(max.Substring(9,2)) + 1
            Else
                idx = 1
            End If
            e.DataRow("订单号") = bh & "-" & Format(idx,"00")
        End If
    End If
End If

 

但有时要用自编的订单号,如:104239

之后再用当天的日期就无法生成编号,会出现调用错误,如下:

 

.NET Framework 版本:2.0.50727.5456
Foxtable 版本:2014.11.11.1
错误所在事件:表,辅料订购单,DataColChanged
详细错误信息:
调用的目标发生了异常。
startIndex 不能大于字符串长度。
参数名: startIndex

请教代码如何修改,谢谢!


--  作者:大红袍
--  发布时间:2015/9/11 16:15:00
--  
If e.DataCol.Name = "订购日期" Then
    If e.DataRow.IsNull("订购日期") Then
        e.DataRow("订单号") = Nothing
    Else
        Dim bh As String = "DG" & Format(e.DataRow("订购日期"),"yyMMdd")
        If e.DataRow("订单号").StartsWith(bh) = False
            Dim max As String
            Dim idx As Integer
            max = e.DataTable.Compute("Max(订单号)","订购日期 = #" & e.DataRow("订购日期") & "# And [_Identify] <> " & e.DataRow("_Identify"))
            If max > "" Then
                idx = CInt(max.Substring(bh.Length+1,2)) + 1
            Else
                idx = 1
            End If
            e.DataRow("订单号") = bh & "-" & Format(idx,"00")
        End If
    End If
End If

--  作者:cwm591825
--  发布时间:2015/9/11 17:00:00
--  
老师,还是出现同样的错误!
--  作者:大红袍
--  发布时间:2015/9/11 17:11:00
--  

编号一定要符合规范,不能不一致。否则就会出错

 

If e.DataCol.Name = "订购日期" Then
    If e.DataRow.IsNull("订购日期") Then
        e.DataRow("订单号") = Nothing
    Else
        Dim bh As String = "DG" & Format(e.DataRow("订购日期"),"yyMMdd")
        If e.DataRow("订单号").StartsWith(bh) = False
            Dim max As String
            Dim idx As Integer
            max = e.DataTable.Compute("Max(订单号)","订购日期 = #" & e.DataRow("订购日期") & "# And [_Identify] <> " & e.DataRow("_Identify"))
            If max > "" AndAlso max.Length >= bh.Length +3 Then
                idx = CInt(max.Substring(bh.Length+1,2)) + 1
            Else
                idx = 1
            End If
            e.DataRow("订单号") = bh & "-" & Format(idx,"00")
        End If
    End If
End If