Reputation: 41
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"
Upvotes: 0
Views: 211
Reputation: 112324
It's hard to say what t the problem is exactly, since we see only a part of it. Where do min and max come from?
I cannot give you a solution, however, I suggest you to structure your code better. You have a lot (!) of redundant code. This makes the code difficult to read, to understand, to change and to test.
Put the user data into a class. This makes it easier to handle than a lot of individual variables.
Public Class User
Public Property ID As Integer
Public Property FirstName As String
Public Property LastName As String
Public Property EMail As String
Public Property Username As String
Public Property Password As String
End Class
Extract redundant code into subroutines
Private Sub CreateUser(ByVal u As User)
Const InsertQuery As String = _
"INSERT INTO UserDetails VALUES ({0},'{1}','{2}','{3}','{4}','{5}')"
Dim query As String = String.Format(InsertQuery, u.ID, u.FirstName, u.LastName, _
u.Email, u.Username, u.Password)
Using conn As New OleDbConnection(aConnectionString)
conn.Open()
Dim cmd As New OleDbCommand(query, conn)
cmd.ExecuteNonQuery()
End Using
End Sub
The code then becomes something like this
If Not aDataReader2.Read() Then
CreateUser(user)
ElseIf min = 1 Then
...
user.userID = max + 1
CreateUser(user)
Else
...
user.userID = min - 1
CreateUser(user)
lblResults.Text = "User Account successfully created"
btnCreateUser.Enabled = False
End If
This looks much nicer now.
(My code is not tested, it's just to give you an idea.)
UPDATE
You cannot read the min and max values like this. Try something like this
Dim min, max As Integer
Using conn As New OleDbConnection(aConnectionString)
Dim cmd As OleDbCommand = _
New OleDbCommand("SELECT MIN(UserID), MAX(UserID) FROM UserDetails", conn)
conn.Open()
Using reader As OleDbDataReader = cmd.ExecuteReader()
reader.Read()
If reader.IsDBNull(0) Then
' The table is empty
min = 1
max = 1
Else
min = reader.GetInt32(0)
max = reader.GetInt32(1)
End If
End Using
End Using
Upvotes: 1