How to suggest available username04:33

  • 0
Published on March 12, 2017

Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video

In this video we will discuss, how to suggest available username using asp.net web services and jquery ajax.

This is continuation to Part 65, please watch Part 65 before proceeding.

When you try to create a gmail account, if the username that you have provided is already taken by another user, you will be suggested with a username that is available. Once you click on the available username, the username textbox will be populated with the suggested username.g

Let us now discuss how to achieve this using asp.net web services and jQuery AJAX.

Step 1 : Modify RegistrationService.asmx.cs as shown below.

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
using System.Web.Services;

namespace Demo
{
[WebService(Namespace = ”
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]

[System.Web.Script.Services.ScriptService]
public class RegistrationService : System.Web.Services.WebService
{
public bool UserNameExists(string userName)
{
string cs = ConfigurationManager.ConnectionStrings[“DBCS”].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand(“spUserNameExists”, con);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(new SqlParameter()
{
ParameterName = “@UserName”, Value = userName
});

con.Open();
return Convert.ToBoolean(cmd.ExecuteScalar());
}
}

[WebMethod]
public void GetAvailableUserName(string userName)
{
Registration regsitration = new Registration();
regsitration.UserNameInUse = false;

while(UserNameExists(userName))
{
Random random = new Random();
int randomNumber = random.Next(1, 100);
userName = userName + randomNumber.ToString();
regsitration.UserNameInUse = true;
}

regsitration.UserName = userName;
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(regsitration));
}
}
}

Enjoyed this video?
"No Thanks. Please Close This Box!"