Solved

Query to MySQL from ASP.net returns System.Byte[]

Posted on 2009-06-27
7
1,801 Views
Last Modified: 2012-05-07
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?


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();
        }
    }
}

Open in new window

0
Comment
Question by:Philip Pinnell
  • 3
  • 2
  • 2
7 Comments
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 300 total points
ID: 24730949
0
 
LVL 23

Assisted Solution

by:Tiggerito
Tiggerito earned 200 total points
ID: 24731012
One option maybe to switch to using the dedicated MySQL .Net Connector instead:

http://dev.mysql.com/downloads/connector/net/5.2.html

We use both and over the last few years I have found that the MySQL one is getting very stable while the odbc one is getting worse! We even get odbc errors know that require us to restart the server.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24731767
Hmmm...is there an echo in here?... *BIG GRIN*.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 13

Accepted Solution

by:
Philip Pinnell earned 0 total points
ID: 24732735
Thanks for your suggestion. I will try to get the hosts to implement the .net connector anyway.

However, as often happens, describing the problem has set my mind off in another direction in order to fix this.

My thoughts were that a stored procedure might do the trick. If it only returned strings there should not be a problem. My thinking perhaps wasn't too clear here but following this approach I discovered some flaws in my query. Presumably being "forgiven" under Win2000.

The query that now reside in my stored proc is as follows

     SELECT
     ID,
     head,
     IF(LENGTH(item)> 50, CONCAT(SUBSTRING(item,1,50)," ..."),item) AS item,
     CONCAT("<A HREF=\"newsitem.aspx?from=ai&id=",Cast(ID as char(4)),"\">Read item</a>") AS ReadItem,
     DATE_FORMAT(modified,"%D %b %y") AS modified,    
     forcetop,
     forceverytop,
     modified as sortdate
     FROM newsitems WHERE deleted = 0 ORDER BY sortdate DESC ;


The third field "item" has the single quotes replaced with double quotes.
The fourth likewise with escaped double quotes and, perhaps more importantly, a cast of the integer ID.
Fifth field, a proper format of the date rather than just using substring.

so with this in a sp the page works! here I don't know if the same as in line sql works but I don't care.

just need to fix all the other bits than don't work now.

Again thanks for your participation.






0
 
LVL 13

Author Comment

by:Philip Pinnell
ID: 24732760
It remains to be seen whether the .net connector would forgive the "weaknessses" in my query.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24733081
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.
0
 
LVL 23

Expert Comment

by:Tiggerito
ID: 24733151
That's the second time in a week I've been "pipped to the post". Time for that typing class.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to prevent website from publishing with errors 9 45
How can i use linq for parsing string datetime to real datetime? 16 44
Printing 1 51
MySQL Memory Keeps Increasing 4 34
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question