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.