MySQL Connector .NET, AJAX with username checking

Posted on 2007-07-31
Last Modified: 2010-04-23

I'm trying to use an AJAX function to see if a username already exists in a MySQL database, the function works in VS2005 if I use the debug utility to change the value of 'count' to 1, here is the code:

Private Function UserNameExists(ByVal userName As String) As Boolean
        Dim con As New MySqlConnection(ConnectionStrings("testConn").ConnectionString)
        Dim cmd As New MySqlCommand("Select Count(*) From dk_users Where username = @username", con)
        cmd.Parameters.AddWithValue("@username", userName)

        Dim result As Boolean = False
        Using con
            Dim count As Integer = CType(cmd.ExecuteScalar(), Integer)

            If count > 0 Then
                result = True
            End If
        End Using
        Return result
    End Function

But as it stands count is always 0, I've run the SQL through MySQL Query Browser and appended a username to it's parameters manually and count is returning 1, so I don't know why the count is not correct in the function.

Please let me know if you need any more of the code?
Question by:G0ggy
    LVL 17

    Accepted Solution

    >        Dim cmd As New MySqlCommand("Select Count(*) From dk_users Where username = @username", con)

    Have you tried not using the parameter, but constructing the SQL by concatenation:
            Dim cmd As New MySqlCommand("Select Count(*) From dk_users Where username = '" + userName +"' ", con)

    LVL 11

    Author Comment

    Talk about going backwards! That worked, thanks.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
    Creating and Managing Databases with phpMyAdmin in cPanel.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now