user3422209
user3422209

Reputation: 195

Convert from String to integer without truncating zeroes at the left side

I have three strings. 1. str1="450" 2. str2="SKDR" 3. str3="008001". I want to join these three strings. Each time str3 value should be incremented by 1. If we take it as integer then intial zeroes will be truncated.

      Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click


    Dim d1 As String = ""
    Dim s As String = ""
    Dim dtqry1 As Integer = 0
    Dim br As String = dgvCanaraBankCAU.CurrentRow.Cells(2).Value.ToString
    Dim pro As String = dgvCanaraBankCAU.CurrentRow.Cells(3).Value.ToString
    Dim dtqry As DataTable = mainModule.DatabaseTableQuery("SELECT DPNO,StartNumber from Master_CINumber where Branch='" + br + "' and ProjectOffice='" + pro + "'")
    If dtqry.Rows.Count > 0 Then
        For Each dr As DataRow In dtqry.Rows
            d1 = dr(0).ToString
            s = dr(1).ToString
        Next
        Dim chk As DataTable = mainModule.DatabaseTableQuery("Select * from CBEntry where BankBranch='" + br + "' and ProjectOffice='" + pro + "'")
        If chk.Rows.Count = 0 Then
            dtqry1 = Val(s)
        Else
            dtqry1 = mainModule.DatabaseScalarQuery("SELECT max(startNumber) from CBEntry where BankBranch='" + br + "' and ProjectOffice='" + pro + "'")
            dtqry1 = dtqry1 + 1
        End If
    End If
End Sub

Upvotes: 0

Views: 129

Answers (1)

Richard
Richard

Reputation: 109015

Use a custom format string passed to In32.ToString to include leading zeros.

Eg.

Dim x = 42
Console.WriteLine(x.ToString("000000"))

will output

000042

Upvotes: 2

Related Questions