Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using sql server database, AgreementDB table. In that table i have a column called "AgreementNo" - Data Type "nvarchar(50)". My agreement no sample is like "ABC/AGR/001"

I want to get this agreement no last saved record + 1 ("ABC/AGR/001" + 1 = ABC/AGR/002) to textbox called txtAgreementNo. Below I show the codes that I used and when running the program there is an error "Input string was not in a correct format" with highlighting this code row
VB
txtAgreementNo.Text = Convert.ToInt64(cmd.ExecuteScalar()) + 1


please give me a solution

What I have tried:

VB
Private Sub GetLastAgreementNo()
        MysqlConn = New SqlConnection
        MysqlConn.ConnectionString = Local(0)

        Dim query = "SELECT max(AgreementNo) FROM AgreementDB"
        Dim cmd As New SqlCommand(query, MysqlConn)
        MysqlConn.Open()
        txtAgreementNo.Text = Convert.ToInt64(cmd.ExecuteScalar()) + 1
    End Sub
Posted
Updated 25-Mar-18 6:26am
v2
Comments
A_Griffin 25-Mar-18 4:11am    
What happens when you get to ABC/AGR/999 ?
Member 13745089 25-Mar-18 12:38pm    
shoulld be - ABC/AGR/1000

That's because the data you retrieve from the DB isn;t a number: you said yourself it's in the format "ABC/AGR/001" which cannot be directly converted to a number.

You need to process the data string that is returned (or better, change your DB to include a numeric field and a computed column) using either string.Substring or a Regular Expression.

But ... if you are doing this so you can generate a "free number" for the next record you want to add, I'd strongly suggest you rethink this whole design: SQL Server is a multiuser system, so generating "new" values from the latest plus one is a recipe for intermittent problems when two or more users try to do it all at once. You can seriously corrupt your database integrity that way, and it can take a huge amount of human input to sort it all out.
 
Share this answer
 
Comments
Maciej Los 25-Mar-18 12:25pm    
5ed!
Member 13745089 25-Mar-18 12:38pm    
Can you give me a solution code for this matter?
OriginalGriff 25-Mar-18 14:12pm    
Did you read any of the words that I wrote above?
Member 13745089 27-Mar-18 5:09am    
I agreed what you telling here. The only option is I have to create 2 columns for agreement No, one for the text field and other for the numeric field. then it will easy to generate. but what will happen when saving data to sql server. I mean how to divide text field and numeric field when inserting into sql server database?
OriginalGriff 27-Mar-18 5:13am    
Do it in your presentation language, where you generate the value. You have much better string manipulation facilities there - and you probably have both halves separated to start with...
 
Share this answer
 
Comments
Member 13745089 25-Mar-18 12:38pm    
Can you give me vb.net solution for this?
Maciej Los 25-Mar-18 13:19pm    
No, i can't for set of reason, but the main reason is: in multi-user environment, you'll get duplicated numbers. You have to generate AgreementNo on database level.

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