why sql command cannot be converted to mysql command in vb.net - mysql

I am currently working on connecting my data grid view to my database, but the problem is I can't connect with this error : MySql.Data.MySqlClient.MySqlCommand' cannot be converted to 'System.Data.SqlClient.SqlCommand
here's my code:
Private Sub populateGird()
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim bs As New BindingSource
Try
sqlC = New MySqlCommand("SELECT c_id, CONCAT(c_firstname, c_lastname), rank_name, c_bir_status, rank_basic_pay, rank_positional_allowance," &
"rank_meal_allowance, ca_worth FROM tbl_crew, tbl_cash_advance, tbl_rank", conn)
da.SelectCommand = sqlC '--> error is that sqlC
da.Fill(ds)
bs.DataSource = ds
PayrollGrid.DataSource = bs
da.Update(ds)
conn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
this is my connection code in a module:
Imports MySql.Data.MySqlClient
Module Module1
Public conn As MySqlConnection
Public Sub connect()
conn = New MySqlConnection
conn.ConnectionString = "server=localhost;user=root;database=fat2x_payroll;pwd=;Convert Zero Datetime=True"
Try
conn.Open()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Module

The error message is quite clear - you are trying to assign from an incompatible type! SelectCommand is of type System.Data.SqlClient.SqlCommand because you have defined da as a variable of type System.Data.SqlClient.SqlDataAdapter. On the other hand, you are creating a variable of type MySql.Data.MySqlClient.MySqlCommand, which is a different type and which cannot be converted to SqlCommand when you try to do the assignment.
The way to fix this is to change your code to use the corresponding classes from MySql.Data.MySqlClient, like so:
Dim da As New MySqlDataAdapter

