Andrew Crofts
asked on
Query to MySQL from ASP.net returns System.Byte[]
I have an ASP.net website that has been working fine. Some of what is displayed is retrieved from a MySQL database. For example, news items.
Recently where a date was retrieved and a URL consructed from info from the db suddenly started returning System.Byte[].
On googling this seemed to be related to a version of the odbc. After a lot of to-ing and fro-ing with the site's support, the odbc is the latest and MySQL is v5. The problem still remains.
All the while my "production" machine is an ancient laptop running win2000 (latest odbc 3.51 and MySQL5) everything works fine.
I have discovered however that on my vista desktop (latest odbc, MySQL) the problem also happens.
As dealing with the website support is like wading through treacle I thought I should try to fix it on the vista machine and I might then have an idea of what to do with the live site.
A workaround may be changing my code.
Still reading?
the c# code is posted below for one page where the problem occurs. The fields that show as System.Byte[] are the modified date and the "read more" link. Both have string operations applied to them. Removing this formatting results in the correct raw data being retrieved.
But one other field has string operations but is ok. The two that error involve a date and the other an int.
The url of an example is http://www.badttc.org.uk/olditems.aspx
Any idea what is going on here? and how I might get round it?
Recently where a date was retrieved and a URL consructed from info from the db suddenly started returning System.Byte[].
On googling this seemed to be related to a version of the odbc. After a lot of to-ing and fro-ing with the site's support, the odbc is the latest and MySQL is v5. The problem still remains.
All the while my "production" machine is an ancient laptop running win2000 (latest odbc 3.51 and MySQL5) everything works fine.
I have discovered however that on my vista desktop (latest odbc, MySQL) the problem also happens.
As dealing with the website support is like wading through treacle I thought I should try to fix it on the vista machine and I might then have an idea of what to do with the live site.
A workaround may be changing my code.
Still reading?
the c# code is posted below for one page where the problem occurs. The fields that show as System.Byte[] are the modified date and the "read more" link. Both have string operations applied to them. Removing this formatting results in the correct raw data being retrieved.
But one other field has string operations but is ok. The two that error involve a date and the other an int.
The url of an example is http://www.badttc.org.uk/olditems.aspx
Any idea what is going on here? and how I might get round it?
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data.Odbc;
using System.Data;
using System.Drawing;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web;
using System;
public partial class _Default : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
LoadItems();
}
private void LoadItems()
{
string strConnstring = "DSN=MyODBCName;USER=user;PASSWORD=pass;";
System.Data.Odbc.OdbcConnection conn = new System.Data.Odbc.OdbcConnection(strConnstring);
try
{
conn.Open();
string sql = "select ID, head, ";
sql += "if(length(item)> 50, concat(substring(item,1,50),' ...'),item) as item, ";
sql += "concat('<A HREF=\"newsitem.aspx?from=ai&id=',ID,'\">Read item</a>') as ReadItem, ";
sql += "substring(modified,1,11) as modified, forcetop, forceverytop ";
sql += "from newsitems where deleted = 0 order by modified desc ";
System.Data.Odbc.OdbcCommand cmd = new OdbcCommand(sql, conn);
System.Data.Odbc.OdbcDataAdapter da = new OdbcDataAdapter(cmd);
System.Data.DataSet ds = new DataSet();
da.Fill(ds);
//this.txtHead1.ScrollBars = ScrollBars.None ;
this.DataGrid1.DataSource = ds;
this.DataGrid1.DataBind();
}
catch (Exception ex)
{
this.lblItem1.Text = ex.Message.ToString();
}
finally
{
conn.Close();
}
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hmmm...is there an echo in here?... *BIG GRIN*.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It remains to be seen whether the .net connector would forgive the "weaknessses" in my query.
ODBC is not as optimized as .NET data providers, especially when dealing with SQL Server, so you should always go that direction when there is the opportunity.
That's the second time in a week I've been "pipped to the post". Time for that typing class.