Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
While entering the data in textbox, it should check from database whether username exists or not.

How can I write with ASP.NET validations.
Posted
Updated 17-Sep-14 10:19am
v2
Comments
Nelek 17-Sep-14 5:41am    
Get used to write proper english please. Chat-SMS-Text makes your message looks not serious, on the other hand... not all people are english speakers and may not understand what you say
Afzaal Ahmad Zeeshan 17-Sep-14 16:20pm    
These are not the ASP.NET validations that will help here, but a simple business logic. That you will create to look for the data inside the Database.

 
Share this answer
 
CSS
send the text box value to data base

and store the result in dataset

if(ds.rows.count>0)
{
//exist
}

else{
//not exist
}
 
Share this answer
 
SQL
I don't think Asp.Net validation can validate this type of occurrence.
if can then i don't know.
only i can suggest you that you can use ajax/jquery to validate similar to this.


using ajax/jquery to call the code-behind method where you doing the validation check and returning the result.
 
Share this answer
 
That depends on what type of Login service you're providing. If you're providing the default WebSecurity service. Then you can simply just search inside the Database table and get the results. For example

C#
// create the connection
var db = Database.Open("DatabaseName");
// create the query string and get the result using the parameters
var searchQuery = "SELECT * FROM UserProfile WHERE LOWER(Username) = LOWER(@0)";
// get the results for that username.
var results = db.Query(searchQuery, username);


..now results would contain an enumeration. That would have the rows that were returned. Obviously there will be one row returned if there is a match. If there is no match then no result would be returned. You can see that using this code block.

// if a result was returned
if(results.Count > 0) {
   // generate an error
   <div>Sorry, this username is being used.</div>
   // otherwise
} else {
   // continue the process.
}


As I have said, it depends on your requirements.

Code example from ASP.NET Team

If you're using WebMatrix or Visual Studio you can create a new website and then go to the Accounts -> Register.cshtml page there you can see the usage of this process. They search for a user with the email address specified and then if the result is zero. They continue the process otherwise show an error saying email already exists.

StarterSite ASP.NET template for Web Pages has this functionality.

You don't need to be having a jQuery or JavaScript ajax request just to check. You can do this job on a simple HTTP POST or GET request too. You just need to search for the data inside your database and if no data is returned from SQL, username is available, if there is a result then the username is already taken.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900