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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:31:51+00:00 2026-05-28T17:31:51+00:00

Using SQL Membership Provider for ASP.NET membership. I’m using first.last as the username, which

  • 0

Using SQL Membership Provider for ASP.NET membership. I’m using first.last as the username, which is created programmatically from the user details filled in on a form.

When user submits the form, I want to be able to check if the username exists, and change it to username1 if it does, check username1 exists, and make it username2 if it exists, etc. until it is a unique username.

I don’t know how to do stored procedures, so I’m trying to use a SQLDataReader to check if username exists.

The problem is my loop. The logic is basically to set a boolean and keep looping and adding 1 to the counter, until it doesn’t find a duplicate. I have stepped through this many times, and even when it sets the boolean to false, it keeps looping.

Ideas please?

Code behind:

protected void Membership_add()
{
    SqlConnection con = new SqlConnection(connectionString);
    string NewUserNameString = FirstName.Text + "." + LastName.Text;

    //Check for duplicate aspnet membership name and add a counter to it if exists
    // Check for valid open database connection before query database
    bool match = true;
    SqlDataReader _SqlDataReader = null;
    string TestNameString = NewUserNameString;
    string selectDupeString = "SELECT UserId FROM aspnet_Users WHERE UserName = '" + TestNameString + "'";
    SqlCommand SQLdatareaderCmd = new SqlCommand(selectDupeString, con);
    int UserNameCounter = 0;
    con.Open();
    while (match = true)
    {
        //Open the connection

        try
        {
            //Read the table
            _SqlDataReader = SQLdatareaderCmd.ExecuteReader();
        }
        catch (Exception ex)
        {
            lblDatareaderEx.Text = "An Exception occurred. " + ex.Message + " " + ex.GetType().ToString();
        }

        if (_SqlDataReader.HasRows)
        {
            //match = true;
            //increase counter by 1 for each record found and change First.Name to First.Namex
            TestNameString = NewUserNameString;
            UserNameCounter = UserNameCounter + 1;
            TestNameString = TestNameString + UserNameCounter.ToString();
            _SqlDataReader.Close();
            _SqlDataReader.Dispose();
            selectDupeString = "SELECT UserId FROM aspnet_Users WHERE UserName = '" + TestNameString + "'";
            SQLdatareaderCmd = new SqlCommand(selectDupeString, con);
        }
        else
        {
            // close sql reader
            _SqlDataReader.Close();
            _SqlDataReader.Dispose();
            //get out of loop
            match = false;
        }
    }
    con.Close();
    con.Dispose();
}
  • 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-05-28T17:31:52+00:00Added an answer on May 28, 2026 at 5:31 pm

    This line:

    while (match = true)
    

    does an assignment.
    If you want your code to work you have to do a comparison:

    while (match == true)
    

    Or, since your variable is already a bool, you can just use the variable directly:

    while(match)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently using the SQL Membership provider for ASP.NET, which uses GUIDs for the
Example scenario in an ASP.NET application using SQL Server membership provider : 1) a
I have an ASP.NET 3.5 Web Site using the standard SQL Membership Provider. The
I'm using the ASP.NET SQL Membership Provider. So, there's an aspnet_Users table that has
I'm using linq to SQL along with asp.net membership provider. I have three roles
We're using ASP.Net membership providers (the SQL Server provider), and I want to be
I am using the default sql membership provider with ASP.NET and I would like
I'm having a problem with a site where I'm using the ASP.NET SQL Membership
I'm using asp.net's built-in membership provider with security question-and-answer enabled for password recovery against
I am developing asp.net mvc2 application and I use asp.net membership provider which uses

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.