Click here to Skip to main content
15,888,238 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my Product Details Windows Form, there is a column like Product ID and much more. I used textBox1 control for Product ID column. I want to create the Product ID automatically in the Load Event or Validating Event like this: 02112013/PID0001. When the user will submit the 1st Product ID(02112013/PID0001) and other details to SQL Server 2005 database. After submitting the 1st Product ID(02112013/PID0001) whenever the user will open the Product Details form, the Product ID should be created automatically in the Load Event or Validating Event like: 02112013/PID0002.
I wrote code in the Validating event of the textBox1 like this:
C#
private void textBox1_Validating(object sender, CancelEventArgs e)
       {
           DateTime dt = DateTime.Now;
           int mon = dt.Month;
           int year = dt.Year;
           int day = dt.Day;
           string id1, id2;
           int id;
           SqlConnection con = new SqlConnection(Class1.cs);
           con.Open();
           string query = "select 'Max_No'=max(ProductID) from Product_Details";
           SqlCommand cm = new SqlCommand(query, con);
           SqlDataReader dr = cm.ExecuteReader();
           dr.Read();
           {
               if (dr[0].ToString() == "")
               {
                   id1 = "PID0000";
               }
               else
               {
                   id1 = dr[0].ToString();
               }
               id2 = id1.Substring(3, 4);
               id = Convert.ToInt32(id2);

               if ((id >= 0) && (id < 9))
               {
                   id = id + 1;
                   textBox1.Text = day.ToString() + mon.ToString() + year.ToString() + "/" + "PID000" + id;
               }
               else if ((id >= 9) && (id < 99))
               {
                   id = id + 1;
                   textBox1.Text = day.ToString() + mon.ToString() + year.ToString() + "/" + "PID00" + id;
               }
               else if ((id >= 99) && (id < 999))
               {
                   id = id + 1;
                   textBox1.Text = day.ToString() + mon.ToString() + year.ToString() + "/" + "PID0" + id;
               }
               dr.Close();
           }
           con.Close();
       }

The first Product ID is submitted successfully, when I start for the second it does not create the 2nd Product ID.
I am unable to find out the reason. Please someone help me.
Posted
Updated 1-Nov-13 18:53pm
v2

1 solution

Firstly using
select 'Max_No'=max(ProductID) from Product_Details
is unsafe in multi-user environments, and not efficient on larger tables.

You would be better off defining a column on your table as an auto-increment ID ... see msdn documentation here[^]

As to your problem with the 2nd Product ID with the code that you have ... do you actually update the database with each new ID ... if not then your select statement is always going to return zero.
 
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