Good day!

Im developing a client server application that send dataset from the server going to client. I have successfully send my dataset from the server after converting it to xml string. My client had also successfully recieve my xml data from the server. I had also successfully load my xml data to the client dataset. Now the problem is when i try to use the column whith BLOB datatype(originaly from the server) i stuck on with this error. Unable to cast object of type 'System.String' to type 'System.Byte[]'. the line that producess this error is as follows:

        byte[] tempByte;

        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            tempByte = (byte[])ds.Tables[0].Rows[i]["BLOBCol"]; //erro is in this line

            //some code here
        }

this Code is perfecly running on the server. This problem is on the client side. I do not know if the structure of my dataset had been affected when i converted it to xml and back to dataset.

Please help me.....
Thanks in advance.

Recommended Answers

All 3 Replies

The issue is that you are trying to set a byte[] to the value of the cell which is a string, this does not have a direct convert thus your cast does not work.

You will need to do a conversion first.

static byte[] GetBytes(string str)
{
    byte[] bytes = new byte[str.Length * sizeof(char)];
    System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
    return bytes;
}

The above will do this for you, I have obtained it from a simple google. Here is the source post on SO, credit goes to them for it.

Thanks MikeyIsMe your suggestion works but unfurtunately it cause another error. Here is my comple code for the loop. im comparing a template store on a database on a capture feature set using digital persona reader. Here it is:

DPFP.Verification.Verification ver = new DPFP.Verification.Verification();

 DPFP.Verification.Verification.Result res = new DPFP.Verification.Verification.Result();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
    //I add here a toString Method because your parameter is in a form of a string
    tempBytes = GetBytes(ds.Tables[0].Rows[i}["BlogCol"].ToString());

    DPFP.Template template = new DPFP.Template();

    template.DeSerialize(tempBytes);

    ver.Verify(FeatureSet, template, ref res); //here is the error Occurs

    if (res.Verified)
                break;
  }

  if (!res.Verified)

      lblWarning.Text = "Failed";

   else
        lblWarning.Text = "success";

the error says:Exception from HRESULT: 0xFFFFFFF8

silly me the solution of my problem is so simple. Here is the solution i found.

tempBytes = Convert.FromBase64String(ds.Tables[0].Rows[i]["BlogCol"].ToString());

hope this help to someone.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.