Bug #83552 SQL Query using DATE_FORMAT returns a data reader error even though a data reade
Submitted: 26 Oct 2016 15:44 Modified: 26 Oct 2016 16:42
Reporter: Nick Franklin Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version:5.1.43sp1 OS:Any
Assigned to: CPU Architecture:Any

[26 Oct 2016 15:44] Nick Franklin
Description:
This code in ASP.NET causes a 'There is already an open DataReader associated with this Connection which must be closed first'. The error occurs below at the line da.Fill(ds, "messages").

sql = "SELECT id,message_subject, CASE WHEN length(message) <= 39 THEN message ELSE CONCAT(SUBSTRING_INDEX(LEFT(message,40), ' ' , 12),'.....') end as message,target,status,DATE_FORMAT(sent_datetime, '%d/%m/%Y %H:%m:%s') as date_sent FROM msg_master ORDER BY date_sent DESC"
        'sql = "SELECT id,message_subject,message,target,status,DATE_FORMAT(sent_datetime, '%d/%m/%Y %H:%m:%s') as date_sent FROM msg_master ORDER BY date_sent DESC"
        Me.MessageGridView.DataSource = Nothing
        Me.MessageGridView.DataBind()
        Using conn As New MySqlConnection(ConfigurationManager.ConnectionStrings("ConnString").ConnectionString), da As New MySqlDataAdapter(sql, conn)
            da.Fill(ds, "messages")
        End Using
        Me.MessageGridView.DataSource = ds.Tables(0)
        Me.MessageGridView.DataBind()
        Me.MessageGridView.Visible = True
        Me.MessageGridView.SelectedIndex = -1
        Me.UpdatePanel1.Update()

How to repeat:
Use the code above to read a US date format to UK date format in the select query.

Suggested fix:
Removing the DATE_FORMAT part of the query to a simple sent_datetime as sent_date stops the error occurring and the data is displayed.
[26 Oct 2016 16:35] Nick Franklin
My error, please ignore this report, the date was incorrectly formatted as a string in the database. Apologies.