Click here to Skip to main content
16,004,479 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to auto generate ID with charactor in VB.Net?
for Example : "C-00001", "SV-1"

if this method is correct then how to contain charactor in this Method:
( for Example : "AREA-1" )

VB
Private Sub auto()

        con = New SqlConnection(cs)
        con.Open()

        Dim ct As String = "select Max(NUM) as NUMS from AREAs"
        cmd = New SqlCommand(ct)
        cmd.Connection = con

        rdr = cmd.ExecuteReader()
        rdr.Read()

        If rdr("NUMS").ToString() <> "" Then
            txtSerID.Text = Integer.Parse(rdr("NUMS").ToString()) + 1
        Else
            txtSerID.Text = 1
        End If
        rdr.Close()
        If con.State = ConnectionState.Open Then
            con.Close()
        End If
        con.Close()
    End Sub


If this method is correct/OK then how to contain character in this Method:
( for Example : "AREA-1"
Posted
Updated 7-Nov-15 1:49am
v3
Comments
Maciej Los 7-Nov-15 7:51am    
What database: SQL Server?

1 solution

You should obtain such of data from sql server side.

Take a look here: Auto Incremented Column With VARCHAR and NVARCHAR Data Type in SQL[^]
 
Share this answer
 
Comments
[no name] 7-Nov-15 15:42pm    
A 5. Even I have to say "Auto generated" for me should be _only_ sequence nowadays. A thing my old load "interbase" supported it since the beginning -mas o menos- it was called Generator :-)
Maciej Los 8-Nov-15 6:33am    
Thank you, Bruno.
Maciej Los 8-Nov-15 7:06am    
As i mentioned, you should obtain such of data from SQL Server side. On client side - in mulituser environment - you wouldn't be able to control unique data. But - in single user environment - you can use Linq. See the example:


Dim dt As DataTable = New DataTable()

dt.Columns.Add(New DataColumn("ID", GetType(Integer)))
dt.Columns.Add(New DataColumn("CategoryName", GetType(String)))
dt.Columns.Add(New DataColumn("ProductName", GetType(String)))

dt.Rows.Add(New Object(){10, "Car", "VW"})
dt.Rows.Add(New Object(){100, "Mobile", "Samsung"})
dt.Rows.Add(New Object(){1001, "Screen", "LG"})


Dim carid = dt.AsEnumerable() _
.Where(Function(c) c.Field(Of String)("CategoryName") = "Car") _
.Select(Function(c) c.Field(Of Integer)("ID")) _
.Max()

Console.WriteLine("New car id is: Car{0}", carid + 1)

On the same basis you can obtain new id for each category.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900