Check Whether Username alraedy exists in database

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 324
Reputation: sonia sardana has a little shameless behaviour in the past 
Solved Threads: 7
sonia sardana sonia sardana is offline Offline
Posting Whiz

Check Whether Username alraedy exists in database

 
0
  #1
Feb 22nd, 2009
  1. public partial class FrmNewUser : System.Web.UI.Page
  2. {
  3. SqlConnection conn = new SqlConnection("Data Source=SONIA-B408A4159\\SQLEXPRESS;Initial catalog=sonia;Integrated Security=true");
  4. string query;
  5. SqlCommand cmd;
  6.  
  7. public string name="Code";
  8.  
  9. protected void Page_Load(object sender, EventArgs e)
  10. {
  11. btnRegister.Attributes.Add("onclick", "javascript:return LenofPassword()");
  12. }
  13. protected void btnRegister_Click(object sender, EventArgs e)
  14. {
  15. try
  16. {
  17. query = "Insert into Newuser values(@UserName,@Password,@FullName)";
  18. cmd = new SqlCommand(query, conn);
  19. cmd.Parameters.AddWithValue("@UserName", txtUsername.Text);
  20. cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
  21. cmd.Parameters.AddWithValue("@FullName", txtFullname.Text);
  22. conn.Open();
  23. cmd.ExecuteNonQuery ();
  24. conn.Close ();
  25. }
  26. catch(Exception ex)
  27. {
  28. lblErrors.Text =ex.Message.ToString();
  29. }
  30.  
  31.  
  32. }


  1. <title>New User Registration</title>
  2. <script type ="text/javascript" language ="javascript" >
  3. function LenofPassword()
  4. {
  5. var Password=document.getElementById("txtPassword");
  6.  
  7. if(Password.value == "")
  8. {
  9. alert("Password should not be empty");
  10. Password.focus();
  11. return false;
  12. }
  13.  
  14. if (Password.value.length <= 6 )
  15. {
  16. alert("Password length should be greater than 6");
  17. Password.focus();
  18. return false;
  19. }
  20.  
  21.  
  22. var ConfirmPass=document.getElementById("txtConfirmPass");
  23. if(ConfirmPass.value == "")
  24. {
  25. alert("Confirm Password should not be empty");
  26. ConfirmPass.focus();
  27. return false;
  28. }
  29.  
  30. if( Password.value != ConfirmPass.value)
  31. {
  32. alert(" Password & Confirm Password do not match");
  33. return false;
  34. }
  35. }

How Can i Check in JS Function that mine UserName already exists in database or not...
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Check Whether Username alraedy exists in database

 
-1
  #2
Feb 22nd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,242
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 577
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Check Whether Username alraedy exists in database

 
0
  #3
Feb 24th, 2009
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):
  1. try
  2. {
  3. 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";
  4. cmd = new SqlCommand(query, conn);
  5. cmd.Parameters.AddWithValue("@UserName", txtUsername.Text);
  6. cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
  7. cmd.Parameters.AddWithValue("@FullName", txtFullname.Text);
  8. conn.Open();
  9. int rowCnt = Convert.ToInt32(cmd.ExecuteScalar());
  10. conn.Close ();
  11. }
  12. catch(Exception ex)
  13. {
  14. lblErrors.Text =ex.Message.ToString();
  15. }

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.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC