i have this query to make where I have to use two DateTimePicker. The DateTimePicker works but whenever I click the Search button to get the information I want with the ranged dates, there's this error that says, "the commandtext has not been properly initialized. Please help. thank you!
this is my code:
Imports MySql.Data.MySqlClient Imports System.Data.Sql Imports System Imports System.Data Imports System.Windows.Forms.DataGridView Public Class Form5 Dim con As New MySqlCoection Dim result As Integer 'MySqlCommand It represents a SQL statement to execute against a MySQL Database Dim cmd As New MySqlCommand 'Represents a set of data commands and a database coection that 'are used to fill a dataset and update a MySQL database. This class caot be inherited. Dim da As New MySqlDataAdapter Dim MySqlCo As MySqlCoection Dim dd As DataTable
Private query As String
Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DateTimePicker1.Format = DateTimePickerFormat.Custom
DateTimePicker1.CustomFormat = "dd/MM/yyyy"
DateTimePicker2.Format = DateTimePickerFormat.Custom
DateTimePicker2.CustomFormat = "dd/MM/yyyy"
query = "select * from transaction"
da = New MySqlDataAdapter(query, con)
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs)
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow
row = Me.DataGridView1.Rows(e.RowIndex)
row.Cells("PRODUCT").Value.ToString()
row.Cells("QUANTITY").Value.ToString()
row.Cells("PRICE").Value.ToString()
row.Cells("DISCOUNT").Value.ToString()
row.Cells("DATE").Value.ToString()
row.Cells("TOTALPRICE").Value.ToString()
row.Cells("CASHIER").Value.ToString()
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
con.CoectionString = ("server=localhost;user id=root; password=amacc;database=sales")
Try
con.Open()
With cmd
.Coection = con
'in this query it does simply selecting or getting all the user found in the database.
.CommandText = "Select * from transactions"
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Close()
da.Dispose()
filltable(DataGridView1)
End Sub
Private Sub filltable(dataGridView As DataGridView)
'declare a variable as new datatable
Dim publictable As New DataTable
Try
'Gets or sets an SQL statement or stored procedure used to select records in the database.
da.SelectCommand = cmd
da.Fill(publictable)
DataGridView1.DataSource = publictable
DataGridView1.Columns(0).Visible = False
da.Dispose()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub EXITToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles EXITToolStripMenuItem.Click
If MsgBox("Are you sure you want to exit?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
Close()
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim ss As String
ss = DateTimePicker1.Text
Dim sd As String
sd = DateTimePicker2.Text
con.CoectionString = ("server=localhost;user id=root; password=amacc;database=sales")
Try
con.Open()
With cmd
.Coection = con
query = "Select * from transactions where Date between '" & ss & "'and '" & sd & "'"
da = New MySqlDataAdapter(query, con)
dd = New DataTable
da.Fill(dd)
DataGridView1.DataSource = dd
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Close()
da.Dispose()
filltable(DataGridView1)
End Sub
End Class
