1.11M Members

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

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

}
 
0
 

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

 
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

 
0
 

Type conversion is required.

bytes = (byte[])dt.Rows[1]["photo"];
 
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"];
 
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!

 
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.....

 
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! :)

 
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