Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8683207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:58:41+00:00 2026-06-12T21:58:41+00:00

I am generating a random number admission no and this is my DAL public

  • 0

I am generating a random number admission no and this is my DAL

 public static int randomgen()
   {
       int id=0;
       int number = r.Next(100);
       HttpContext.Current.Session["number"] = "SN" + (" ") + number.ToString();
       SqlConnection con = DBConnection.OpenConnection();
       try
       {
           string sql1 = "select admissionno from tblstudent_details";
           SqlCommand cmd=new SqlCommand(sql1,con);
           SqlDataReader dr = cmd.ExecuteReader();
           if (dr.Read())
          {
               id = Convert.ToInt32(dr[0]);

           }
            dr.Close();
           return id;
       }

       catch (Exception)
       {

           throw;
       }
   }

and i am checking if there is any duplicate is getting generated but i am getting an error like Input string is not in a correct format?Where i am doing wrong?Is any better way than this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:58:43+00:00Added an answer on June 12, 2026 at 9:58 pm

    You asked if there is a better way…

    From what I understand about the question what you are trying to do is pick a random value and then check the database to see if that value already exists. You want to return a value back to the UI to tell the UI whether the value exists or not…

    Here is a couple alternatives to consider…

    public static bool randomgen() 
    {
        bool isFound = false;
        string admissionNumber = "SN " + r.Next(100); 
        HttpContext.Current.Session["number"] = admissionNumber; 
        using (SqlConnection con = new SqlConnection()) // use "using" to guarantee connection is closed
        {
            string sql1 = "SELECT CASE WHEN EXISTS(SELECT admissionno FROM tlblstudent_details WHERE admissionno = @admissionno) THEN 1 ELSE 0 END";
            using (SqlCommand cmd = con.CreateCommand())
            {
                 cmd.CommandText = sql;
                 cmd.Parameters.AddWithValue("@admissionno", number);
                 using (SqlDataReader dr = cmd.ExecuteReader())
                 {
                     if (dr.Read())
                     {
                         isFound = (Convert.ToInt32(dr[0]) == 1)
                      }
                  }
             }
         }
         return isFound;
    }
    

    This way you let SQL Server check to see if the value exists.

    Another approach…

    Not sure if you are required to prompt the user if the value is not unique, if that is not a requirement then I would consider a different approach; Keep trying until you find a unique value…Like this…

    public static int randomgen() 
    { 
        bool isFound = true;
        while (isFound)
        {
            string admissionNumber = "SN " + r.Next(100); 
            using (SqlConnection con = new SqlConnection()) // use "using" to guarantee connection is closed
            {
                string sql1 = "SELECT CASE WHEN EXISTS(SELECT admissionno FROM tlblstudent_details WHERE admissionno = @admissionno) THEN 1 ELSE 0 END";
                using (SqlCommand cmd = con.CreateCommand(sql1))
                {
                    cmd.Parameters.AddWithValue("@admissionno", admissionNumber);
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        if (dr.Read())
                        {
                            isFound = (Convert.ToInt32(dr[0]) == 1)
                         }
                     }
                 }
            }
            return number;
        }
    

    This keeps checking until a unique value is returned. Then you return that unique value to the calling method. Now you set HttpContent.Current session from the caller, leaving the responsibility of this method to only be finding a unique Admission Number. The downside to the second approach is that it may take a long time to find a unique value, depending on how many values are already used, especially considering you are only allowing 100 values.

    Hope this gives you good alternatives to consider. Let me know if you have additional questions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm generating a random number between min and max with this code: return min
Possible Duplicate: Generating random number in a range with Java This feels pretty silly,
I want it keeps generating different random number. How to use While statement? int
Is this how PHP implemented random number generating? Say I want to calculate a
Can someone please help with this. I am generating a random number and based
I have a vba for generating a random number. This number is generated after
Possible Duplicate: Algorithm for generating a random number is posible to generate a random
I have a problem with random number generating in C# I have tried all
I'm generating a number of circles with random positions. what I want to prevent
Possible Duplicate: Java: generating random number in a range I want to generate a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.