Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with: Unable to cast object 2

Status
Not open for further replies.

aalmeida

MIS
Aug 31, 2000
468
US
Unable to cast object of type 'System.Data.Linq.Binary' to type 'System.Byte[]'.
Another C# Linq to SQL issue this time with Image datatype conversion.
The situation is this:
I add an image to a database table to a column that is type Image using the folowing process:
Code:
/// <summary>
        /// The insert Personal Info method.
        /// Using a stored procedure for insert data.
        /// </summary>
private Int32 newSigCard()
        {
            SigCardLinqDataContext db = new SigCardLinqDataContext();
            var q = db.usp_newassinante(  
                                  name_txtbx.Text.ToString()
                                , cpf_txtbx.Text.ToString()
                                , rg_txtbx.Text.ToString()                                
                                , abrev_txt.Text.ToString()
                                , civil_txt.Text.ToString()
                                , profissao_txt.Text.ToString()
                                , nascimento_txt.Text.ToString()
                                , rua_txt.Text.ToString()
                                , bairro_txt.Text.ToString()
                                , cidade_txt.Text.ToString()
                                , uf_txt.Text.ToString()
                                , cep_txt.Text.ToString());
        return q;
        }
        
        /// <summary>
        /// The insert image method to add new scaned signature 
        /// to a specific assinante.
        /// Using a stored procedure for image data.
        /// </summary>
        private Int32 newSigImg(Int32 id, Binary sigimg)
        {
            //Int32 id, Binary sigimg
            SigCardLinqDataContext db = new SigCardLinqDataContext();
            Int32 newimage_id = db.usp_newscanedimage(id, sigimg);

            return (newimage_id);
        }
/// <summary>
/// The Save action.
/// To start the process to add the personal data and the personal imag to the database.
/// </summary>
private void Salvar_Click(object sender, EventArgs e)
        {
            Int32 Card_id = newSigCard();
            Bitmap img = (Bitmap)newImagesrc;
            // First we have to convert the Bitmap into a Binary data type 
            // Assuming img is an Image object:
            MemoryStream stream = new MemoryStream();
            img.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
            Byte[] imgbytes = stream.ToArray();
            //And finally we put the string to the sproc  
            Int32 Image_id = newSigImg(Card_id, (Binary)imgbytes);
        }
And this is the process to retrieve the data from the database and display it on the form:
Code:
/// <summary>
/// The Function to convert the Binary datatype from the database into bitmap to display.
/// </summary>
private void PictureFormat(object sender, ConvertEventArgs e)
        {
            // e.Value is the original value
            Byte[] img = (Byte[])e.Value;
            MemoryStream ms = new MemoryStream();
            int offset = 0;
            ms.Write(img, offset, img.Length - offset);
            Bitmap bmp = new Bitmap(ms);
            ms.Close();

            // Writes the new value back
            e.Value = bmp;
        }

/// <summary>
/// Retrieve the data from the database and put it in the form controls.
/// </summary>
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int colIndex = e.ColumnIndex;
            int rowIndex = e.RowIndex;
            if (rowIndex >= 0 && colIndex >= 0)
            {
                DataGridViewRow theRow = dataGridView1.Rows[rowIndex];
                // Write a line for each control that needs to 
                // be filed with information from the Linq Sproc
                // Get a typed table to run queries.
                SigCardLinqDataContext dc = new SigCardLinqDataContext();
                Table<vw_all> Assinaturas = dc.GetTable<vw_all>();
                IQueryable<vw_all> q = from c in Assinaturas
                        where c.card_id == (int)theRow.Cells[0].Value
                        select c;
  
              foreach (vw_all a in q)
                {

                Binding bdPhoto = new Binding("Image", a, "scan_img");
                bdPhoto.Format += new ConvertEventHandler(this.PictureFormat);
                assinaturas_img.DataBindings.Add(bdPhoto);
                //assinaturas_img.Image = image; //.FromStream(a.scan_img);// byte[Convert.ToInt32(a.scan_img)];
                assinaturas_img.Visible = true;
                
                }
I'm getting the following erro on PictureFormat(object sender, ConvertEventArgs e) at Byte[] img = (Byte[])e.Value;
Unable to cast object of type 'System.Data.Linq.Binary' to type 'System.Byte[]'.

AL Almeida
CIO
May all those that come behind us, find us faithful.
 
yes! I had searched all over the internet but this post had what I needed. quick and easy thanks.
For the benefit of all the others that might get hear looking for a solution I had to modify the display foreeach part of the program as below:
Code:
foreach (vw_all a in q)
                {

                byte[] bdPhoto = (byte[])(a.scan_img).ToArray();
                assinaturas_img.Image = Image.FromStream(new MemoryStream(bdPhoto));
                //assinaturas_img.Image = image; //.FromStream(a.scan_img);// byte[Convert.ToInt32(a.scan_img)];
                assinaturas_img.Visible = true;
                
                }

This eliminates the need for the PictureFormat function.

Thanks,

AL Almeida
CIO
May all those that come behind us, find us faithful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top