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 7793323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:27:03+00:00 2026-06-01T22:27:03+00:00

I am trying to allow people to login using their UserID(int) or their username

  • 0

I am trying to allow people to login using their UserID(int) or their username (string). The way I thought about doing this is creating two seperate methods, one that accepts a string, and one that accepts an int. But I could not get this to work and it seems there might be a better solution.

public static string ForgotPassword ( string username )
{
    using ( var conn = new SqlConnection( GetConnectionString() ) )
    using ( var cmd = conn.CreateCommand() )
    {
        conn.Open();
        cmd.CommandText =
        @"SELECT 
             Password 
         FROM 
             Distributor
         WHERE 
             Username = @username";
        cmd.Parameters.AddWithValue( "@username", (string)username );
        using ( var reader = cmd.ExecuteReader() )
        {
            if ( !reader.Read() )
            {
                // no results found
                return null;
            }
            return reader.GetString( reader.GetOrdinal( "Password" ) );
        }
    }
}

public static string ForgotPassword ( int username )
{
    using ( var conn = new SqlConnection( GetConnectionString() ) )
    using ( var cmd = conn.CreateCommand() )
    {
        conn.Open();
        cmd.CommandText =
        @"SELECT 
             Password 
         FROM 
             Distributor
         WHERE 
             DistributorID= @username";
        cmd.Parameters.AddWithValue( "@username", (int)username );
        using ( var reader = cmd.ExecuteReader() )
        {
            if ( !reader.Read() )
            {
                // no results found
                return null;
            }
            return reader.GetString( reader.GetOrdinal( "Password" ) );
        }
    }
}

What is the best way to allow them to login with a user_id or a username?

  • 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-01T22:27:05+00:00Added an answer on June 1, 2026 at 10:27 pm

    In your first method, I would get hold of the userId from that username, then pass that to the 2nd method.

    If a username doesn’t match a UserID, then return an error in the same way you would if the UserID doesn’t exist.

    public static string ForgotPassword ( string username )
    {
        int userid = //Get userid from username
        return ForgotPassword(userid);
    }
    
    public static string ForgotPassword ( int username )
    {
        using ( var conn = new SqlConnection( GetConnectionString() ) )
        using ( var cmd = conn.CreateCommand() )
        {
            conn.Open();
            cmd.CommandText =
            @"SELECT 
                 Password 
             FROM 
                 Distributor
             WHERE 
                 DistributorID= @username";
            cmd.Parameters.AddWithValue( "@username", (int)username );
            using ( var reader = cmd.ExecuteReader() )
            {
                if ( !reader.Read() )
                {
                    // no results found
                    return null;
                }
                return reader.GetString( reader.GetOrdinal( "Password" ) );
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a BMI calculator. This should allow people to use either
Im trying to allow users to login to a website by verifying if they
I'm trying to post a link + status through my app, using this "popup"
I am trying to build a tool that allow people to get word or
I am trying to build a django form wizard to allow people to register
I am trying to allow a group of users to be able to login
I'm trying to implement a simple plugin system which will allow people to write
I'm trying to code a site that will allow multiple people to be working
I'm trying to create a Gmap to allow people to geocode an address and
I'm trying to allow users to register on my web application via Facebook. As

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.