Solved

vb.net get maximum id after JPE000003

Posted on 2010-08-20
6
314 Views
Last Modified: 2012-06-27
i have 3 records in cardholder table where would like to take the last id
JPE00000001
JPE00000002
JPE00000003
and make a string call JPE0000004
with the code below,
Try
            Using objDataCtrl As New DBCore(gstrConnectionStr)

                strSQL = "SELECT * FROM cardholder"

                objReader = objDataCtrl.ExecuteReader(strSQL, "")

                If objReader.Read Then



                End If

            End Using

        Catch ex As Exception
            strError = ex.Message
        End Try

Open in new window

0
Comment
Question by:doramail05
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 3

Expert Comment

by:LDH
ID: 33483132
Try the code below.

lastid = Right("JPE0000003", 4)
lastid = CInt("1" & lastid)
lastid = lastid + 1;
lastid = "JPE" & Right(Cstr(lastid), 1)

Open in new window

0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 33483142
Modify the query to return the maximum record (that having 'JPE00000003')

Then write code like attached.

Raj
Dim sLastString As String
        Dim nLastValue As Integer

        sLastString = "JPE00000003" '' <-- From query
        sLastString = Convert.ToInt32(Right(sLastString, Len(sLastString) - 3))
        sLastString = Left(sLastString, Len(sLastString) - Len(Convert.ToString(nLastValue + 1))) & nLastValue + 1

Open in new window

0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 33483152
Assuming the first part 'JPE' is having 3 characters.

Raj
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 1

Author Comment

by:doramail05
ID: 33483156
ya, any codes for sorting out the maximum record = JPE00000003
0
 
LVL 3

Expert Comment

by:LDH
ID: 33483164
strSQL = "SELECT * FROM cardholder ORDER BY [idfield] DESC"

0
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 500 total points
ID: 33483810
Check my snippet.

Hope that helps.
Try
    Using objDataCtrl As New DBCore(gstrConnectionStr)
        strSQL = "SELECT MAX(name_of_id_field_what_contains_jpe_string) FROM cardholder"
        Dim result As String = String.Empty + objDataCtrl.ExecuteScalar(strSQL, "")
        Dim nextID As String
        If result.Length.Equals(0) Then
            nextID = "JPE000001"
        Else
            nextID = "JPE" + (Integer.Parse(result.Substring(3)) + 1).ToString("000000")
        End If
        'Right now you can check nextID variable to get your next ID
    End Using
Catch ex As Exception
    strError = ex.Message
End Try

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question