Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

User Registration – Check Username Availability

Posted on July 21, 2014October 26, 2015 By David Kittell

This is a simple example of how to check for the existence of a username prior to letting someone select a username.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UserRegistration.aspx.cs"
	Inherits="UserRegistration" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
	<title>User Registration - Check Username Availability</title>
</head>
<body>
	<form id="form1" runat="server">
	<asp:Label ID="lblUsername" runat="server">
			Username:
	</asp:Label><br />
	<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
	<asp:Label ID="lblUsernameError" runat="server" />
	<p>
		<asp:Button ID="btnSubmit" runat="server" Text="Register" OnClick="btnSubmit_Click" />
	</p>
	</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration; // Only needed if you are pulling data from web.config.

public partial class UserRegistration : System.Web.UI.Page
	{
	// Load Connection String From Web.Config
	public string SqlConnStr = ConfigurationManager.ConnectionStrings["ConnectionStringName"].ToString();

	// Windows Domain Account Access
	//public string SqlConnStr = "Server=<Server Name/IP>;Database=<Database Name>;Integrated Security=True";

	// SQL Server Account Access
	//public string SqlConnStr = "Data Source=<Server Name/IP>;Database=<Database Name>;Persist Security Info=True;User ID=<Username>;Password=<Password>";


	protected void Page_Load (object sender, EventArgs e)
		{

		}

	public bool bUserExists (string sUsername)
		{
		bool bExists = false;
		try
			{
			//Declare the connection object
			SqlConnection Conn = new SqlConnection(SqlConnStr);

			//Make the connection
			Conn.Open();

			//Declare the Command
			using (SqlCommand cmd = new SqlCommand("select count(*) from [Usertable] where UserName = @UserName", Conn))
				{
				cmd.Parameters.AddWithValue("UserName", sUsername);
				bExists = (int)cmd.ExecuteScalar() > 0;
				}

			if (bExists)
				{
				lblUsernameError.Text = "<br/><font style='color:red'>Username Exists, Please Try Another</font>";
				}
			else
				{
				lblUsernameError.Text = "<br/><font style='color:green'>Username Available</font>";
				}

			}
		catch (Exception ex)
			{

			}
		return bExists;
		}


	protected void btnSubmit_Click (object sender, EventArgs e)
		{
		bUserExists(txtUsername.Text);
		}
	}
Originally Posted on July 21, 2014
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

C# ASPX Code

Post navigation

Previous post
Next post

Related Posts

Ubuntu – Install GUI Desktop From Terminal

Posted on March 18, 2016March 18, 2016

sudo apt-get –assume-yes update && sudo apt-get –assume-yes upgrade && sudo apt-get –assume-yes autoremove sudo apt-get –assume-yes install ubuntu-desktop build-essential sudo chown -R [user-name]:[user-name] /home/[user-name] sudo startx Make sure that you do the chown command otherwise you may not be able to login. Example would be this for username demo…

Read More

Ubuntu – Remove Preloaded Applications

Posted on January 4, 2018January 10, 2020

Installing Ubuntu for a server OS? The code below will remove some of the preloaded applications that are needed on a server. Arguably you could remove Gnome in general and all Gtk but that depends on how you want to run your server. for i in aisleriot \ blinken \…

Read More

PowerShell – Get Special Path

Posted on November 9, 2015

############################################################################### ## Get-SpecialPath Function (should be an external function in your profile, really) ## This is an enhancement of [Environment]::GetFolderPath($folder) to add ## support for 8 additional folders, including QuickLaunch, and the common ## or "All Users" folders… while still supporting My Documents, Startup, etc. # FUNCTION Get-SpecialPath { param([string]$folder)…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • PowerShell - IIS Remove Site
  • SQLite - Auto-Increment / Auto Generate GUID
  • PowerShell - FTP Upload Directory With Sub-Directories

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • PowerShell - IIS Remove Site
  • SQLite - Auto-Increment / Auto Generate GUID
  • PowerShell - FTP Upload Directory With Sub-Directories
  • Raspberry Pi - Remove Default Apps
  • PowerShell - Change Windows CD/DVD Drive Letter
©2025 David Kittell | WordPress Theme by SuperbThemes