Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please Help to solve one kind problem
I want generate an auto id like this where ARA = ARA then display result like ARA004+1= ARA005 but displayed an error conversion from string "ARA001" to type 'Integer' is not valid. See details below
Shop_No      Block
ARA001      ARA
ARA002      ARA
ARA003      ARA
ARA004      ARA
BRA001      BRA
BRA002      BRA
BRA003      BRA
BRA004      BRA
CRA001      CRA
CRA002      CRA
CRA003      CRA
CRA004      CRA
CRA005      CRA


Shop_No Field set for text in access database and I change my dim to string still error displayed conversion from string "ARA001" to type 'String' is not valid

What I have tried:

below is my tried codes

VB
Dim IncrID As Integer
CustAutoSqlstr = "Select *From tblRevenueRegister WHERE Block='" & cbBlock.Text & "'"
CustAutodr = CustAutocmd.ExecuteReader()
        If IncrID < CustAutodr.Item(0) Then
                    IncrID = CustAutodr("Shop_No").ToString
                End If
 txtShopNo.Text = IncrID + 1
Posted
Updated 26-Feb-18 3:38am
v2

VB.NET
Dim maxValue As Object
Dim block As String = cbBlock.Text

Using con As New SqlConnection("...")
    Using cmd As New SqlCommand("SELECT Max(Shop_No) FROM tblRevenueRegister WHERE Block = @Block")
        cmd.Parameters.AddWithValue("@Block", block)
        
        con.Open()
        maxValue = cmd.ExecuteScalar()
    End Using
End Using

If maxValue Is Nothing OrElse Convert.IsDBNull(maxValue) Then
    txtShopNo.Text = "1"
Else
    Dim numberPart As String = Convert.ToString(maxValue).Substring(block.Length)
    Dim shopNumber As Integer = Convert.ToInt32(numberPart)
    txtShopNo.Text = (shopNumber + 1).ToString()
End If


Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
 
Share this answer
 
v2
Comments
Dauda Muhammad 1-Mar-18 7:35am    
Thanks you very much Richard Deeming
Your suggestion help create something like and work fine
Dim cmdAsPaid As New OleDbCommand("SELECT Count(Shop_No) FROM tblRevenueRegister WHERE Block='" & txtBlockNo.Text & "'", Cnn)
' ExecuteScalar
Dim Totals As Decimal = CDec(cmdAsPaid.ExecuteScalar())
Dim CountAsPaid As Double = String.Format(Totals)
txtShopNo.Text = txtBlockNo.Text & Microsoft.VisualBasic.Format(CountAsPaid, "000") + IncrID
Richard Deeming 1-Mar-18 8:57am    
No, that code is still vulnerable to SQL Injection. Read the links at the bottom of my answer.
Using cmdAsPaid As New OleDbCommand("SELECT Count(Shop_No) FROM tblRevenueRegister WHERE Block = ?")
    cmdAsPaid.Parameters.AddWithValue("@p0", txtBlockNo.Text)
    
    Dim Totals As Decimal = CDec(cmdAsPaid.ExecuteScalar())
    Dim CountAsPaid As Double = String.Format(Totals)
    txtShopNo.Text = txtBlockNo.Text & Microsoft.VisualBasic.Format(CountAsPaid, "000") + IncrID
End Using
In multiuser environment your code will generate duplicated Shop_no's. Stop doing this that way! You have to create custom-auto-generated sequence on server side!
See: Custom Auto-Generated Sequences with SQL Server - SQLTeam.com[^]

More at Google: sql autonumber based on another field - Google Search[^]
 
Share this answer
 

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