February 26, 2009 at 12:21 pm
This is bordering on being an ASP.NET question, but since I'm getting no joy on that forum, and it does distinct revolve around SQL, I thought I'd give it a shot here. I'm very much a novice at this, so please bear with me. I've "inherited" a project from someone with whom I no longer have contact. I've been trying (successfully up to now) to re-engineer some of the application but have run into a problem since i endeavoured to capture some personal information from users when they register as a user on the site.
I cannot easily debug wit the MS Visual Web Developer 2005 Express that I'm using, since there are certain elements contained within the code (like CR Public Key Tokens) which cause the app not to run locally. I've been having to upload my code to the remote server and debug by trial and error.
Anyways, here goes....
I was capturing a UserID and Password, but since I have tried to also capture a name and email address, my app will not write to the database. Nor does it trap when I try to register an already existing user (hardly surprising if it's not writing to the database, but I thought I'd mention it anyway). The app does not throw up an error but continues to the proper page as if it has worked. Can anyone tell me if I've done anything wrong in the SQL code please?
There are three areas BELOW (CreateUser.aspx.vb, DataAccess.vb and DataAccessBase.vb) where I changed code to include my new fields - these are commented out with three *** (to differentiate from the usual comments).
Part of CreateUser.aspx.vb.....
Protected Sub CreateUserButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreateUserButton.Click
If Page.IsValid Then
Dim success As Boolean = False
CreateUserButton.Enabled = False
Try
'''*** success = _dal.CreateNewUserAccount(UserNameTxt.Text, PasswordTxt.Text, EmailTxt.Text, FirstNameTxt.Text, SurnameTxt.Text)
success = _dal.CreateNewUserAccount(UserNameTxt.Text, PasswordTxt.Text)
Catch ex1 As UserAlreadyExistsException
log.Error(ex1.Message, ex1)
InfoLabel.Visible = True
InfoLabel.ForeColor = Color.Red
InfoLabel.Text = "The user name " & UserNameTxt.Text & " already exists. Please choose a new user name and try again"
Catch ex2 As UserCreationFailedException
log.Error(ex2.Message, ex2)
InfoLabel.Visible = True
InfoLabel.ForeColor = Color.Red
InfoLabel.Text = "New user creation failed for an unspecified reason."
Finally
If success Then
InfoLabel.Text = "User successfully created."
End If
Response.Redirect("Login.aspx?NewUser=" & UserNameTxt.Text)
End Try
End If
================================================================================
Part of DataAccess.vb......
'''
''' Attempts to create a new user account. Throws UserAlreadyExistsException exception if duplicate user name
'''
'''
'''
'''
'''
'''***Public Overrides Function CreateNewUserAccount(ByVal userName As String, ByVal pass As String, ByVal eml As String, ByVal firstName As String, ByVal surname As String) As Boolean
Public Overrides Function CreateNewUserAccount(ByVal userName As String, ByVal pass As String) As Boolean
Dim strSQL As String = "SELECT count(UserName) FROM " & USERS_TABLE_NAME & " WHERE UserName = @UserName"
Dim conn As New SqlConnection(Me.ConnectionString)
Dim cmd As New SqlCommand(strSQL, conn)
cmd.Parameters.AddWithValue("@UserName", userName)
cmd.Parameters.AddWithValue("@Password", pass)
conn.Open()
If CInt(cmd.ExecuteScalar()) > 0 Then
Throw New UserAlreadyExistsException
End If
conn.Close()
cmd.Dispose()
'''***strSQL = "INSERT INTO " & USERS_TABLE_NAME & "(UserName, Password, eml, FirstName, Surname) VALUES(@UserName, @Password, @email, @FirstName, @Surname)"
strSQL = "INSERT INTO " & USERS_TABLE_NAME & "(UserName, Password) VALUES(@UserName, @Password)"
cmd = New SqlCommand(strSQL, conn)
cmd.Parameters.AddWithValue("@UserName", userName)
cmd.Parameters.AddWithValue("@Password", pass)
'''***cmd.Parameters.AddWithValue("@email", eml)
'''***cmd.Parameters.AddWithValue("@FirstName", firstName)
'''***cmd.Parameters.AddWithValue("@Surname", surname)
Try
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
Return True
Catch ex As Exception
log.Error(ex.Message, ex)
Throw New UserCreationFailedException()
End Try
End Function
===============================================================================
Part of DataAccessBase.vb . . . .
#Region "Login Related Function Prototypes"
'''
''' Prototype for function used to create new user account
'''
'''
'''
'''
'''
Public MustOverride Function CreateNewUserAccount(ByVal userName As String, ByVal pass As String) As Boolean
'''***Public MustOverride Function CreateNewUserAccount(ByVal userName As String, ByVal pass As String, ByVal eml As String, ByVal firstName As String, ByVal surname As String) As Boolean
#End Region
February 27, 2009 at 6:22 am
I'm not a .NET guru by any means, but doesn't this code:
Finally
If success Then
InfoLabel.Text = "User successfully created."
End If
Response.Redirect("Login.aspx?NewUser=" & UserNameTxt.Text)
End Try
Always redirect you to Login.aspx regardless of the outcome of your user creation? What does Login.aspx do when the NewUSer parameter is an Empty String?
Are you able to set up a trace or profiler session against the SQL Server to verify that your SQL statement is being passed appropriately?
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply