Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi!
When i try beetween low date like 2021-10-01 to 2021-10-08 works fine.
But with long date like 2021-01-01 to 2021-10-01 I have fatal error.

This is my code:
VB.NET
Imports CrystalDecisions.CrystalReports.Engine
Imports MySql.Data.MySqlClient
Imports CrystalDecisions.Shared
Imports System.Configuration
Public Class Venta_Pales_por_Fecha_RPT
    Dim da As MySqlDataAdapter
    Dim ds As DataSet
    Dim p(7) As MySqlParameter
    Dim MysqlConn As MySqlConnection
    Dim COMMAND As MySqlCommand
    Dim READER As MySqlDataReader
    Dim con As New MySqlConnection(ConfigurationManager.ConnectionStrings("MYCNXMASKED").ToString)

    Private Sub CrystalReportViewer1_Load(sender As Object, e As EventArgs) Handles CrystalReportViewer1.Load

    End Sub

    Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
        DateTimePicker1.CustomFormat = "yyyy-MM-dd"
    End Sub

    Private Sub DateTimePicker2_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker2.ValueChanged
        DateTimePicker1.CustomFormat = "yyyy-MM-dd"
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


        Me.Cursor = Cursors.WaitCursor
        Dim ds As New DataSet
                    Dim query As String
                    p(0) = New MySqlParameter("@FECHAINICIO", MySqlDbType.String)
                    p(0).Value = DateTimePicker1.Text
                    p(1) = New MySqlParameter("@FECHAFINAL", MySqlDbType.String)
                    p(1).Value = DateTimePicker2.Text
                    p(2) = New MySqlParameter("@NAVE", MySqlDbType.String)
                    p(2).Value = ComboBox1.Text
                    p(3) = New MySqlParameter("@PRODUCTO", MySqlDbType.String)
                    p(3).Value = ComboBox2.Text
                    query = "SELECT
                    Produccion.Producto,
                    Produccion.Producto_tipo,
                    Produccion.Formato,
                    Produccion.Marcado,
                    Produccion.Codigo_referencia,
                    Produccion.Particularidad,
                    count(*) as TOTAL_PALES,
                    SUM(Piezas) AS TOTAL_PIEZAS,SUM(Kilos) AS TOTAL_Kilos,
                    CONCAT(Produccion.Longitud, 'x',Produccion.Anchura) AS TAMANO
                    FROM Lote_Cliente, Produccion
                    WHERE (fecha_preparacion BETWEEN @FECHAINICIO AND @FECHAFINAL) AND Lote_Cliente.numero_lote_cliente = Produccion.Lote_cliente AND Produccion.Nave = @NAVE AND Produccion.Producto = @PRODUCTO
                    GROUP BY Produccion.Producto,Produccion.Producto_tipo,Produccion.Formato,Produccion.Marcado,Produccion.Codigo_referencia,Produccion.Particularidad,TAMANO
                    ORDER BY Produccion.Producto, Produccion.Producto_tipo,Produccion.Formato,TAMANO ASC,Produccion.Codigo_referencia,Produccion.Marcado ASC"


                    Dim dscmd As New MySqlDataAdapter(query, con)
                    dscmd.SelectCommand.Parameters.Add(p(0))
                    dscmd.SelectCommand.Parameters.Add(p(1))
                    dscmd.SelectCommand.Parameters.Add(p(2))
                    dscmd.SelectCommand.Parameters.Add(p(3))
                    dscmd.Fill(ds, "VENTAPALESPORFECHAS")
                    Dim cryds As New Venta_Pales_por_Fecha
                    cryds.SetDataSource(ds.Tables(0))
                    CrystalReportViewer1.ReportSource = cryds
        CrystalReportViewer1.Refresh()
        con.Close()
        con.Close()
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub Venta_Pales_por_Fecha_RPT_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class



Thanks you

What I have tried:

