I tried to search stack overflow and I found a post but the solution did not work for me.
I am using a MySql Database which is connected to Visual Studio 2017 Professional (Community Edition) software with the project being written in VB.net. I had to install the following extensions: - MySQL for Visual Studio 1.2.7 and MySQL ConnectorNet 8.0.11. One should ensure that the DataSource is MySQL Database in order to connect to the MySQL database. I have managed to load/view the data, from the MySql database, into the DataGridView. The source code is the following: -
Imports MySql.Data.MySqlClient
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports MySql.Data
Imports System.Windows.Forms
Imports System.Drawing
Imports System
Public Class Form1
Dim connection As New MySqlConnection("server=localhost; Port=3306; username=root; Password=mypassword")
Dim MysqlConn As MySqlConnection
Dim COMMAND As MySqlCommand
Private connStr As String
Private Sub SubmitButton_Click(sender As Object, e As EventArgs) Handles
SubmitButton.Click
Dim dtEmployee As New DataTable()
Dim command As New MySqlCommand("SELECT * FROM Employees WHERE EmployeeDate BETWEEN @d1 And @d2", connection)
command.Parameters.Add("@d1", MySqlDbType.Date).Value = DateTimePicker1.Value
command.Parameters.Add("@d2", MySqlDbType.Date).Value = DateTimePicker2.Value
Dim adapter As New MySqlDataAdapter(command)
adapter.Fill(dtEmployee)
DataGridView.DataSource = dtEmployee
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView.DataSource = GetEmployeeList()
End Sub
Private Function GetEmployeeList() As DataTable
Dim dtEmployee As New DataTable
Dim connString As String = ConfigurationManager.ConnectionStrings("dbx").ConnectionString
Using conn As New MySqlConnection(connString)
Using cmd As New MySqlCommand("SELECT * FROM Employees", conn)
conn.Open()
Dim reader As MySqlDataReader = cmd.ExecuteReader()
dtEmployee.Load(reader)
End Using
End Using
Return dtEmployee
End Function
End Class
The code in the App.config file is: -
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<connectionStrings>
<add name="dbx" connectionString
="server=localhost;port=3306;database=mydatabase;user
id=root;password=mypassword" providerName="MySql.Data.MySqlClient"/>
I am now experiencing a problem as I am unable to filter the data between a specific data range in the Datagridview. When I set my date range between two dates and I click the submit button, a 'No database selected' error message is thrown at the line: -
adapter.Fill(dtLab)
and the error message: -
Exception Thrown
MySql.Data.MySqlClient.MySqlException: 'No database selected'
MySql.Data.MySqlClient.MySqlException
HResult=0x80004005
Message=No database selected
Source=MySql.Data
StackTrace:
at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64&
insertedId)
at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32&
affectedRows, Int64& insertedId)
at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
Thank you in advance for any assistance
try with this one
Dim connection As New MySqlConnection("server=localhost; Port=3306; database= mydatabase; username=root; Password=mypassword")
User contributions licensed under CC BY-SA 3.0