I have created a function to generate auto increment number with string (in some format) -
Public Sub GetAutoNumber()
Dim intValue As Integer = 0
If Not ViewState("intValue") Is Nothing Then
intValue = CInt(ViewState("intValue"))
End If
intValue += 1
ViewState("intValue") = Convert.ToString(intValue)
Dim str1 As String = txtObjectProfilesName.Text // suppose value is "Testname"
Dim str2 As String = str1.Substring(0, 3)
Dim str3 As String = str2.ToUpper()
Dim str4 As String = cmbObjectProfilesObjectType.Text // suppose value is "ObjectType"
Dim str5 As String = str4.Substring(0, 3)
Dim str6 As String = str5.ToUpper()
txt_Objectcode.Text = str6 + str3 + Convert.ToString(ViewState("intValue"))
Dim test As String = txt_Objectcode.Text
End Sub
i want value like "OBJTES1" in my txt_Objectcode but i am not getting number increment like 1,2,3.... its coming everytime only OBJTES1, diffvalue1,diffvalue1
if i use code -
Public Sub GetAutoNumber()
Dim intValue As Integer = 0
If Not ViewState("intValue") Is Nothing Then
intValue = CInt(ViewState("intValue"))
End If
intValue += 1
ViewState("intValue") = Convert.ToString(intValue)
txt_Objectcode.Text = ViewState("intValue")
Dim test As String = txt_Objectcode.Text
End Sub
then i am getting auto increment in number like 1,2,3.....
is there anything wrong in code please tell me, its urgent.

Sunny SharmaRank #223 · 11.1K reputationPosted Sep 29, 2013, 11:22 PM EDT
Solution to your problem: you need to declare the intValue outside of the GetAutoNumber() method, like:
Dim intValue As Integer = 0
Public Sub GetAutoNumber()
... // your code goes here
End Sub
Now, just to shorten your code:
----------------------------------------------
Public Sub GetAutoNumber()
Dim intValue As Integer = 0
If Not ViewState("intValue") Is Nothing Then
intValue = CInt(ViewState("intValue"))
End If
intValue += 1
ViewState("intValue") = Convert.ToString(intValue)
Dim str1 As String = txtObjectProfilesName.Text.Substring(0, 3).ToUpper()
Dim str2 As String = cmbObjectProfilesObjectType.Text.Substring(0, 3).ToUpper()
txt_Objectcode.Text = str1 + str2 + Convert.ToString(intValue)
Dim test As String = txt_Objectcode.Text
End Sub
-----------------------------------------------
Hope it helps :)
Reply for any clarification.
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Sep 29, 2013, 9:50 PM EDT
Use this method to get the incremented alphnumeric.
rachayita jaiswal182 reputationPosted Sep 29, 2013, 10:11 PM EDT
rachayita jaiswal182 reputationPosted Sep 29, 2013, 11:29 PM EDT
Ya its working fine now, in place of Dim intValue As Integer = 0 i have mentioined Dim intValue As string= 0 then also it was working fine.
and can you tell me if i want to add "0000" before incremented number so how i ll do that, but i don't want to add it statically.
means auto increment number should start from "00001" and 10th number should be "00010"
like this i want, please help .
Sunny SharmaRank #223 · 11.1K reputationPosted Sep 29, 2013, 11:42 PM EDT
-------------------------------------
txt_Objectcode.Text = str1 + str2 + String.Format("{0:00000}",Convert.ToString(intValue));
-------------------------------------
Happy Coding :)
rachayita jaiswal182 reputationPosted Sep 29, 2013, 11:49 PM EDT
its not taking 0, its coming like "NAMTES1" only
its should come like "NAMTES00001" ...............
Sunny SharmaRank #223 · 11.1K reputationPosted Sep 29, 2013, 11:55 PM EDT
this is because you're passing a string. Keep it as int and you'll be fine.
txt_Objectcode.Text = str1 + str2 + String.Format("{0:00000}",intValue); // intValue is Integer here
txt_Objectcode.Text = str1 + str2 + String.Format("{0:00000}",Convert.ToInt32(intValue)); // intValue is String here.
Cheers !!
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Sep 29, 2013, 11:56 PM EDT
using System.IO;
using System;
using System.Text;
class Program
{
static void Main()
{
string var1 = "ABC";
int leadingZeros = 5;
string intString = "10";
string output = var1 + intString.PadLeft(leadingZeros, '0');
Console.WriteLine(output); // Outputs ABC00010
}
}
rachayita jaiswal182 reputationPosted Sep 30, 2013, 12:12 AM EDT
Thank you so much, it's working fine.
Again thanks
Sunny SharmaRank #223 · 11.1K reputationPosted Sep 30, 2013, 12:30 AM EDT