Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
plz help me in my project how text box can generate auto increment number when user click on insert button after filling values,,,,,,i need sequtenial values in format like C1,C2, and so on....kindly plz help me
Posted
Comments
DaveAuld 10-Sep-11 13:43pm    
What is the significance of the letter? does it change at any point?
duaa2 10-Sep-11 14:10pm    
no it doesnt change...... C indicates that id is of cargo services

make a field _nextNumber, in your code behind, to store the next number and then in your button click handler you set the text of the textbox to "C"+_nextNumber and then increment _nextNumber
 
Share this answer
 
v2
Comments
duaa2 10-Sep-11 14:11pm    
can u help me with code i didnt get sorry
You can problably use any one of the two approaches.

1. Create a static class like below
C#
public static class SequentialNumber
{
       private static int _currentNumber=0;
       public static string GetNextNumber()
       {
            _currentNumber++;
            return "C"+_currentNumber.ToString();
       }
}

and then in insert buttons click, write a code like below to get the sequential number in the text box
C#
string ContractNo=SequentialNumber.GetNextNumber();



2 approach.

Declare a variable _currentnumber in the code behind page
in the click event of the insert button write code as below.

C#
if(string.IsNullOrEmpty(ViewStatte["Num"]))
   _currentNumber=0;
else
{
   _currentNumber=(int)ViewState["Num"];
}
_currentNumber++;
ViewState["Num"]=_currentNumber;
string ContractNo=_currentNumber;


Approach 1 can be used in all types of project where as approach 2 can be used in asp.net only.

Hope this helps.

--sunil
 
Share this answer
 
Hi hope this will help You
C#
public void Method()
         {
             Connection();
             int Num1 = 0;
             cmd = new SqlCommand("Select MAX(Column) from TableName", cs);
             dr = cmd.ExecuteReader();
             while (dr.Read())
             {
                 Num1 = int.Parse(dr[0].ToString());
             }
             dr.Close();
             TextBox.Text ="C" + (Num1 + 1).ToString();
             cs.Close();


         }
 
Share this answer
 
SqlDataAdapter sda = new SqlDataAdapter("Select * from Newentry2", con);
DataTable dt = new DataTable();
sda.Fill(dt);
int i = dt.Rows.Count;
string cnt = Convert.ToString(i + 1);
textBox3.Text = "NIRU-" + cnt;
 
Share this answer
 
Comments
CHill60 22-May-13 10:01am    
Please note - It's not considered good form to post solutions to questions that were answered over 18 months ago.
add this code on your Insert button...
C#
private void btnInsert_Click(System.Object sender, System.EventArgs e)
{
	string txt = TextBox1.Text;
	TextBox1.Text = "C" + Convert.ToInt32(txt.Substring(1, txt.Length - 1)) + 1;
}
 
Share this answer
 
Comments
CHill60 22-May-13 10:01am    
Please note - It's not considered good form to post solutions to questions that were answered over 18 months ago.
Basmeh Awad 22-May-13 10:09am    
ohhh **** i havent seen really this guy(SachinRana) updated it. it came on the first page and i also updated it...bull sh*t
CHill60 22-May-13 10:18am    
I'll let you off :-) I've been caught out by that myself so I know how it feels
Basmeh Awad 22-May-13 10:25am    
hahahah;-)

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