Click here to Skip to main content
15,891,923 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm having a column named "OptionNumber" and it has a value like op0021. I want to increment it by 1. may i know how can i?


I want to increment the max of column by 1 that is y i've written like this

dsOptionDetails.Tables[0].Rows[(dsOptionDetails.Tables[0].Rows.Count) - 1]["OptionNumber"].ToString();
Posted
Updated 7-Sep-15 4:21am
v2
Comments
Sergey Alexandrovich Kryukov 7-Sep-15 14:38pm    
What does it mean, increment a sting by 1. String is not a number. Concatenating 1 to a string is possible, but hardly what you mean do to. Interpret a string as a number? This is what pretty bad Solution 1 explains to you, but it's more important to understand: don't work with strings representing data, try to work with data itself. Work with data binding or the data in your data layer, instead of manipulating the UI.

Also, what do you mean by "ASP". If you use C#, it's probably ASP.NET, but ASP.NET is not ASP.

—SA

There is exactly this question answered in an article here:
http://www.sqlteam.com/article/string-functions-incrementing-a-number-in-a-char[^]
 
Share this answer
 
Comments
Wendelius 8-Sep-15 13:18pm    
Good link!
If you want to do that in C#, i'd suggest to use String.Substring[^] method to get numeric part of OptionNumber. Now, use Int32.Parse[^] method to convert string-number to its proper numeric value.

Try!
 
Share this answer
 
Comments
Wendelius 8-Sep-15 13:18pm    
That would do it.
Maciej Los 8-Sep-15 13:50pm    
Thank you, Mika.
I really wouldn't advice doing this. If you need a growing sequence number, store the number separately. Have a separate column containing the number and increment it by one whenever needed. Based on your example the prefix "OP" looks like a static one but if it isn't then store the prefix in another column. But the key is to have the number in a separate, single, numeric column. This will make your life much easier in the future.

To go a bit further, if the number is used to uniquely identify a record and you use a database, I would suggest letting the database to generate the next value for you. If you do it in client side code, and you store the records in a database, there is always a chance that two client applications generate the same number and you would have a collision.
 
Share this answer
 
Comments
Maciej Los 8-Sep-15 12:35pm    
Really good advice!
Wendelius 8-Sep-15 13:18pm    
Thanks!
Matt T Heffron 8-Sep-15 13:33pm    
+5
Wendelius 8-Sep-15 14:33pm    
Thank you!
Try below code:
C#
// Input op0021(value of 'OptionNumber' column)

string num = set.Tables[0].Rows[(set.Tables[0].Rows.Count) - 1]["OptionNumber"].ToString();
            num = num.Substring(2, num.Length - 2);
            int test = Convert.ToInt32(num) + 1;
            string test2 = "op" + Convert.ToString(test).PadLeft(num.Length, '0');

// Output: op0022
 
Share this answer
 
v4
Comments
phil.o 7-Sep-15 10:59am    
Given there are some letters in the string, I seriously doubt that Convert method will return what you expect. It may throw an exception instead, saying that given string is not a valid number expression.
Maciej Los 7-Sep-15 11:02am    
Seems, you misundersttod the question. OP needs to increament value like this: op0021
BTW: i'm not down-voter
[no name] 7-Sep-15 11:11am    
Updated my answer..
Maciej Los 7-Sep-15 11:26am    
A 4!

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