怎么样:
txtTransID="R" & i.ToString().PadLeft(8, "0"c)
编辑:
您需要将 i 声明为您的新请求编号。
这里是示例代码。
Public Class oRs
Public Shared EOF As Boolean = False
Public Shared Fields As New Dictionary(Of String, Object)
End Class
Protected Overrides Sub Test()
oRs.Fields.Add("REQ_ID", "R00000001")
Dim txtTransID As String = Nothing
' Your code starts here '
If oRs.EOF Then
txtTransID = "R" & "00000001"
Else
Dim strOldRequest As String = System.Convert.ToString(oRs.Fields("REQ_ID"))
Dim iOldRequest As Integer = Int32.Parse(strOldRequest.Split("R"c)(1))
iOldRequest += 1
txtTransID = "R" & iOldRequest.ToString().PadLeft(8, "0"c)
End If
End Sub
如果您的经典 ASP 没有声明Int32.Parse
,请尝试CInt(YOUR_STRING)
,如果它不支持,如果您没有,object.ToString
请将其添加为正常功能:CStr(YOUR_Number)
PadLeft
Function Rpad (sValue, sPadchar, iLength)
Rpad = sValue & string(iLength - Len(sValue), sPadchar)
End Function
Function Lpad (sValue, sPadchar, iLength)
Lpad = string(iLength - Len(sValue),sPadchar) & sValue
End Function
在旁注中,为什么不将旧请求号保存为整数而不是字符串?
"R" & (iOldNr + 1).ToString().PadLeft(8, "0"c)
保持数据和它的展示分开。
如果您仍然需要在数据表中使用 R0000xy,您可以随时执行如下 SQL 查询:
SELECT
'R' + RIGHT('00000000' + CAST(REQ_ID AS varchar(20)), 8) AS RequestNr
FROM T_Requests
在另一个旁注中,您实际上并不需要任何代码,您的问题非常简单,您甚至可以完全在 SQL 中完成:
SELECT
'R' + RIGHT('00000000' + CAST(ISNULL(MAX(REQ_ID), 0) + 1 AS varchar(20)), 8) AS NextRequestNr
FROM T_Requests