0

I have a database containing various users, and I need to change the value of their gender.

When they register the only data added to the table Users is the Username and the Password leaving the gender blank.
I have a button to add other information just like the gender, I tried to use the update query, the program runs with no errors but when I open the database to see the result nothing is added/changed.

Table example:

id Username Password Gender
1 usrExmpl pwdExmpl

What I'm trying to do is to add a value for the gender, either "Male" or "Female" to the respective user, this is what I tried:

Dim gender As String = txtGender.txt
Dim queryGender As String = "UPDATE Users SET Gender=@Gender WHERE Username=@Username"

Using cmdGender= New OleDbCommand(queryGender, conn)
    cmdGender.Parameters.Add("Username", OleDbType.VarChar).Value = My.Settings.currentUser
    cmdGender.Parameters.Add("Gender", OleDbType.VarChar).Value = gender

    If conn.State = ConnectionState.Closed Then conn.Open()
    cmdGender.ExecuteNonQuery()
    cmdGender.Dispose()
End Using

This gives me no errors and the table doesn't get updated either.

NB: My.Settings.currentUser is a string that contains the username of the current logged-in user and is correct and functioning like it should.

Edit: Thanks to @GSerg i fixed it, the problem was that i wasn't adding the parameters in the same order as the one in the query, i swapped them following the correct order.

Daniele S.
  • 40
  • 5
  • 1
    `OleDbCommand` distinguishes the parameters by the order they are specified in the query, not by the name. Swap your two `Parameters.Add` lines. – GSerg Dec 29 '20 at 19:13
  • @GSerg The answer you linked didn't really answer my question but your suggestion of swapping the parameters order worked fine, thanks. – Daniele S. Dec 29 '20 at 20:11
  • 1
    I linked to the [question](https://stackoverflow.com/q/2407685/11683), which describes exactly the same underlying problem, and while the [accepted answer](https://stackoverflow.com/a/2407712/11683) there is not very descriptive, the [next answer](https://stackoverflow.com/a/21925885/11683) is. – GSerg Dec 29 '20 at 20:12

0 Answers0