Joris_H
Joris_H

Reputation: 47

UPDATE statement in asp.net doesn't do anything

I am making a settings page on my website where the user can change his email adress, name, etc. The problem is the update statement, it doesn't work, because nothing has changed in the database, but it doesn't give me an error, so I don't know what's up. When I check with breakpoints it shows that the parameters are getting the right values, and when i search on the internet I can't find anyone with the same problem.

I will paste the code of the update below:

Dim CmdUpdate As New OleDbCommand
        Dim Sqlstatement As String = "UPDATE tblUsers SET firstname = @firstname, lastname = @lastname, userPassword = @userPassword, email = @email, avatar = @avatar WHERE userID = @userID;"
        CmdUpdate.Connection = dbConn.cn
        CmdUpdate.CommandText = Sqlstatement

        CmdUpdate.Parameters.AddWithValue("firstname", txtFirstName.Text)
        CmdUpdate.Parameters.AddWithValue("lastname", txtLastName.Text)
        CmdUpdate.Parameters.AddWithValue("userID", Session("userID"))

        If txtPassword.Text = "" Then
            CmdUpdate.Parameters.AddWithValue("userPassword", Session("hashedpass"))
        Else
            CmdUpdate.Parameters.AddWithValue("userPassword", hash(txtPassword.Text))
        End If

        CmdUpdate.Parameters.AddWithValue("email", txtEmail.Text)
        CmdUpdate.Parameters.AddWithValue("avatar", strAvatar)

        dbConn.cn.Close()
        dbConn.cn.Open()
        CmdUpdate.ExecuteNonQuery()
        dbConn.cn.Close()

Upvotes: 0

Views: 501

Answers (4)

Jim Dagg
Jim Dagg

Reputation: 2032

Is it possible that the userid column just doesn't match, so your WHERE clause matches zero records and nothing gets updated? That could be caused by a number of things -- whitespace, character encoding, and so forth.

Upvotes: 1

Dhaval Marthak
Dhaval Marthak

Reputation: 17366

Use

CmdUpdate.Parameters.AddWithValue("@firstname", txtFirstName.Text)

You were missing to add '@' in AddWithValue()

See Tutorial here

Upvotes: 1

Fabian Bigler
Fabian Bigler

Reputation: 10895

You missed the '@'s in the sql-parameters:

 CmdUpdate.Parameters.AddWithValue("@firstname", txtFirstName.Text)

etc.

Upvotes: 1

Laurent S.
Laurent S.

Reputation: 6946

You just have to include the "@" in your parameter add statements :

CmdUpdate.Parameters.AddWithValue("@firstname", txtFirstName.Text)

...and so on...

Upvotes: 1

Related Questions