ASP.NET interview questions: – How to create 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 toSolution 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.

Advertisements

About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s