Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 4-Apr-16 23:18pm
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?

//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();
}
 
Share this answer
 
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.
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);
 
Share this answer
 
C#
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#[^]
 
Share this answer
 

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