views:

570

answers:

2

I currently have a problem attepting to update a record within my database. I have a webpage that displays in text boxes a users details, these details are taken from the session upon login. The aim is to update the details when the user overwrites the current text in the text boxes.

I have a function that runs when the user clicks the 'Save Details' button and it appears to work, as i have tested for number of rows affected and it outputs 1. However, when checking the database, the record has not been updated and I am unsure as to why.

I've have checked the SQL statement that is being processed by displaying it as a label and it looks as so:

UPDATE [users] 
   SET [email] = @email, 
       [firstname] = @firstname, 
       [lastname] = @lastname, 
       [promo] = @promo 
 WHERE [users].[user_id] = 16

The function and other relevant code is:

Sub Page_Load(sender As Object, e As EventArgs)

usernameLabel.text = session.contents.item("UserName")

if usernameLabel.text = "" then

    logoutButton.Visible = False
    loggedInAsLabel.Visible = False

else

    labelGuest.Visible = False
    linkLogin.Visible = False
    linkRegister.Visible = False

end if

emailBox.text = session.contents.item("Email")
firstBox.text = session.contents.item("FirstName")
lastBox.text = session.contents.item("LastName")
promoBox.text = session.contents.item("Promo")


End Sub

Sub Button1_Click(sender As Object, e As EventArgs)

    changeDetails(emailBox.text, firstBox.text, lastBox.text, promoBox.text)

End Sub

Function changeDetails(ByVal email As String, ByVal firstname As String, ByVal lastname As String, ByVal promo As String) As Integer
    Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\Documents an"& _
        "d Settings\Paul Jarratt\My Documents\ticketoffice\datab\ticketoffice.mdb"
    Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)

    Dim queryString As String = "UPDATE [users] SET [email]=@email, [firstname]=@firstname, [lastname]=@lastname, "& _
        "[promo]=@promo WHERE ([users].[user_id] = " + session.contents.item("ID") + ")"
    Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
    dbCommand.CommandText = queryString
    dbCommand.Connection = dbConnection

    Dim dbParam_email As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
    dbParam_email.ParameterName = "@email"
    dbParam_email.Value = email
    dbParam_email.DbType = System.Data.DbType.[String]
    dbCommand.Parameters.Add(dbParam_email)

    Dim dbParam_firstname As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
    dbParam_firstname.ParameterName = "@firstname"
    dbParam_firstname.Value = firstname
    dbParam_firstname.DbType = System.Data.DbType.[String]
    dbCommand.Parameters.Add(dbParam_firstname)

    Dim dbParam_lastname As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
    dbParam_lastname.ParameterName = "@lastname"
    dbParam_lastname.Value = lastname
    dbParam_lastname.DbType = System.Data.DbType.[String]
    dbCommand.Parameters.Add(dbParam_lastname)

    Dim dbParam_promo As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
    dbParam_promo.ParameterName = "@promo"
    dbParam_promo.Value = promo
    dbParam_promo.DbType = System.Data.DbType.[String]
    dbCommand.Parameters.Add(dbParam_promo)

    Dim rowsAffected As Integer = 0
    dbConnection.Open
    Try
        rowsAffected = dbCommand.ExecuteNonQuery
    Finally
        dbConnection.Close
    End Try

    labelTest.text = rowsAffected.ToString()

    if rowsAffected = 1 then

    labelSuccess.text = "* Your details have been updated and saved"

    else

    labelError.text = "* Your details could not be updated"

    end if

End Function

Any help would be greatly appreciated.

A: 

Does your page have a RequiresTransaction property? If so, check that there are no exceptions thrown elsewhere during the request which might cause the transaction to roll back - leaving the data unchanged.

Paul Alexander
There is no RequiresTransaction in my page. However I have just done a test. Currently the page would fill the textboxes with the session values for the user and the user would have to overwrite and submit. I have stopped the textboxes from using the session data and left them blank and then filled in the textboxes and the database updates correctly. So it appears the problem is that it is updating using the original textbox values when the query is run even if I overwrite them? Therefore it is just updating the table with the previous data resulting in no change?
Ronnie
A: 

Are you sure that you're updating the database you think you're updating? I don't like the look of that connection string at all (and, frankly, I doubt Paul Jarratt would be too happy to see it posted here).

Larry Lustig
I am connecting to the correct database and I am Paul Jarratt.
Ronnie