This will show you how to store images
to database and bind to gridView. I have used FileUpload control to upload
image from computer and it will be stored to database as binary format and
display these images to GridView.
First we have to
create a table with three fields like ImageId, ImageName and Image. We need to
store Image as binary format so we have to use varbinary() datatype for the column Image.
Then we design our ASPX page. First we have add TextBox control for
image name, FileUpload control for image and Button to store data in DB, Gridview for Display Image.
<table>
<tr>
<td>
ImagrName:
</td>
<td>
<asp:TextBox ID="txtImageName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Browse Image:
</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btnSubmit"runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td colspan="2">
<asp:GridView ID="gvwImageDetails" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Image Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%#Eval("ImageName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="imgPreview"
ImageUrl='<%#"ShowImage.ashx?ImageId="+
Eval("ImageId") %>' runat="server"
Height="120px"
Width="100px"
/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
On button click event we have to write following code to store image to
database.
protected
void btnSubmit_Click(object
sender, EventArgs e)
{
try
{
Byte[]
bImage = null;
if
(FileUpload1.HasFile)
{
string
filename = FileUpload1.PostedFile.FileName;
string filePath = Path.GetFileName(filename);
Stream
fs = FileUpload1.PostedFile.InputStream;
BinaryReader
br = new BinaryReader(fs);
bImage = br.ReadBytes((Int32)fs.Length);
string
strQuery = "insert into Images
(ImageName,Image) values (@ImgName,@Img)";
string
strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLConString"].ConnectionString;
using
(SqlConnection con = new SqlConnection(strConnString))
{
con.Open();
SqlCommand
cmd = new SqlCommand(strQuery,
con);
cmd.Parameters.Add("@ImgName", SqlDbType.NVarChar,
50).Value = txtImageName.Text;
cmd.Parameters.Add("@Img", SqlDbType.Binary).Value
= bImage;
cmd.ExecuteNonQuery();
con.Close();
}
}
BindGrid();
}
catch (Exception ex)
{
}
}
Bind GridView Code
private
void BindGrid()
{
DataSet
ds = new DataSet();
string
strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLConString"].ConnectionString;
using (SqlConnection con = new
SqlConnection(strConnString))
{
con.Open();
string
cmdstring = "Select distinct
ImageId,ImageName,Image from Images";
SqlCommand
cmd = new SqlCommand(cmdstring,
con);
SqlDataAdapter
adp = new SqlDataAdapter(cmd);
adp.Fill(ds);
gvwImageDetails.DataSource = ds;
gvwImageDetails.DataBind();
}
}
Then Add new generic handler in the Project, here ShowImage.ashx
is my generic handler.
Now we have Handler file containing "ProcessRequest(HttpContext context)" method which gets
triggered automatically.
public
void ProcessRequest(HttpContext
context)
{
SqlDataReader
dr = null; SqlConnection
conn = null;
SqlCommand selectcmd = null;
try
{
conn = new
SqlConnection (System.Configuration.ConfigurationManager.ConnectionStrings["SQLConString"].ConnectionString);
selectcmd = new SqlCommand("select Image from Images where ImageId="
+ context.Request.QueryString["ImageId"],
conn);
conn.Open();
dr = selectcmd.ExecuteReader();
while
(dr.Read())
{
context.Response.ContentType = "image/jpg";
context.Response.BinaryWrite((byte[])dr["Image"]);
}
dr.Close();
}
finally
{
conn.Close();
}
}
Now
run the Project The result is Like given below.
This
tutorial will show you how to store images to database and bind to gridView. I
have used FileUpload control to upload image from our computer and it’ll be
stored to database as binary format and display these images to GridView.
First we have to create a table with
three fields, ImageID, ImageName and Image. We need to store Image as binary
format so we have to use varbinary() datatype.
- See more
at:
http://www.dotnetfox.com/articles/store-images-to-database-and-bind-to-gridview-in-Asp-Net-1114.aspx#sthash.He3NynJk.dpuf
This
tutorial will show you how to store images to database and bind to gridView. I
have used FileUpload control to upload image from our computer and it’ll be
stored to database as binary format and display these images to GridView.
First we have to create a table with
three fields, ImageID, ImageName and Image. We need to store Image as binary
format so we have to use varbinary() datatype.
- See more
at:
http://www.dotnetfox.com/articles/store-images-to-database-and-bind-to-gridview-in-Asp-Net-1114.aspx#sthash.He3NynJk.dpuf