1. Back To Blog

ASP.NET interview questions: - Creating a Unique Code in ASP.NET.

This is one of the requirements that would come across you many a times while working on .NET projects. Some of the senior developers will find this question very easy but many of the developer friends find it difficult to answer the above question.

 

So we have tried to give an answer for this ASP. NET interview questions in the practical form as follows: -

 

So take a small example to see how exactly we can create a Unique Code like C001, C002......, C00n.

 

In order to see it practically you just need to follow the following steps: -

 

Step1: - Create an ASP.NET Web Application for that just open Visual Studio >> go to >> File >> New >> Project >> Web >> Select ASP.NET Empty Web Application.

 


Step2: - Now simply just add a Web Form in to your Web Application for that just go to Solution Explorer >> Right click on the Project name >> ADD >> New Item >> Select Web Form.

 

Step3: - Assume that we have the table of Customer like below diagram.

 


Step4: - Design your Web Form like below diagram.

 

 

Also add a GridView Control to your WebForm.aspx page.

 

Step5: - This is the most important step for creating Unique Customer code.

 

Add the below code snippet in to your WebForm.aspx.cs file.

 

        protected void Page_Load(object sender, EventArgs e)
        
            if (!IsPostBack)
            
                AutoGenerateCode();
            }
        }
        public void AutoGenerateCode()
        
            string Unique Code = "";
            SqlConnection con = new SqlConnection(str);
            con.Open();
            SqlCommand com = new SqlCommand();
            com.CommandText = "select CustCode from Customer where 
             CustomerID in (select max(CustomerID) from Customer)";
            com.Connection = con;
            SqlDataReader objRead = com.ExecuteReader();
            if (objRead.Read())
            
                UniqueCode = objRead[0].ToString();
                string sd = UniqueCode.Remove(0, 1);
                int sd1 = Int32.Parse(sd) + 1;
                UniqueCode = sd1.ToString();
                if (UniqueCode.Length == 3)
                
                    UniqueCode = "C" + UniqueCode;
                }
                if (UniqueCode.Length == 2)
                 
                    UniqueCode = "C" + "0" + UniqueCode; 
                }
                if (UniqueCode.Length == 1)
                
                    UniqueCode = "C" + "0" + "0" + UniqueCode; 
                }
            }
            else 
            
                UniqueCode = "C001"; 
            }
            objRead.Close();
            con.Close();
            TextBox3.Text = UniqueCode;
            //The below line will not allow 
            //the user to modify the Unique Code.
            TextBox3.ReadOnly = true;
        }

Once you have done with all the above steps simply run the application and will see the result like below diagram.

 

In the above code snippet you can clearly see that the Unique Customer Code is been generated.

 See the following video on ASP.NET Authentication, Authorization, Principal and Identity objects as follows: -

 

 

Visit for more ASP.NET interview questions

 

Regards,

 

Get more from author's blog for ASP.NET interview questions.

Shiv Prasad Koirala

Visit us @ www.questpond.com or call us at 022-66752917... read more

We are on Social