Click here to Skip to main content
12,758,918 members (33,924 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
dear
I want to retrieve a image form the sql data base and in the sql database the image data type is image and i want to retrieve in the image control. plz help me out
Posted 4-Jul-12 1:02am
Updated 5-Apr-16 0:18am
v2
Comments
Vani Kulkarni 4-Jul-12 6:09am
   
What have you tried till now? What has happened to Google.com at your place? Did you try searching the same question in google?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

try
    {
        SqlCommand cmdSelect=new SqlCommand("select Picture" + 
              " from tblImgData where ID=@ID",this.sqlConnection1);
        cmdSelect.Parameters.Add("@ID",SqlDbType.Int,4);
        cmdSelect.Parameters["@ID"].Value=this.editID.Text;

        this.sqlConnection1.Open();
        byte[] barrImg=(byte[])cmdSelect.ExecuteScalar();
        string strfn=Convert.ToString(DateTime.Now.ToFileTime());
        FileStream fs=new FileStream(strfn, 
                          FileMode.CreateNew, FileAccess.Write);
        fs.Write(barrImg,0,barrImg.Length);
        fs.Flush();
        fs.Close();
        pictureBox1.Image=Image.FromFile(strfn);
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        this.sqlConnection1.Close();
    }


few links:
Storing and Retrieving Images from SQL Server using Microsoft .NET[^]
GridViewImages from DB in ASP.NET using C#[^]
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

In this example i have fetched the binary code from the database.


SqlCommand command = new SqlCommand("Select Image From Table",Connection);

byte[] objByte = ((byte[])command.ExecuteScalar());

System.IO.MemoryStream objStream =new System.IO.MemoryStream(objByte);

Picture.Image=Image.FromFile(objStream);
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

//how to retrive text, image form the database using asp.net c #


protected void SelectSubCat_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"data source=localhost\sqlexpress;initial catalog=consultancy;integrated security=true;pooling=false");
SqlDataReader dr;

SqlCommand cmd = new SqlCommand("select * from category where subcatName= '"+ DropDownList2.SelectedValue+"'" , con);
con.Open();
dr = cmd.ExecuteReader();

while (dr.Read())
{
TextBox1.Text = dr["content"].ToString();
TextBox2.Text = dr["description"].ToString();

byte[] image = (byte[])(dr["image"]);
string base64String = Convert.ToBase64String(image);
Image1.ImageUrl = String.Format("data:image/jpg;base64,{0}", base64String);
}
con.Close();
}
  Permalink  
Comments
CHill60 5-Apr-16 5:28am
   
This question was asked and answered over 3 years ago. Your badly formatted solution adds absolutely nothing to the previous answers.

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

    Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 5 Apr 2016
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100