Click here to Skip to main content
15,883,829 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Img.aspx.cs" Inherits="Img" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnUpload" runat="server" onclick="btnUpload_Click"
            Text="Upload Image" />
        <br />
        <br />
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <br />
        <br />
        <asp:Button ID="btnRetrieve" runat="server" onclick="btnRetrieve_Click"
            Text="Retrieve Image" />
        <br />
        <br />
        <asp:Image ID="Image1" runat="server" />
        <br />
        <asp:GridView ID="GridView1" runat="server"
            onselectedindexchanged="GridView1_SelectedIndexChanged">
        </asp:GridView>
        <br />
    </div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </form>
</body>
</html>




This is the coding for inserting image.

protected void btnUpload_Click(object sender, EventArgs e)
    {

        FileUpload img = (FileUpload)FileUpload1;
       
        if (img.HasFile && img.PostedFile != null)
        {
            HttpPostedFile File = FileUpload1.PostedFile;
            byt = new Byte[File.ContentLength];
            File.InputStream.Read(byt, 0, File.ContentLength);
            SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=adventureworks;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into ImageStore values(@ID,@ImageContent)", con);            
            cmd.Parameters.AddWithValue("@ID",TextBox1.Text);         
            cmd.Parameters.AddWithValue("@ImageContent", byt);            
            cmd.ExecuteNonQuery();           
            con.Close();
        }  
    } 

How shall i retrieve image from database without using .ashx file.I want directly retrieve all those images by gridview from database.Can you help me?
Posted
Updated 8-Apr-11 0:21am
v5
Comments
Toniyo Jackson 8-Apr-11 3:04am    
Added pre tag for code

 
Share this answer
 
Comments
Banajyotsna 7-Apr-11 7:18am    
Sir it is not running in my visual studio 2008
try these Links

Link 1:[^]

Link 2:[^]
 
Share this answer
 
Go there-[Save and Retrieve Images from the Database][^] to read step by step process to store and Retrieve Image from database.
 
Share this answer
 
Comments
Banajyotsna 7-Apr-11 6:40am    
Sir i want to retrieve the image from database without .ashx file.How is it possible?how shall i retrieve the image from database directly without .ashx file

I already succeed in that concept means using .ashx file .now i want how shall i retrieve those images without .ashx file?
I think this might help you...

make a class image conversion

and add this code..

public class Image_Conversion
{
public byte[] ReadFile(string ImagePath)
{
byte[] Image_Binary_data = null;
FileInfo FInfo = new FileInfo(ImagePath);
long numBytes = FInfo.Length;
FileStream FStream = new FileStream(ImagePath, FileMode.Open, FileAccess.Read);
BinaryReader BR = new BinaryReader(FStream);
Image_Binary_data = BR.ReadBytes((int)numBytes);
return Image_Binary_data;
}
}

then...

add this code to the field where you want to load the picture....
//this is to load the picture from database...

byte[] ImageData = null;
ImageData = "load the binary file from database using Sql Query";
Image NewImage;
using (MemoryStream Marine_MS = new MemoryStream(ImageData, 0, ImageData.Length))
{
Marine_MS.Write(ImageData, 0, ImageData.Length);
NewImage = Image.FromStream(Marine_MS, true);
}
picbox = NewImage;


converting the image into binary...

Image_Conversion Image_Con = new Image_Conversion();

string Path = (@Application.StartupPath+&quot;\\Marine Resource\\Image.jpg&quot;).ToString();
byte[] ImageFile = Image_Con.ReadFile(Path);
 
Share this answer
 
v2
Comments
Banajyotsna 8-Apr-11 6:15am    
can you describe briefly? where which coding i have to write in which button click?
simple wway of insert a image in data base like this

for button click

try
{
if (fleUpload.HasFile)
{
byte[] b = fleUpload.FileBytes;



if (con.State == ConnectionState.Open)
con.Close();
con.Open();
cmd.CommandText = "insert into imageTable(imageName,images) values(@name,@img)";;
cmd.Parameters.AddWithValue("@name", txtimage.Text);
cmd.Parameters.AddWithValue("@img", b);
int i=cmd.ExecuteNonQuery();
if (i > 0)
{
Response.Write("quot;Insertion Sucess....");
}
else
{
Response.Write("Insertion faild....");
}


}
else
{
Response.Write("select a images");
}

}
catch
{
}
 
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