But when i try with mysql client like HeidiSQL it´s long (2mn) but works fine. What i do check ?
Posted
Updated 8-Oct-21 0:46am

1 solution

Simple: don't use text strings to pass Date values: use the Value property of the DateTimePicker, and pass that directly as a DateTime value.

That way, SQL doesn't have to "work out" what format the date is in, and - quite probably - get it wrong. DateTime values are passed as a number of ticks since a defined point in time, so they require no conversion to work with VB or MySql.

In fact, that you are passing them as text implies that you should probably look at your DB design: the chances are that you are storing them as text as well, and that's a really bad idea.
 
Share this answer
 
Comments
Member 12919322 8-Oct-21 7:01am    
Thank you for reply. But how i do that. I´am newbee on this.
Date field is not text is date in mysql
OriginalGriff 8-Oct-21 7:25am    
Are you kidding me? You don't know how to fetch the Value property of a DTP instead of the Text property?
Member 12919322 8-Oct-21 7:31am    
I not kidiing, sorry.
I try the follow :
Dim d1 As DateTime = DateTimePicker1.Value

Dim d2 As DateTime = DateTimePicker2.Value

Me.Cursor = Cursors.WaitCursor
Dim ds As New DataSet
Dim query As String
p(0) = New MySqlParameter("@FECHAINICIO", MySqlDbType.String)
p(0).Value = d1
p(1) = New MySqlParameter("@FECHAFINAL", MySqlDbType.String)
p(1).Value = d2
p(2) = New MySqlParameter("@NAVE", MySqlDbType.String)
p(2).Value = ComboBox1.Text
p(3) = New MySqlParameter("@PRODUCTO", MySqlDbType.String)
p(3).Value = ComboBox2.Text

But doesn´t work
OriginalGriff 8-Oct-21 8:04am    
Why are you still trying to pass them as strings?
Come on, think about what you are doing before you do it - it'll save you a lot of time ...
Member 12919322 8-Oct-21 9:01am    
I try this but same error :
Me.Cursor = Cursors.WaitCursor

Dim table As New DataTable()
Dim command As New MySqlCommand("SELECT
Produccion.Producto,
Produccion.Producto_tipo,
Produccion.Formato,
Produccion.Marcado,
Produccion.Codigo_referencia,
Produccion.Particularidad,
count(*) as TOTAL_PALES,
SUM(Piezas) AS TOTAL_PIEZAS,SUM(Kilos) AS TOTAL_Kilos,
CONCAT(Produccion.Longitud, 'x',Produccion.Anchura) AS TAMANO
FROM Lote_Cliente, Produccion
WHERE (fecha_preparacion BETWEEN @FECHAINICIO AND @FECHAFINAL) AND Lote_Cliente.numero_lote_cliente = Produccion.Lote_cliente AND Produccion.Nave = @NAVE AND Produccion.Producto = @PRODUCTO
GROUP BY Produccion.Producto,Produccion.Producto_tipo,Produccion.Formato,Produccion.Marcado,Produccion.Codigo_referencia,Produccion.Particularidad,TAMANO
ORDER BY Produccion.Producto, Produccion.Producto_tipo,Produccion.Formato,TAMANO ASC,Produccion.Codigo_referencia,Produccion.Marcado ASC", con)

command.Parameters.Add("@FECHAINICIO", MySqlDbType.Date).Value = DateTimePicker1.Value
command.Parameters.Add("@FECHAFINAL", MySqlDbType.Date).Value = DateTimePicker2.Value
command.Parameters.Add("@NAVE", MySqlDbType.Text).Value = ComboBox1.Text
command.Parameters.Add("@PRODUCTO", MySqlDbType.Text).Value = ComboBox2.Text
Dim adapter As New MySqlDataAdapter(command)
adapter.Fill(table)

DataGridView1.DataSource = table

con.Close()
con.Close()
Me.Cursor = Cursors.Default

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900