Change your declaration of the SqlDataAdapter to the appropriate type for MySQL (I'm guessing MySqlDataAdapter?)
Dim da As New SqlDataAdapter

Related

chart connection with MySql

I am connecting a chart object in mysql database but getting this error:
object reference not set to an instance of an object
This is the code I used:
Imports MySql.Data.MySqlClient
Public Class Form1
Dim con As New MySqlConnection
Dim com As MySqlCommand
Dim dt As New DataTable
Private Sub btnlaod_Click(sender As Object, e As EventArgs) Handles btnlaod.Click
con = New MySqlConnection
con.ConnectionString = "server=localhost;userid=root;password=;database=noh_mis"
Dim Reader As MySqlDataReader
Try
con.Open()
Dim query As String
query = "Select * From database.students_profile"
Reader = com.ExecuteReader
While Reader.Read
Chart1.Series("Male").Points.AddXY(Reader.GetString("Gender"), Reader.GetString("GradeLevel"))
End While
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
con.Dispose()
End Try
End Sub
End Class
You do not make a command before using com.ExecuteReader. You make a string of sql, but never connect that with your database.
Try adding the below line after building query but before Reader = com.ExecuteReader:
com = New MySqlCommand(query, con)

VB.NET Display data from MySql to Datagrid

I already read all the questions and answers from previous post
but not fit with my problem
Problem :
I want to display data from db(mysql) to datagrid
but the problem is MySqlDataReader doesnt recognize
SelectCommand
Fill
Update
as the member of MySqlDataReader
I follow the exact same way of code writing from the tutorial
Why this problem occur ??
Below I attached my code.
I hope someone could help me.
Imports MySql.Data.MySqlClient
Public Class AdminAddItem
Dim MysqlConnection As MySqlConnection
Dim FoodCommand As MySqlCommand
Dim DataFoodRead As MySqlDataReader
Dim dbDataSetFood As New DataTable
Private Sub btnLoadFood_Click(sender As Object, e As EventArgs) Handles btnLoadFood.Click
'declare new connection
MysqlConnection = New MySqlConnection
'prepare connection string
MysqlConnection.ConnectionString =
"server = localhost; userid = root; password = 1234 ; database = kedaikopimamba_db"
Dim bSource As New BindingSource
Try
MysqlConnection.Open()
Dim query As String
query = "select * from kedaikopimamba_db.foodtable"
FoodCommand.Connection = MysqlConnection
FoodCommand.CommandText = query
DataFoodRead.SelectCommand = FoodCommand
DataFoodRead.Fill(dbDataSetFood)
bSource.DataSource = dbDataSetFood
dgDisplayItem.DataSource = bSource
DataFoodRead.Update(dbDataSetFood)
MysqlConnection.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MysqlConnection.Dispose()
End Try
End Sub
End Class
What part to your code is the error? Please let us know to easily identify the problem.

How to add a column to a mysql database using visual basic

My Goal: Add a column to mysql database using visual basic!
I have tried multiple ways, but here is the code I'm currently using:
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString = "Server=xxx.xx.xxx.xxx;Port=3306;Database=test;Uid=tester;Pwd=***"
Try
MysqlConn.Open()
Dim Query As String
Query = "ALTER TABLE user ADD test INT NOT NULL"
COMMAND = New MySqlCommand(Query, MysqlConn)
MysqlConn.Close()
Catch ex As Exception
End Try
End Sub
I think it's a problem with the query! Thanks for your help in advance!
This is different from ms sql, it's visual basic.
My visual studio was actually not using my most up to date code and now everything is working LOL. Here is my revised code:
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString = "Server=**.**.***.***;Port=3306;Database=test;Uid=tester;Pwd=***"
Dim READER As MySqlDataReader
Try
MysqlConn.Open()
Dim Query As String
Query = "alter table test.user add test int not null"
COMMAND = New MySqlCommand(Query, MysqlConn)
READER = COMMAND.ExecuteReader
MysqlConn.Close()
Catch ex As Exception
End Try

MySqlDataAdapter SelectCommand Statement

I have the following functional code:
Call ConnSettings()
Dim objDs As New DataSet
Dim Query As String
Query = "the query"
Cmd = New MySqlCommand(Query, MysqlConn)
Dim dAdapter As New MySqlDataAdapter
dAdapter.SelectCommand = Cmd
''Dim dAdapter As New MySqlDataAdapter(Query, MysqlConn)
Try
MysqlConn.Open()
dAdapter.Fill(objDs)
MysqlConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
I have noticed that the code is equally functional if I also use
Dim dAdapter As New MySqlDataAdapter(Query, MysqlConn)
instead of:
Cmd = New MySqlCommand(Query, MysqlConn)
Dim dAdapter As New MySqlDataAdapter
dAdapter.SelectCommand = Cmd
Obviously, I would like to use a single line instead of three. However, I am fairly new to VB and would like to know if there are any issues with doing that.
Let's try to improve your code....
First ConnSettings doesn't initialize a global connection variable but a local one and returns it
Public Function ConnSettings() As MySqlConnection
Dim conn As MySqlConnection
conn = new MySqlConnection(yourConnectionStringHere)
conn.Open()
return conn
End Function
Now the code that needs a MySqlConnection could call this ConnSettings and put the return value in a Using Statement
objDS = new DataSet()
Try
Using conn = ConnSettings()
Using dAdapter = New MySqlDataAdapter(theQuery, conn)
dAdapter.Fill(objDs)
End Using
End Using
Catch(ex as Exception
MessageBox.Show(ex.Message)
End Try
This code puts the connection returned in a Using Statement. When the code flows out of the Using Statement the connection is closed and disposed (same for the MySqlDataAdapter) As you can see there is no need for the Finally clause and the Try/Catch block is present just because you want to give an error message to your user (while this is a common practice there is no really sense to put your user in the unconfortable position to try to understand these technically dense messages, better use a log file and advise your user to send the log to you)
In this context also the code inside the ConnSettings is a bit useless. What you really need is just the connectionstring and you could write the creation of the MySqlConnection directly in the calling code.

key word not supported 'data source'

im having problem in my connection. the error said that key word not supported in the data source and i dont what to do next. thanks in advance!
Imports MySql.Data
Imports MySql.Data.MySqlClient
Public Class frmMasancayClinicSignupvb
Dim connection As New MySqlConnection
Dim myReader As MySqlDataReader
Dim Adapter As MySqlDataAdapter
Dim myCommand As MySqlCommand
Dim Dset As New DataSet
Dim table As New DataTable
Dim MyQuery As String
Dim i As Integer
Private Sub frmMasancayClinicSignupvb_Load(sender As Object, e As EventArgs) Handles MyBase.Load
connection = New MySqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Mark\Documents\MASANCAYCLINICLOGIN.mdf;Integrated Security=True;Connect Timeout=30")
Try
If connection.State = ConnectionState.Closed Then
connection.Open()
MsgBox("connected to database")
Else
connection.Close()
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
connection.Close()
End Sub
End Class
It appears that you're passing in the arguments for a SqlConnectionStringBuilder - that's for Microsoft's SqlServer database, not MySql. (see
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.integratedsecurity(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2 ).
MySqlConnection takes "Data Source" but doesn't take those other parameters. See :
http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqlconnection.html#connector-net-examples-mysqlconnection-defctor