Introduction
In my previous article
Determine Whether a User Name is Available Using jQuery and Ajax determines whether or not a user exists in the table. In this article we are learning whether or not a username exists. If the user exists in the table then some other name is suggested like Gmail. We can check whether the user exists in the table using the front end (C# Language) or the back end. I always prefer the back end(database).
First of all we need a table for holding the customer login data.
In the previous article in create a query in the .cs file, this type of query creates a SQL Injection and SQL Injection is very bad for developers. So in this article we create a procedure for connecting with the database. The procedure has one parameter @EmailID varchar(max).
- if exists(select id from mytable where emailid=@EmailId) //Check Email id already exists or not in the mytable.
- create proc Proc_CheckUserAndSuggestEmail
- (
- @EmailId varchar(max) // input Parameter
- )
- as
- begin
- if exists(select id from mytable where emailid=@EmailId)
- begin
- select Email from fun_suggest(@EmailID) //if already exists call this function with parameter
- end
- end
We need another function that returns the table. The function name is fun_suggest. The one parameter is @dataone.
- ALTER function [dbo].[fun_suggest]
- (@dataone varchar(max))
- returns @temptable table(Email varchar(max))
- as
- begin
- declare @cslice varchar(max)
- declare @notsingle int=1
- declare @five int =5
- declare @temp varchar(max)
- declare @charindexvalue int
- declare @boolvalue int=1
- SELECT @cslice= LEFT(@dataone,(CHARINDEX('@',@dataone)) - 1)
- while(@five>0)
- begin
- select @charindexvalue=CHARINDEX('_',@dataone)
-
- if(@charindexvalue=0)
- begin
- SELECT @temp=@cslice+'_'
- end
- else
- begin
- SELECT @temp=@cslice
- end
- select @five-=1
- while(@notsingle>0)
- begin
- if(@boolvalue!=1)
- begin
- select @temp=left(@temp,charindex('@',@temp)-1)
- end
- set @temp+=(select myrand from Get_Rand)
- if (CHARINDEX('@',@temp)=0)
- begin
- select @temp+=RIGHT(@dataone,(len(@dataone)-CHARINDEX('@',@dataone)+1))
- end
- if not exists(select id from mytable where Emailid=@temp)
- begin
- if not exists( select email from @temptable where Email=@temp)
- begin
- select @notsingle-=1;
- end
- end
- set @boolvalue=0
- end
- set @notsingle=1
- set @boolvalue=1
- insert into @temptable values(@temp)
- SELECT @temp=''
- SELECT @temp=@cslice+'_'
- end
- return
- end
This view always returns a number in the range 1 to 9.
- CREATE VIEW [dbo].[Get_RAND]
- AS
- SELECT CAST(CAST(RAND(CHECKSUM(NEWID())) * 9 AS int) + 1 AS varchar) AS MyRAND
Our back end work is complete. Now for the front end, in other words C# and jQuery. This project uses jQuery. We want to do it without a complete page refresh. I have the suggestion email id. So I create one function
checkuser().
- url: "Register.aspx/CheckUserName",
msg.d.split(","); return the array of data. var temp to hold the array of data. If we want to get the data one by one we need a Loop. I decided to make a loop for getting the data one by one.
- var temp = msg.d.split(",");
- document.getElementById("msgbox").innerHTML = "";
- for (var i = 0; i < temp.length; i++) {
-
- document.getElementById("msgbox").innerHTML+= ("<br/>"+temp[i]+"<br/>");
The following is the complete function of JavaScript.
- function checkuser() {
- var uname = $("#<%=txtUserName.UniqueID%>");
- if (uname.val().length > 5) {
- $.ajax({
- type: "POST",
- url: "Register.aspx/CheckUserName",
- data: "{'args': '" + uname.val() + "'}",
- contentType: "application/json; charset=utf-8",
- dataType: "json",
- success: function (msg) {
-
- if (msg.d == 'Available') {
- uname.removeClass("notavailablecss");
- uname.addClass("availablecss");
- document.getElementById("msgbox").innerHTML = "";
-
- msgbox.html('<img src="Images/a.png"> <font color="Green"> Available </font>');
-
- }
- else {
-
- uname.removeClass("availablecss");
- uname.addClass("notavailablecss");
- var temp = msg.d.split(",");
- document.getElementById("msgbox").innerHTML = "";
- for (var i = 0; i < temp.length; i++) {
-
- document.getElementById("msgbox").innerHTML+= ("<br/>"+temp[i]+"<br/>");
- }
- }
- }
- });
- }
- else {
-
- uname.addClass("notavailablecss");
- msgbox.html('<font color="#cc0000">User Name must be more than 5 characters</font>');
- }
-
-
- }
-
- /script>
And my last point is the function of a .cs file.
- [System.Web.Services.WebMethod]
- public static string CheckUserName(string args)
- {
- DataTable dt = new DataTable();
- string returnValue = string.Empty;
- SqlConnection sqlConn = new SqlConnection(@"Data Source=-----;Initial Catalog=----;Integrated Security=True");
- try
- {
- SqlCommand sqlCmd = new SqlCommand("Proc_CheckUserAndSuggestEmail", sqlConn);
- sqlCmd.CommandType = CommandType.StoredProcedure;
- sqlCmd.Parameters.AddWithValue("@EmailId", args);
- sqlConn.Open();
- SqlDataReader rd= sqlCmd.ExecuteReader();
- dt.Load(rd);
-
- if (dt.Rows.Count == 0)
- {
-
- returnValue = "Available";
- }
- else
- {
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- returnValue +=","+ dt.Rows[i]["email"].ToString();
-
- }
- }
-
- }
- catch
- {
-
- }
- finally
- {
- sqlConn.Close();
- }
- return returnValue;
- }
To check whether or not the email id exists in the table I enter the id already in the table and get the suggestion of another five email ids.
Then enter again another email id. This email id does not exist in the table.
Final word
If you have any query then drop a comment in the comment box. You can download this project with the database query.