| | |
Check Whether Username alraedy exists in database
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 324
Reputation:
Solved Threads: 7
C# Syntax (Toggle Plain Text)
public partial class FrmNewUser : System.Web.UI.Page { SqlConnection conn = new SqlConnection("Data Source=SONIA-B408A4159\\SQLEXPRESS;Initial catalog=sonia;Integrated Security=true"); string query; SqlCommand cmd; public string name="Code"; protected void Page_Load(object sender, EventArgs e) { btnRegister.Attributes.Add("onclick", "javascript:return LenofPassword()"); } protected void btnRegister_Click(object sender, EventArgs e) { try { query = "Insert into Newuser values(@UserName,@Password,@FullName)"; cmd = new SqlCommand(query, conn); cmd.Parameters.AddWithValue("@UserName", txtUsername.Text); cmd.Parameters.AddWithValue("@Password", txtPassword.Text); cmd.Parameters.AddWithValue("@FullName", txtFullname.Text); conn.Open(); cmd.ExecuteNonQuery (); conn.Close (); } catch(Exception ex) { lblErrors.Text =ex.Message.ToString(); } }
C# Syntax (Toggle Plain Text)
<title>New User Registration</title> <script type ="text/javascript" language ="javascript" > function LenofPassword() { var Password=document.getElementById("txtPassword"); if(Password.value == "") { alert("Password should not be empty"); Password.focus(); return false; } if (Password.value.length <= 6 ) { alert("Password length should be greater than 6"); Password.focus(); return false; } var ConfirmPass=document.getElementById("txtConfirmPass"); if(ConfirmPass.value == "") { alert("Confirm Password should not be empty"); ConfirmPass.focus(); return false; } if( Password.value != ConfirmPass.value) { alert(" Password & Confirm Password do not match"); return false; } }
How Can i Check in JS Function that mine UserName already exists in database or not...
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
Well other than this is NOT a java forum, this is for c# and theres a .net web forum too.. The first thing is that you would have to use some form of ajax type thing as you would need to ask the db if the usernames there - but best advice, ask in a more apprioriate forum.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
I think he crosses 3 forums with this question since he didn't ask a specific question (c#, asp.net, sql).
BUT since you have the mechanism down to execute a query, I don't think its c#. Since you have the page designed, I don't think its asp.net. That leaves SQL (and how you deal with it in c#).
Assuming you're using MSSQL you can do something like (I don't have a compiler with me, so this hasn't been checked for compilation issues):
If the rowcount returns 0 then the username existed, if it returns 1 then you know it didn't. As a recommendation for best practices you should specify the columns in your Insert Into() statement since they're likely to change over time, which could break your query.
BUT since you have the mechanism down to execute a query, I don't think its c#. Since you have the page designed, I don't think its asp.net. That leaves SQL (and how you deal with it in c#).
Assuming you're using MSSQL you can do something like (I don't have a compiler with me, so this hasn't been checked for compilation issues):
c# Syntax (Toggle Plain Text)
try { query = "Insert into Newuser Select @UserName,@Password,@FullName Where Not Exists (Select * From NewUser x Where x.[UserNameColumn] = @UserName) Select Cast(@@ROWCOUNT as int) As Result"; cmd = new SqlCommand(query, conn); cmd.Parameters.AddWithValue("@UserName", txtUsername.Text); cmd.Parameters.AddWithValue("@Password", txtPassword.Text); cmd.Parameters.AddWithValue("@FullName", txtFullname.Text); conn.Open(); int rowCnt = Convert.ToInt32(cmd.ExecuteScalar()); conn.Close (); } catch(Exception ex) { lblErrors.Text =ex.Message.ToString(); }
If the rowcount returns 0 then the username existed, if it returns 1 then you know it didn't. As a recommendation for best practices you should specify the columns in your Insert Into() statement since they're likely to change over time, which could break your query.
![]() |
Other Threads in the C# Forum
- Previous Thread: Unknown Error
- Next Thread: Enumerations...displaying their content
| Thread Tools | Search this Thread |
.net access ado.net algorithm array backup barchart bitmap box broadcast buttons c# check checkbox client color combobox control conversion csharp custom database datagrid datagridview dataset datetime decryption degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ httpwebrequest i18n image imageprocessing index input install java label list listbox listener mandelbrot math microsystems mouseclick mysql operator path photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer update uploadatextfile usercontrol users validation view visualstudio webbrowser whileloop windows winforms wpf xml






