Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
hi,

I need to store the images in database and retrive it through asp.net with c#.

I am using sql 2005 database,file uploader and stored procedures.

Please help me to solve this.

Thanks in Advance.
Posted
Updated 19-May-11 21:21pm
v2
Comments
[no name] 20-May-11 3:22am    
What have you tried for this? Please post your code. We will guide you.
gowthammanju 20-May-11 3:53am    
Query :

create database sss
go
use sss
go
create table ss_imgsample(imgid int identity(1,1),imgname varchar(150),imgfile image,primary key(imgid))
go
select * from ss_imgsample
go






Design :

<div style="height: 361px">
file name :
<asp:TextBox ID="TextBox1" runat="server">
<br />
choose the image file
<br />
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="click" />
<br />
<br />
<asp:Label ID="Label1" runat="server">
<br />
<asp:Label ID="Label2" runat="server">
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="imgid" DataSourceID="SqlDataSource1">
<columns>
<asp:BoundField DataField="imgid" HeaderText="imgid" InsertVisible="False"
ReadOnly="True" SortExpression="imgid" />
<asp:BoundField DataField="imgname" HeaderText="imgname"
SortExpression="imgname" />


<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:sssConnectionString %>"
SelectCommand="SELECT * FROM [ss_imgsample]">
</div>






Coding :

using System.Data.SqlClient;

SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=sss;Integrated Security=True");
SqlCommand cmd = new SqlCommand();


protected void Button1_Click(object sender, EventArgs e)
{
attach();
}
protected void attach()
{
lbl_attachments.Text = "";
try
{
if (FileUpload1.HasFile)
{
Label1.Text = fileupload_img.FileName;
FileUpload1.SaveAs(Server.MapPath("~/Tempfiles/" + Label1.Text));
cmd = new SqlCommand("insert into ss_imgsample values('" + TextBox1.Text + "','" + FileUpload1.PostedFile.FileName + "') ", con);
if(cmd.EndExecuteNonQuery()>0)
{
Label1.Text="inserted";
}else{
Label1.Text="not done";
}

}
else
{
Label1.Text = "No file selected";

}
}
catch (Exception ex)
{ Response.Write(ex.Message); }

}



--------------------------------------


we have to insert the value in to database and show the value in grid view.
if u click on the grid view image then the page is redirected ti new page and displays the file name and image.

waiting for ur response.

To store Image in Sql server use following code:
Stream imgStream = FileUpload1.PostedFile.InputStream;
            BinaryReader imgBinary = new BinaryReader(imgStream);
            Byte[] bytes = imgBinary.ReadBytes((Int32)imgStream.Length);
            imgBinary.Close();
            imgStream.Close();
            string imageName = FileUpload1.PostedFile.FileName;
            string imgBinary = Convert.ToBase64String(bytes);
//Store the imageName and imgBinary in the database. Make sure the column data type for imgBinary is Image.


Now to retrieve and show the stored image from database, create new aspx page and put below code in Page_Load event.
string imgName = Request.QueryString["ImageName"].ToString();
            string sql = "Select Img from TestImage where ImgName='" + imgName + "'";
            SqlCommand myCommand = new SqlCommand(sql, con);
            con.Open();
            object objImage = myCommand.ExecuteScalar();
            Response.Clear();
            Response.ContentType = "image/jpeg";
            Response.BinaryWrite((byte[])objImage);
            Response.End();
 
Share this answer
 
Comments
gowthammanju 20-May-11 6:28am    
i am getting error in this line
string imgBinary = Convert.ToBase64String(bytes);
as

1)Cannot implicitly convert type 'string' to 'System.IO.BinaryReader'
2)A local variable named 'imgBinary' is already defined in this scope

can u help me
Ravindra Nidhonkar 23-May-11 2:35am    
You are doing wrong thing. Check the below code again:

Stream imgStream = FileUpload1.PostedFile.InputStream;
BinaryReader imgBinary = new BinaryReader(imgStream);
Byte[] bytes = imgBinary.ReadBytes((Int32)imgStream.Length);
string imgBinary = Convert.ToBase64String(bytes);

 
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