0

What would be the problem with my code? I am having an error AddWithValue is not member of MySql.Data.MySqlClient.MySqlParameter

This is my code:

    Dim bn As String = ""
    Dim bottles As Integer = 0

    Dim SQLStatement As String = "UPDATE patient SET number_of_bottles = @bottles WHERE bednumber = @bednumber"

    Using cnn As New MySqlConnection("Server=localhost; database=patientinfo;user id=root;password=")
        Dim cmdn As New MySqlCommand(SQLStatement, cnn)
        cmdn.Parameters.AddWithValue("bottles", bottles)
        cmdn.Parameters.AddWithValue("bednumber", bn)
        cnn.Open()
        cmd.ExecuteNonQuery()

    End Using

I am new in VB.net as well as MySql. Any help would be appreciated. Thanks ! :)

5
  • 2
    What ADO.Net MySql Provider do you use? I used the connector for ado.net at mysql.com/products/connector and your code checked out. Perhaps you could show us a screenshot or give some more details, pls! Commented Aug 13, 2012 at 19:02
  • 1
    One little error occured in your code. You wrote cmd.ExecuteNonQuery() instead of cmdn.ExecuteNonQuery() Commented Aug 13, 2012 at 19:08
  • What version of the MySql library are you using? It could be that you are using an old version; @pilgerstorfer-franz points to the download location for the ADO.Net client download. I'm using 6.5.4 (latest version) and Command.Parameters.AddWithValue is a supported method... Commented Aug 13, 2012 at 19:31
  • I just re-checked the tutorial and @LarsTech is perfectly right. You have to use @ with any parameters. So change your code to cmdn.Parameters.AddWithValue("@bottles", bottles). Did you try changing your bn var to an existing value? Commented Aug 13, 2012 at 19:39
  • Just a note, I've always had to use ? instead of @ with the MySql provider. It used to be very particular about that unless it's changed which is possible. Commented Aug 13, 2012 at 19:49

2 Answers 2

1

You have to use the name you used in the parameter (the @ sign is missing):

cmdn.Parameters.AddWithValue("@bottles", bottles)
cmdn.Parameters.AddWithValue("@bednumber", bn)
Sign up to request clarification or add additional context in comments.

4 Comments

it doesnt solved my problem but i know it should not be like that reference: stackoverflow.com/questions/8907146/…
@CatherineTakishima bn looks like it should be a number, not a string. What is bednumber in your table, string or int? See error in mysql syntax in vb.net
@CatherineTakishima Also, if the @ is the issue (I don't use MySQL very often), try using a differnt name than bednumber since your table has that name already.
I used Parameter.Add(parametername, value) I hope it is similar with Parameter.AddWithValue
1

If you're using a provider that doesn't support "AddWithValue" (and some don't), you can make your own with extension methods. Here is an example of an "AddWithValue" extension method with a few overloads (that extends IDbCommand).

    ''' <summary>
    ''' Adds a parameter into the IDbCommand and sets it's name, value and type.
    ''' </summary>
    ''' <param name="cmd"></param>
    ''' <param name="paramName"></param>
    ''' <param name="paramValue"></param>
    ''' <param name="dbType"></param>
    ''' <remarks></remarks>
    <Extension()> _
    Public Sub AddWithValue(ByVal cmd As IDbCommand, ByVal paramName As String, ByVal paramValue As Object, ByVal dbType As DbType)
        Dim param As DbParameter = cmd.CreateParameter
        param.ParameterName = paramName
        param.DbType = dbType
        param.Value = paramValue
        cmd.Parameters.Add(param)
    End Sub

    ''' <summary>
    ''' Adds a parameter into the IDbCommand and name and value.
    ''' </summary>
    ''' <param name="cmd"></param>
    ''' <param name="paramName"></param>
    ''' <param name="paramValue"></param>
    ''' <remarks></remarks>
    <Extension()> _
    Public Sub AddWithValue(ByVal cmd As IDbCommand, ByVal paramName As String, ByVal paramValue As Object)
        Dim param As DbParameter = cmd.CreateParameter
        param.ParameterName = paramName
        param.Value = paramValue
        cmd.Parameters.Add(param)
    End Sub

    ''' <summary>
    ''' Adds a parameter into the IDbCommand and sets it's name, value and type.
    ''' </summary>
    ''' <param name="cmd"></param>
    ''' <param name="paramName"></param>
    ''' <param name="paramValue"></param>
    ''' <param name="dbType"></param>
    ''' <remarks></remarks>
    <Extension()> _
    Public Sub AddWithValue(ByVal cmd As IDbCommand, ByVal paramName As String, ByVal paramValue As Object, ByVal dbType As DbType, ByVal size As Integer)
        Dim param As DbParameter = cmd.CreateParameter
        param.ParameterName = paramName
        param.DbType = dbType
        param.Value = paramValue
        param.Size = size
        cmd.Parameters.Add(param)
    End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.