Click here to Skip to main content
14,265,990 members
Rate this:
Please Sign up or sign in to vote.
<%@ 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
Rate this:
Please Sign up or sign in to vote.

Solution 1

   
Comments
Banajyotsna 7-Apr-11 7:18am
   
Sir it is not running in my visual studio 2008
Rate this:
Please Sign up or sign in to vote.

Solution 2

try these Links

Link 1:[^]

Link 2:[^]
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

Go there-[Save and Retrieve Images from the Database][^] to read step by step process to store and Retrieve Image from database.
   
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?
Rate this:
Please Sign up or sign in to vote.

Solution 4

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);
   
v2
Comments
Banajyotsna 8-Apr-11 6:15am
   
can you describe briefly? where which coding i have to write in which button click?
Rate this:
Please Sign up or sign in to vote.

Solution 6

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

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




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