Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

[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.
Phil Weber at 2007-11-11 20:48:05 >
# 2 Re: [2005] MySQL Query in VB.net find value *code included*
You need to change query to command (what you declared your sqlcommand to be named)

command.Parameters.AddWithValue("@UserName", Me.TextBox1.Text)
conn.Open()
If CInt(comand.ExecuteScalar()) > 0 Then
joewmaki at 2007-11-11 20:49:11 >
# 3 Re: [2005] MySQL Query in VB.net find value *code included*
Error 1 'AddWithValue' is not a member of 'MySql.Data.MySqlClient.MySqlParameterCollection'. C:\Documents and Settings\Eric\My Documents\Visual Studio 2005\Projects\VCLICS\VCLICS\frmRegister.vb 103 9 VCLICS

i changed it per joewmaki's specs and this is the error i get... any ideas?
vchatlive at 2007-11-11 20:50:10 >
# 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.
joewmaki at 2007-11-11 20:51:05 >
# 5 Re: [2005] MySQL Query in VB.net find value *code included*
ok ive gone ahead and taken a jump on my own here and was able to add the vclics database by clicking add new datasource, now i have a "dataset" i beleive, how can i use the button to check that dataset so i can find out if a username is available or not. Thanks! the field in the database is "username" that i want to check.
vchatlive at 2007-11-11 20:52:06 >
# 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.
joewmaki at 2007-11-11 20:53:11 >
# 7 Re: [2005] MySQL Query in VB.net find value *code included*
says ds is not declared, this is so confusing... how would i declare ds so it knows that the dataset is vclicsDateSet

Thanks
vchatlive at 2007-11-11 20:54:15 >
# 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.
joewmaki at 2007-11-11 20:55:08 >
# 9 Re: [2005] MySQL Query in VB.net find value *code included*
dim ds as new vclicsdataset? im tryin to learn, lol
vchatlive at 2007-11-11 20:56:12 >