[2005] MySQL Query in VB.net find value *code included*
i have some code here, that i did, and i cant get query to be declared... if it needs to be modified, if someone can please help me modify this code it would be great... i want to search the database and see if a "value" is listed in a certain column. Basically i want to find out if a username is available or not. Thanks
Slightly modified the code, heres what i have now, need someone to check it for me... Thanks
Private Sub cmdCheckUN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCheckUN.Click
conn = New MySqlConnection()
conn.ConnectionString = "server=x.x.x.x; user id=root; password=xxx; database=xxx"
Dim command As New MySqlCommand("SELECT COUNT(*) FROM User WHERE UserName = @UserName", conn)
query.Parameters.AddWithValue("@UserName", Me.TextBox1.Text)
conn.Open()
If CInt(query.ExecuteScalar()) > 0 Then
MessageBox.Show("User name in use.")
Else
command.CommandText = "INSERT INTO `username` '" & Me.TextBox1.Text & "' VALUES (@UserName)"
command.ExecuteNonQuery()
End If
' Try
' conn.Open()
' MessageBox.Show("Connection Opened Successfully")''
'conn.Close()
'Catch myerror As MySqlException
' MessageBox.Show("Error Connecting to Database: " & myerror.Message)
' Finally
' conn.Dispose()
' End Try
End Sub
Hope someone can help
[1468 byte] By [
vchatlive] at [2007-11-11 10:21:51]

# 1 Re: [2005] MySQL Query in VB.net find value *code included*
Your INSERT statement should be in the format:
INSERT INTO TableName (ColumnName) VALUES (Value)
There should not be quotes around the table name (e.g., `username`), and unless TextBox1 contains the column name, you don't want it in the middle of the statement.
# 4 Re: [2005] MySQL Query in VB.net find value *code included*
Sorry, I don't use MySql, so I'm not sure what properties are supported. I used your existing code example.
The point was, you were referring to query which was undeclared and should have been referring to command, your declared MySqlCommand.
When typing in the code, IntelliSense should supply the correct parameters.add syntax.
# 6 Re: [2005] MySQL Query in VB.net find value *code included*
The syntax I'm using works in SQL Server, yours should be similar.
Assuming your dataset (ds) is already filled with all users and the table named "User"
Dim strUSER As String = Me.TextBox1.Text
Dim dv As New DataView(ds.Tables("User"), "USERNAME = '" & strUSER & "'", "", DataViewRowState.CurrentRows)
If dv.Count > 0 Then
MessageBox.Show(strUSER & " already Used!", "Pick A New Name", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Else
'insert new record here
End If
You use the dataview to filter your dataset.
The: "USERNAME = '" & strUSER & "'" is a row filter for your dataset and should return a collection of datarows matching the filter.
# 8 Re: [2005] MySQL Query in VB.net find value *code included*
You can just replace ds with vclicsDateSet.
I'm sorry, but I can't write your application for you. I assume you know how to declare variables and use them.
Don't be afraid to use the help files, they are very informative.