943,865 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 5869
  • C# RSS
Feb 22nd, 2009
0

Check Whether Username alraedy exists in database

Expand Post »
C# Syntax (Toggle Plain Text)
  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. }


C# Syntax (Toggle Plain Text)
  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...
Reputation Points: 0
Solved Threads: 8
Posting Whiz
sonia sardana is offline Offline
326 posts
since Mar 2008
Feb 22nd, 2009
-2

Re: Check Whether Username alraedy exists in database

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.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Feb 24th, 2009
0

Re: Check Whether Username alraedy exists in database

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):
c# Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Unknown Error
Next Thread in C# Forum Timeline: Enumerations...displaying their content





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC