title: How can i add auto increment in vb.net with date, year and alphabets
body: <p>codes are like this. But instead of getting new number im getting Feb-16-INV-00011 and Feb-16-INV-000111 and Feb-16-INV-0001111 </p>
  
  <pre><code>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 &amp; "-" &amp; ynow &amp; "-"
  
      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 &amp; "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
  </code></pre>
  
  <p>Now I Tried this but no luck.... Conversion from type 'DBNull' to type 'String' is not valid. Function of getnextvalue Error.</p>
  
  <pre><code>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 &amp; GetNextValue.ToString
      Return result
  
      ConObj.Close()
  End Function
  </code></pre>