Click here to Skip to main content
16,017,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to set my text box as auto increment

i have two text boxes
id
name
and one button
new

i have a table in sql server 2008
user

when i click on new button
my id textbox is incremented by 1
and display in textbox

in sql server i already set the identifier increment by one

but problem is how to do this in c sharp text box
Posted
Comments
Bala Selvanayagam 13-Oct-11 8:24am    
I do not think its a good practise to show the next ID in your form. What happens for the multi- user environement.

Say you Id column in the databsae is 10 and assume two users open the form same time and both will get 11?....do not do this instead retrieve and show the ID once the record is saved.
Member 13640681 25-Apr-18 7:35am    
i want to generate a custom id in sequence in @html.TexboxFor each time when page is loaded incremented by one and each time when page is loaded it display next id based on previous added id in MVC using dbcontext. i didnt load the next incremented id in @html.TexboxFor .. please help me
the id is as :-CAT00,CAT001,CAT002 and so on ....



and thanxx in advance

don't set it on screen! Your user does not care for the number. REMOVE that textbox.
 
Share this answer
 
In form Load Event

TextBox1.text= getID().ToString();


private int getID()
{
try
{
int no=0;
SqlCommand cmd = new SqlCommand("SELECT COUNT(*) as tablename ", dbe.getConnection());
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
no = dr.GetInt32("no") + 1;
}
dr.Close();
return no;
}
catch (Exception ex)
{
MessageBox.Show("Error on generating ID.\n" + ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
return 0;
}
}
 
Share this answer
 
v2
It is very simple.copy paste the code in your page load event
string sqlText = "SELECT MAX(product_id) FROM Product ";

SqlConnection con3 = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\QXScustomerportalDB.mdf;Integrated Security=True;User Instance=True");
SqlCommand command = new SqlCommand(sqlText, con3);

try
{
con3.Open();
string pcount = Convert.ToString( command.ExecuteScalar());
if (pcount.Length == 0)
{
TextBox1.Text = "1";

}
else
{
int pcount1 = Convert.ToInt32(pcount);
int pcountAdd = pcount1 + 1;
TextBox1.Text = pcountAdd.ToString();
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con3.Close();
}
 
Share this answer
 
Comments
Member 12363157 12-Mar-16 19:07pm    
Very nice thank you very very muchhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
Try this..

int i=COnvert.toInt32(textBox1.Text);
i++;
textBox.Text=i.tostring();
 
Share this answer
 
Comments
lukeer 23-Aug-11 4:21am    
That way, you would compute a value that is computed again somewhere else (in the database). IMHO the resulting code would have a smell.

It can be correct now. But you would have to keep both computations coherent through all future changes. And most probably, something will change in the future.
as u have told that u have set id to auto increment in database .in new button click write the max query to that table which gets the maximum number from id column then add 1 to it n then display it on ur text box
 
Share this answer
 
Comments
kami124 23-Aug-11 4:10am    
here i create the code there is error at
textBoxModuleId.Text = rdr["modid_int"].ToString();

"that maximium index on a list is less then list size"


private void getMAXid()
{
System.Data.SqlClient.SqlDataReader rdr = null;
string strQ = "Select IsNULL(Max(modid_int+1), 1) ID from sec_modules";
string sql_string = ("Data Source=dfgfdgdsf;gInitial Catalog=cvfgdfg;User Id=dfgdfg;Password=dfgdfg");
SqlConnection conn = new SqlConnection(sql_string);
SqlCommand command = new SqlCommand(strQ, conn);
conn.Open();
rdr = command.ExecuteReader();
rdr.Read();
textBoxModuleId.Text = rdr["modid_int"].ToString();
rdr.Close();
}
Herman<T>.Instance 23-Aug-11 4:39am    
and ISNull and MAX in 1 query. This MUST be slow. MAX leads to a tablescan and when the table gets large..... So: Select @@Rowcount+1 from sec_modules would be better.
 
Share this answer
 

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