Results 1 to 8 of 8

Thread: [RESOLVED] Help needed on web application login!

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2005
    Posts
    150

    [RESOLVED] Help needed on web application login!

    Hey guys
    I am fairly new so would be grateful if anyone could help me out...

    i am creating a web application using asp.net with vb.net code which connects to an sql database.

    I am trying to configure some code for my login section which basically figures out if a username already exists and then outputs 'username already exists' in a label.

    At present it works for the first row in the database
    i.e if my database consists of

    username <-- this is the colum name
    user
    user1
    user2

    then if I try and register a new username called 'user' then everything works and the label is updated with the 'username already exists' label.
    but if I try to pick a new username of 'user1' or 'user2' then it doesn't work and it creates a duplicate entry into the database.

    I beleive this is because I am not looping through all the usernames although I am not sure how to do this, can someone please submit the modifed code or instruct me on how to do this loop?

    my current code is :-

    strSQL = "SELECT * FROM users"
    objCmd.CommandText = strSQL
    objReader = objCmd.ExecuteReader()
    If objReader.Read() Then
    If objReader.Item("username") = userTextbox.Text Then
    levelTextbox.Text = "username already exists"
    objReader.Close()
    Else
    objReader.Close()
    strSQL1 = "INSERT INTO users VALUES ( '" + username + "' , ***other fields contianed here (not important)***)"
    objCmd.CommandText = strSQL1
    objCmd.ExecuteNonQuery()

    End If
    End If
    Last edited by eakin; Sep 27th, 2005 at 08:36 AM.

  2. #2
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: Help needed on web application login!

    im a newbie to so im not too sure,
    i think you might need a where

    i.e. select user name from users where username.text box = username

    then output already exsists
    it works 60% of the time, all the time.

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Help needed on web application login!

    Quote Originally Posted by eakin
    Hey guys
    I am fairly new so would be grateful if anyone could help me out...

    i am creating a web application using asp.net with vb.net code which connects to an sql database.

    I am trying to configure some code for my login section which basically figures out if a username already exists and then outputs 'username already exists' in a label.

    At present it works for the first row in the database
    i.e if my database consists of

    username <-- this is the colum name
    user
    user1
    user2

    then if I try and register a new username called 'user' then everything works and the label is updated with the 'username already exists' label.
    but if I try to pick a new username of 'user1' or 'user2' then it doesn't work and it creates a duplicate entry into the database.

    I beleive this is because I am not looping through all the usernames although I am not sure how to do this, can someone please submit the modifed code or instruct me on how to do this loop?

    my current code is :-

    strSQL = "SELECT * FROM users"
    objCmd.CommandText = strSQL
    objReader = objCmd.ExecuteReader()
    If objReader.Read() Then
    If objReader.Item("username") = userTextbox.Text Then
    levelTextbox.Text = "username already exists"
    objReader.Close()
    Else
    objReader.Close()
    strSQL1 = "INSERT INTO users VALUES ( '" + username + "' , ***other fields contianed here (not important)***)"
    objCmd.CommandText = strSQL1
    objCmd.ExecuteNonQuery()

    End If
    End If
    Avoid posting your question in more than one place. You'll end up with 10 different, half-answers.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2005
    Posts
    150

    Re: Help needed on web application login!

    Yea mendhak I put it in the wrong section earleir by mistake, sorry

    d2005, I'm not trying to select the username form the databse, I am trying to check all the usernames in the table to amke sure it doens't exist in which case I can write the username to the table as it will be unique or otherwise give a 'username already exists' output.

    any ideas guys?

  5. #5
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: Help needed on web application login!

    yeah
    change ure select statement
    strSQL = "SELECT * FROM users"

    put a where clause in it where textbox text = username
    that will check for all the users with that name

    i dont think u even need the object readers and stuff then u see

    if you select username from users where username = ' " & txtbox.text & " '
    then return your error

    thats just how i would go about doin it
    it works 60% of the time, all the time.

  6. #6
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: Help needed on web application login!

    I would say you're best off creating a stored procedure to do this for you

    Code:
    CREATE PROCEDURE up_InsertName
    (
      @username varchar(25), 
      @password varchar(25)
    )
    As
    If EXISTS(SELECT 'True' FROM MyTable WHERE username = @username)
    BEGIN
      --This means it exists, return it To ASP And tell us
      Return (0)
    End
    Else
    BEGIN
      --This means the record isn't In there already, let's go ahead And add it
      INSERT into MyTable(username, userpassword) VALUES(@username, @userpassword)
    End
      Return (1)
    In ASP.NET
    VB Code:
    1. Private Sub SaveNewUser()
    2.         ' Open the connection
    3.         If myCon.State = ConnectionState.Closed Then myCon.Open()
    4.  
    5.         Dim myCmd As New SqlCommand("up_InsertName", myCon)
    6.         myCmd.CommandType = CommandType.StoredProcedure
    7.  
    8.         ' Create a SqlParameter object to hold the output parameter value
    9.         Dim retValParam As New SqlParameter("@RETURN_VALUE", SqlDbType.Int)
    10.  
    11.         ' IMPORTANT - must set Direction as ReturnValue
    12.         retValParam.Direction = ParameterDirection.ReturnValue
    13.  
    14.         ' Add the parameters to the Command's Parameters collection
    15.         myCmd.Parameters.Add("@username", Username.Text.Trim.ToLower)
    16.         myCmd.Parameters.Add("@password", NewPassword.Text)
    17.         ' Finally, add the output parameter
    18.         myCmd.Parameters.Add(retValParam)
    19.  
    20.         Dim dr As SqlDataReader
    21.         ' Call the SProc...
    22.  
    23.         Try
    24.             dr = myCmd.ExecuteReader()
    25.             ' Now you can grab the output parameter's value...
    26.             Dim retVal As Integer = Convert.ToInt32(retValParam.Value)
    27.             If retVal = 1 Then
    28.                 'SaveNewUser = "OK"
    29.                 Response.Redirect("sendtonextpage.aspx", False)
    30.             Else
    31.                 'SaveNewUser = "Error"
    32.                 lblUserName.Text = "Your chosen username already exists please select another"
    33.             End If
    34.         Catch ex As Exception
    35.             ' Record any exceptions and exit
    36.             ' message is a hidden literal control
    37.             message.Visible = True
    38.             message.Text = "The following exception occurred: SavePerson<br />" + ex.Message.ToString
    39.         End Try
    40.  
    41.         dr.Close()
    42.         If myCon.State = ConnectionState.Open Then myCon.Close()
    43.     End Sub
    44.  
    45.     Private Sub cmdSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click
    46.         ' Do validation here
    47.  
    48.         ' Call Save the new user
    49.         SaveNewUser()
    50.  
    51.     End Sub

    This is a good article which I used when learning this stuff - 4GuysFromRolla

    Hope this helps, any questions let me know

    Cheers Al
    Last edited by aconybeare; Sep 27th, 2005 at 04:09 AM.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2005
    Posts
    150

    Re: Help needed on web application login!

    thanks
    problem resolved

  8. #8
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: [RESOLVED] Help needed on web application login!

    Excellent! You need to mark the thread as resolved

    At the top of this page click the item "Thread Tools" choose "Mark Thread Resolved"

    Cheers Al

    Oops I see you've already done it, please accept my apologies

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width