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

}

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

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

Type conversion is required.

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

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"];

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!

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

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

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

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

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.