Click here to Skip to main content
15,913,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below is my c# code.. i want to load image on image1 image control
C#
 protected void Page_Load(object sender, EventArgs e)
        {
 if (Convert.ToInt16( Session["CompanyID"].ToString()) > 0)
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select * from Companies where CompanyID="+Session["CompanyID"],con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds,"Companies");

                if (ds.Tables[0].Rows.Count > 0)
                {
                    Label1.Text = ds.Tables[0].Rows[0]["EmailID"].ToString();
Image1= ???

}
Posted
Comments
Charan_Kumar 14-Mar-14 3:14am    
are you storing image in bytes or saving only path in database?

 
Share this answer
 
Comments
jayraj86 12-Mar-14 4:16am    
how to make changes in this code ?
You don't provide enough info relating to your question, such as where is the image stored, how is it stored (bytes, base64string, ect...), and what control you are trying to attach the image to.

However base on what you have provided this may help you.
exmple:
C#
protected void Page_Load(object sender, EventArgs e) {
    if (Convert.ToInt16(Session["CompanyID"].ToString()) > 0) {
        con.Open();
        SqlCommand cmd = new SqlCommand(
                "select * from Companies where CompanyID="+
                Session["CompanyID"],con);

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds,"Companies");
 
        if (ds.Tables[0].Rows.Count > 0) {
            Label1.Text = ds.Tables[0].Rows[0]["EmailID"].ToString();

            //Assuming you are storing the image as bytes:
            //===========================================
            //Get bytes from database field
            //Where "MyImage" is the Field name in your table
            byte[] img = ds.Tables[0].Rows[0]["MyImage"];
            Image image1 = ConvertImage(img);

            if(image1 != null) //check for image
                //Add to your image to the control, if exist
        }
    }
}

private static Image ConvertImage(byte[] img) {
    try {
        if (img == null || img.Length == 0)
            throw new Exception("Byte[] empty");

        //create stream & convert byte array to image
        MemoryStream ms = new MemoryStream(img); //add bytes to steam
        Image i = Image.FromStream(ms); //create image from stream

        ms.Dispose(); //clean-up, release stream resource

        return i; //image/null return value
    }
    catch(exception e) { //Error:
        //Do somthing
        return null; //genaric return value
    }
}
 
Share this answer
 
Comments
jayraj86 13-Mar-14 4:21am    
I want to fetch image on page load from table in which the path of image is stored and the actual image is in the folder of solution. I have used image control. should i use another control ?
jayraj86 13-Mar-14 4:23am    
Datatype of image is nvarchar(MAX)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900