Results 1 to 4 of 4

Thread: Parameters.AddWithValue equivalent for MySqlClient

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2010
    Location
    San Marcos, TX
    Posts
    177

    Parameters.AddWithValue equivalent for MySqlClient

    What is the equivalent of Parameters.AddWithValue in the MySqlClient?

    I've tried Parameters.Add and it's not working, but AddWithValue gives me an error.

    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Parameters.AddWithValue equivalent for MySqlClient

    Unless the error is a NotSupportedException then it's simply a matter that you're doing it wrong. If you show us what you're doing and tell us what the error message is then we can probably help. In general, AddWithValue requires the name of a parameter and the value for that parameter, where the value MUST be the correct data type, e.g. you cannot pass a String if the parameter expects a number.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2010
    Location
    San Marcos, TX
    Posts
    177

    Re: Parameters.AddWithValue equivalent for MySqlClient

    Here's the code I'm attempting to run.

    Code:
    Public Shared Function GetClientsByFirstName(ByVal FirstName As String) As List(Of clients)
            Dim connection As MySqlConnection = ConnectionFactory.GetConnection
            connection.Open()
    
            'make a command object
            Dim selectCommand As New MySqlCommand
            selectCommand.Connection = connection
    
            'count data query
            selectCommand.CommandText = "SELECT " & _
                "COUNT(clients.client_id) AS count " & _
                "FROM clients " & _
                "WHERE clients.first_name = @firstname"
            selectCommand.Parameters.Add("@firstname", FirstName)
            Dim reader As MySqlDataReader = selectCommand.ExecuteReader()
            Dim count As Integer = Nothing
            Do While reader.Read
                count = reader("count").ToString
            Loop
            Dim i As Integer = 0
            'setup progress bar
            frmMain2.StatusBar.Maximum = count
            frmMain2.StatusBar.Minimum = 0
            frmMain2.StatusBar.Value = 0
            reader.Close()
    End Function
    I'm not exactly getting an error, just no result. If I put in a breakpoint at "Dim i As Integer = 0" and check the value of count it is zero. If I run the query on the table with say the firstname of "mark" I get a result of 28, so I know the query technically works, but for some reason not in vb.net.

    Thanks!

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Parameters.AddWithValue equivalent for MySqlClient

    Count is a reversed keyword. You have to either use a different word or put it in brackets
    Code:
    selectCommand.CommandText = "SELECT " & _
                "COUNT(clients.client_id) AS [count] " & _
                "FROM clients " & _
                "WHERE clients.first_name = @firstname"
    Also, if you just need to get 1 value (the count) then why not calling ExecuteScalar method?
    Last edited by stanav; Mar 11th, 2011 at 04:48 PM.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width