auto incrementing a string?

doc7i9

Member
Joined
Jan 27, 2011
Messages
8
Programming Experience
Beginner
i'm new in vb.net
just began studying it recently and there's one problem i encountered,

i'm doing this for auto incrementing


i called the last value in a row from a database
for say, its p-0004

what i wanna do is take p-0004 and add 1 into it to make it p-0005
sorry for my stupidity, i know this is a noobie question but hey, i'm a noob :D

i tried using the trim method i used in vb 6 but there was an error like "read only property" something
 
p-0005 is a string, not a number, and you can't add 1 to a string. First you need to extract the numeric characters from the string, then convert the string to an integer. After you increment the integer, you need to convert it back into a string, adding on the leading zeros and the letter. For example:

VB.NET:
		Dim mystr As String = "p-0005"
		Dim numstr As String = mystr.Substring(2)
		Dim incnum As Integer = Convert.ToInt32(numstr)
		incnum += 1
		Dim newstr As String = incnum.ToString()
		mystr = "p-" & newstr.PadLeft(4, "0"c)
		MessageBox.Show(mystr)
 
You can provide a custom number format in the ToString call.

VB.NET:
        Dim str = "p-0009"
        Dim numStr = str.Split("-"c)(1)
        Dim num = Convert.ToInt32(numStr)
        num += 1
        str = String.Format("{0}{1}", str.Substring(0, 2), num.ToString("0000"))
 
If you were at p-0010 and it returned p-00-9 then you must have been doing something wrong. My code returned p-0011:

VB.NET:
Dim mystr As String = "p-0010"
		Dim numstr As String = mystr.Substring(2)
		Dim incnum As Integer = Convert.ToInt32(numstr)
		incnum += 1
		Dim newstr As String = incnum.ToString()
		mystr = "p-" & newstr.PadLeft(4, "0"c)
		MessageBox.Show(mystr)

Post your code.
 
rs = New Odbc.OdbcCommand
rs.Connection = con
rs.CommandText = "Select * From tblscat Where scategorycode = (Select Max(scategorycode) From tblscat)" '---to get the last data in the database
result = rs.ExecuteReader
If result.HasRows Then
butt = result.GetValue(0).ToString
ips = butt.Substring(2)
os = Convert.ToInt32(ips)
os += 1
us = os.ToString()
lblcode.Text = "SC-" & us.PadLeft(4, "0"c)
rs = Nothing
result = Nothing
End If

my senior taught me the other codes at the top, i tried to apply the code you said for auto numbering purposes,
it worked, until i get to a table wherein the last data end with 10
 
It may be that the length of your original data increases. If so, try inserting this If block (not tested, can't be sure about this):

If result.HasRows Then
butt = result.GetValue(0).ToString

If butt.Length() = 6 Then
ips = butt.Substring(2)
ElseIf butt.Length = 7 Then
ips = butt.Substring(3)
End If

os = Convert.ToInt32(ips)
os += 1
us = os.ToString()
lblcode.Text = "SC-" & us.PadLeft(4, "0"c)
rs = Nothing
result = Nothing
End If
 
i've done it, ive tried what u said but w/o the if-else
it doesnt work with the if-else statement
i substituted .substring(2) to .substring(3)

i now understood what the .substring(2) for
thx
 
i copy your code but where am i wrong..

what is the declaration of "result" and "butt"

here's my code:

VB.NET:
Connect()
        Dim cmd As New SqlCommand
        cmd.Connection = con
        cmd.CommandText = "SELECT ISNULL(max(transno),0000) FROM transactions"
        'Dim n As String
        result = cmd.ExecuteScalar
        'tno = n + 1
        'mskNo.Text = tno

        If result.HasRows Then
            butt = result.GetValue(0).ToString

            If butt.Length() = 6 Then
                ips = butt.Substring(2)
            ElseIf butt.Length = 7 Then
                ips = butt.Substring(3)
            End If

            os = Convert.ToInt32(ips)
            os += 1
            us = os.ToString()
            lblcode.Text = "SC-" & us.PadLeft(4, "0"c)
            rs = Nothing
            result = Nothing
        End If
        Disconnect()
 
here is what i got above after reading but still im having trouble..

please help me too..

VB.NET:
 Dim da As SqlDataReader
        Dim numstr, numstr2, newstr As String
        Dim incnum As Integer
        Connect()
        Dim cmd As New SqlCommand
        cmd.Connection = con
        cmd.CommandText = "SELECT * FROM tblsubscribers where acc_id = (SELECT max(acc_id) FROM tblsubscribers)"
        da = cmd.ExecuteReader
        If da.HasRows Then
            numstr = da.GetValue(0).ToString()
            numstr2 = numstr.Substring(3)
            incnum = Convert.ToInt32(numstr2)
            incnum += 1
            newstr = incnum.ToString()
            txtID.Text = "AB-" & newstr.PadLeft(4, "0"c)
            da = Nothing
            cmd = Nothing
        End If
        Disconnect()

my error is "Invalid attempt to read when no data is present."
in this line
numstr = da.GetValue(0).ToString()

but in my database i have data in acc_id and that is AB-0001..


thanks!
 
Back
Top