代碼是這樣的。而是獲得新的號碼即時得到二月-16-INV-00011和二月-16-INV-000111和二月-16-INV-0001111如何添加日期,年份和字母在vb.net中的自動增量
Private Function newautonum()
Dim NewValue As Integer
'Dim result As String
Dim resultN As String
Dim ynow As String = DateTime.Today.ToString("yy")
Dim Mnow As String = DateTime.Today.ToString("MMM")
Dim Bilnow As String = Mnow & "-" & ynow & "-"
Dim qry As String = "select MAX(ID) from tblTrns"
ConObj = New SqlConnection(ConStr)
ConObj.Open()
CmdObj = New SqlCommand(qry, ConObj)
resultN = CmdObj.ExecuteScalar().ToString()
If String.IsNullOrEmpty(resultN) Then
resultN = Bilnow & "INV-000"
' resultN = result
End If
resultN = resultN.Substring(0)
Int32.TryParse(resultN, NewValue)
NewValue = NewValue + 1
resultN = resultN + NewValue.ToString
Return resultN
ConObj.Close()
End Function
現在我試過,但沒有運氣....轉換從'DBNull'類型到'String'類型無效。 getnextvalue函數的錯誤。
Private Function newautonum() As String
Dim NewValue As Integer
Dim result As String
Dim qry As String = "Select MAX(ID) from tblTrns"
ConObj = New SqlConnection(ConStr)
ConObj.Open()
CmdObj = New SqlCommand(qry, ConObj)
result = CmdObj.ExecuteScalar().ToString()
If String.IsNullOrEmpty(result) Then
result = String.Format("{0}-INV-{1:000}", DateTime.Now.ToString("MMM-yy"), NewValue)
End If
Int32.TryParse(result, NewValue)
NewValue = GetNextValue()
result = result & GetNextValue.ToString
Return result
ConObj.Close()
End Function
你應該打開'Option Strict' – Plutonix
也試過這個。但字符$是無效的? –
使用'resultN = retval = String.Format(「{0} -INV- {1}」,DateTime.Now.ToString(「MMM-yy」),newvalue.ToString(「000」))'發佈的答案是使用VS2015功能 – Plutonix