1,105,633 Community Members

Cannot implicitly convert type 'byte' to 'byte[]'

Member Avatar
silent.saqi
Newbie Poster
8 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

please help i am confused plz plz plz

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class handler : System.Web.UI.Page
{
    protected void Page_Load(object sender, System.EventArgs e)
    {

        if (Request.QueryString["ImageID"] != null)
        {
            SqlConnection con = ConnectionManager.GetConnection();
            string strQuery = "select Emp_ID,photo,cnic from Employee where Emp_ID=@id";

            SqlCommand cmd = new SqlCommand(strQuery);

            cmd.Parameters.Add("@id", SqlDbType.NVarChar).Value = Convert.ToInt32(Request.QueryString["ImageID"]);

            SqlDataAdapter sda = new SqlDataAdapter();

            cmd.CommandType = CommandType.Text;

            cmd.Connection = con;

            DataTable dt = new DataTable();

            try
            {
                con.Open();

                sda.SelectCommand = cmd;

                sda.Fill(dt);


            }
            catch (Exception ex)
            {
                dt = null;


            }
            finally
            {
                con.Close();

                sda.Dispose();

                con.Dispose();

            }

            if (dt != null)
            {
                byte[] bytes ;
                if (dt.Rows.Count == 0)
                {
                    bytes = null;
                }
                else
                {                    
                    bytes = Convert.ToByte(dt.Rows[1]["photo"]); // Help required please help me
                   
                }
                Response.Buffer = true;

                Response.Charset = "";

                Response.Cache.SetCacheability(HttpCacheability.NoCache);

                Response.ContentType = "image/jpg";

                //Response.AddHeader("content-disposition", "attachment;filename=" & dt.Rows(0)("Name").ToString())

                Response.BinaryWrite(bytes);

                Response.Flush();

                Response.End();

            }

        }
    }
    public handler()
    {
        Load += Page_Load;
    }

}
Member Avatar
dwarvenassassin
Light Poster
30 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

a byte is a single value from 0 to 255. You cannot convert multiple bytes in a single one!

Member Avatar
silent.saqi
Newbie Poster
8 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

a byte is a single value from 0 to 255. You cannot convert multiple bytes in a single one!

Sir, Thnks in advance, em a newbee

could you please help me i wana get just show picture (which saves in binary format in database) to gridview.

plz

Member Avatar
__avd
Posting Genius (adatapost)
6,703 posts since Oct 2008
Reputation Points: 1,811 [?]
Q&As Helped to Solve: 1,272 [?]
Skill Endorsements: 61 [?]
Moderator
 
0
 

Type conversion is required.

bytes = (byte[])dt.Rows[1]["photo"];
Member Avatar
silent.saqi
Newbie Poster
8 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Type conversion is required.

bytes = (byte[])dt.Rows[1]["photo"];

Thnks for reply ... but now it occur new error

Unable to cast object of Type 'System.DBNull' to 'System.Byte[]'.

bytes = (byte[])dt.Rows[1]["photo"];
Member Avatar
Philippe.Lahaie
Posting Whiz
376 posts since Oct 2007
Reputation Points: 42 [?]
Q&As Helped to Solve: 60 [?]
Skill Endorsements: 9 [?]
 
0
 

then the column "photo" in row 1 of your datatable does not contain any data, its set to null, to secure your code against such exceptions, test it before trying to convert...

something like :

if(dt.Rows[1]["photo"] != System.DBNull){
   bytes = (byte[])dt.Rows[1]["photo"];
}

won't make your picture appear but at least as long as your datatable contains no image it will not crash, now you gotta figure why there isn't any image in your database!

Member Avatar
silent.saqi
Newbie Poster
8 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

then the column "photo" in row 1 of your datatable does not contain any data, its set to null, to secure your code against such exceptions, test it before trying to convert...

something like :

if(dt.Rows[1]["photo"] != System.DBNull){
   bytes = (byte[])dt.Rows[1]["photo"];
}

won't make your picture appear but at least as long as your datatable contains no image it will not crash, now you gotta figure why there isn't any image in your database!

thanks alot in advance

well rite know i don't have data in my database.... may b that is y it happening... if data found then this error will not be occur.. u mean to say that.....

Member Avatar
Philippe.Lahaie
Posting Whiz
376 posts since Oct 2007
Reputation Points: 42 [?]
Q&As Helped to Solve: 60 [?]
Skill Endorsements: 9 [?]
 
0
 

yes , if there was an image in your database, then the line :

bytes = (byte[])dt.Rows[1]["photo"];

appears like it would correctly load it in bytes, but since you dont have data in your database, the dt.Rows[1]["photo"] returned "System.DBNull" and you tried to implicitly cast that to a byte array : "(byte[])System.DBNull" which is a type mismatch and that is what caused an exception to be thrown.

the "if" statement i added simply checks ahead of time if there is data in the database, if there isn't, then it will not try to cast it to bytes, and there wont be any exception , but if there is data, it will go inside the "if statement" and convert the image to a byte array! :)

Member Avatar
silent.saqi
Newbie Poster
8 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

yes , if there was an image in your database, then the line :

bytes = (byte[])dt.Rows[1]["photo"];

appears like it would correctly load it in bytes, but since you dont have data in your database, the dt.Rows[1]["photo"] returned "System.DBNull" and you tried to implicitly cast that to a byte array : "(byte[])System.DBNull" which is a type mismatch and that is what caused an exception to be thrown.

the "if" statement i added simply checks ahead of time if there is data in the database, if there isn't, then it will not try to cast it to bytes, and there wont be any exception , but if there is data, it will go inside the "if statement" and convert the image to a byte array! :)

Thnks Buddy its solved....

Question Answered as of 2 Years Ago by Philippe.Lahaie, __avd and dwarvenassassin
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article