0

In my code, I get a result showing "data saved" but in my database, nothing is updating - what is wrong in this?

Private Sub btn_update_Click(sender As Object, e As EventArgs) Handles btn_update.Click
    Try
        Dim usernameuser As String = Session("user").ToString
        conn.Open()
        Dim STRLINE = "UPDATE CUSTOMER_LIST_TBL SET FIRST_NAME=@FIRST_NAME,LAST_NAME=@LAST_NAME,SIR_NAME=@SIR_NAME Where (PHONE_NO=@PHONE_NO)"
        Dim CMD As New SqlCommand(STRLINE, conn)
        CMD.Parameters.AddWithValue("@FIRST_NAME", SqlDbType.NVarChar).Value = f_name.Text.ToString()
        CMD.Parameters.AddWithValue("@LAST_NAME", SqlDbType.NVarChar).Value = l_name.Text.ToString()
        CMD.Parameters.AddWithValue("@SIR_NAME", SqlDbType.NVarChar).Value = sir_name.Text.ToString()
        CMD.Parameters.AddWithValue("@PHONE_NO", SqlDbType.NVarChar).Value = usernameuser

        CMD.ExecuteNonQuery()

        lblmsg.Text = "data saved"""

    Catch ex As Exception
        lblmsg.Text = Err.Description

    End Try
    conn.Close()
End Sub
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Isn't the second parameter to `AddWithValue` supposed to be the value, instead of the data type? (See https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue(v=vs.110).aspx). You could use `Add` instead (https://msdn.microsoft.com/en-us/library/wbys3e9s(v=vs.110).aspx). – ConnorsFan Jun 05 '16 at 17:53
  • Here is another reference that does something similar to your code: http://stackoverflow.com/questions/9155004/difference-with-parameters-add-and-parameters-addwithvalue. – ConnorsFan Jun 05 '16 at 17:57
  • You should check out [Can we stop using AddWithValue() already?](http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/) and stop using `.AddWithValue()` - it can lead to unexpected and surprising results... – marc_s Jun 05 '16 at 18:07
  • .AddWithValue takes the param name and value and does not take a datatype as argument - https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue(v=vs.110).aspx – Ed Schembor Jun 05 '16 at 20:36
  • This is an educated guess, but does `Session("user")` actually holds the phone number of the user? If no records meets the conditions in the where clause, there will be no update and no error, and the names incompatibility is an indication that perhaps the value you are sending to the `"@PHONE_NO"` is not actually the phone number. – Zohar Peled Jun 06 '16 at 04:50

0 Answers0