Question Error, doesn't update the string

ArTDsL

New member
Joined
Nov 17, 2014
Messages
2
Programming Experience
3-5
Sorry for my bad English because i'm Brazilian, and Hi! I'm New here and i need your guys Help!

Here is the code:

VB.NET:
Function ChangeUser() As String       
 objconn.ConnectionString = ("Server=xxx.xxx.xxx.xx;port=3306;Database=xxx;userid=xx;password=xxx")
      
        Try
            objconn.Open()
            strsql = "update tab_usuario set usuario= '" & txt_NUsuario.Text & "' where usuario = '" & My.Settings.usuario_logado.ToString & "'"
            objcmd = New MySqlCommand(strsql, objconn)
            With objcmd
            End With


            objcmd.ExecuteNonQuery()
            objconn.Close()
            MsgBox("UserName Changed!", MsgBoxStyle.OkOnly, Title:="Ambu Video Server")
        Catch ex As Exception
            MsgBox(ex.Message)
            'MsgBox("ERRO008" + vbCrLf + vbCrLf + "Fail", MsgBoxStyle.OkOnly, Title:="Ambu Video Server")
        Finally
            objconn.Dispose()
        End Try
    End Function

txt_NUsuario = New User
My.Settings.usuario_logado = User in in the system!

MySQL error: Says the line strsql = "update tab_usuario set usuario= '" & txt_NUsuario.Text & "' where usuario = '" & My.Settings.usuario_logado.ToString & "'", is wrong and i need check the manual for extra help!

Please Help!
 
It's not saying that that line is wrong. It's saying that the SQL code that that line produces is wrong. Have you actually looked at the value of `strsql` after that line executes?

The first thing you should do is stop using string concatenation to insert values into SQL code. You should always use parameters. That will quite likely fix this problem and help you avoid others. Follow the Blog link in my signature below and check out my post on Parameters In ADO.NET. Fix your code by using parameters and see if the issue still exists. If it does, post back with your new code.
 
Hi jmcilhinney,

Thanks for help i look more inside in the line then i found the error.

Here is a correct line...
VB.NET:
strsql = "update tab_usuarios set usuario= '" & txt_NUsuario.Text & "' where usuario = '" & My.Settings.usuario_logado.ToString & "'"

like you say it's a error on SQL line,

Thanks for your help!

@Closed
 
You're still not using parameters so that's still bad code, even if it manages to work in this specific case. Learn how to do it properly now and save yourself issues in the future.
 
Back
Top