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 SharmaPosted Sep 30, 2013, 3:22 AM
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.
Sunny SharmaPosted Sep 30, 2013, 4:30 AM
rachayita jaiswalPosted Sep 30, 2013, 4:12 AM
Thank you so much, it's working fine.
Again thanks
Jaganathan BantheswaranPosted Sep 30, 2013, 3:56 AM
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
}
}
Sunny SharmaPosted Sep 30, 2013, 3:55 AM
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 !!
rachayita jaiswalPosted Sep 30, 2013, 3:49 AM
its not taking 0, its coming like "NAMTES1" only
its should come like "NAMTES00001" ...............
Sunny SharmaPosted Sep 30, 2013, 3:42 AM
-------------------------------------
txt_Objectcode.Text = str1 + str2 + String.Format("{0:00000}",Convert.ToString(intValue));
-------------------------------------
Happy Coding :)
rachayita jaiswalPosted Sep 30, 2013, 3:29 AM
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 .
rachayita jaiswalPosted Sep 30, 2013, 2:11 AM
Jaganathan BantheswaranPosted Sep 30, 2013, 1:50 AM
Use this method to get the incremented alphnumeric.