Add User to Database not working
Posted
by
user1850189
on Stack Overflow
See other posts from Stack Overflow
or by user1850189
Published on 2012-12-02T19:51:08Z
Indexed on
2012/12/02
23:04 UTC
Read the original article
Hit count: 188
I'm really new to ASP.net and I am currently trying to create a registration page on a site. I was successful in adding a user to the database but I decided to add another feature into the code to check what userID's were available. For example, if a user deleted their account their userID would become available for use again. I'm trying to find the min value and the max value and add or subtract 1 depending on whether it is min or max. I can run the code I have written for this with no errors but the user is not added to the database. Can anyone help me figure out what I'm missing from my code to do this?
EDIT >>>>> Code adds a user to database but it adds the new user at -1 instead. I don't seem to be able to see where the issue is.
If (aDataReader2.Read() = False) Then
aConnection1 = New OleDbConnection(aConnectionString)
aConnection1.Open()
aQuery = "Insert Into UserDetails "
aQuery = aQuery & "Values ('" & userID & "','" & userFName & "','" & userLName & "','" & userEmail & "','" & userUsername & "','" & userPassword & "')"
aCommand = New OleDbCommand(aQuery, aConnection1)
aCommand.ExecuteNonQuery()
aConnection1.Close()
ElseIf (min = 1) Then
aConnection2 = New OleDbConnection(aConnectionString)
aConnection2.Open()
aCommand = New OleDbCommand(aQuery3, aConnection2)
aDataReader2 = aCommand.ExecuteReader()
userID = max + 1
aQuery = "Insert Into UserDetails "
aQuery = aQuery & "Values ('" & userID & "','" & userFName & "','" & userLName & "','" & userEmail & "','" & userUsername & "','" & userPassword & "')"
aCommand = New OleDbCommand(aQuery, aConnection2)
aCommand.ExecuteNonQuery()
aConnection2.Close()
Else
aConnection3 = New OleDbConnection(aConnectionString)
aConnection3.Open()
aCommand = New OleDbCommand(aQuery2, aConnection3)
aDataReader2 = aCommand.ExecuteReader
userID = min - 1
aQuery = "Insert Into UserDetails "
aQuery = aQuery & "Values ('" & userID & "','" & userFName & "','" & userLName & "','" & userEmail & "','" & userUsername & "','" & userPassword & "')"
aCommand = New OleDbCommand(aQuery, aConnection3)
aCommand.ExecuteNonQuery()
aConnection3.Close()
lblResults.Text = "User Account successfully created"
btnCreateUser.Enabled = False
End If
Here's the code I used to get the max and min values from the database. I'm getting a value of 0 for both of them - when min should be 1 and max should be 5
Dim minID As Integer
Dim maxID As Integer
aQuery2 = "Select Min(UserID) AS '" & [minID] & "' From UserDetails"
aQuery3 = "Select Max(UserID) AS ' " & [maxID] & "' From UserDetails"
© Stack Overflow or respective owner