Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How to save image file in database in asp.net
Posted
Updated 2-Mar-12 2:22am
v2
Comments
Anuja Pawar Indore 2-Mar-12 8:22am    
Removed extra pre tag

see below link you may get some idea
Storing Images in MySQL using ASP.NET[^]
 
Share this answer
 
have a look at the article this describes how to save images:
Save An Image Into SQL Server 2000 Database[^]
 
Share this answer
 
 
Share this answer
 
read image in byte and then save byte in database
there is one example:-
i give you my given answer link which is accepted i hope it will help you

how store different type of image into database[^]
 
Share this answer
 
//storing image in data base and access in gridvies


.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;


public partial class insertndaccessimage : System.Web.UI.Page
{//make a sqlconnection
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["contrlConnectionString"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
Label1.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{ string path;
//check if file upload has file
if (FileUpload1.HasFile)
{

try
{
//setting path to store in database
path = @~\images\" + FileUpload1.FileName;
//save file in folder(images) in application's root directory
FileUpload1.SaveAs(Server.MapPath(path));

SqlCommand cmd = new SqlCommand();
cmd.CommandText = insert into Person values(@Lastname,@FristName,@Image)";
cmd.Connection = con;
cmd.Parameters.Add(@LastName", SqlDbType.VarChar, 50).Value = TextBox2.Text;
cmd.Parameters.Add(@FristName", SqlDbType.VarChar, 50).Value = TextBox3.Text;
cmd.Parameters.Add(@Image", SqlDbType.VarChar, 100).Value = path;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Label1.Visible = true;
Label1.Text = record uploaded successfully";
Label1.ForeColor = System.Drawing.Color.Red;
}
catch (Exception e1)
{
Response.Write(e1.Message );
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
object o1=new object();
EventArgs ev=new EventArgs();
GridView1_Load(o1,ev );

}
protected void GridView1_Load(object sender, EventArgs e)
{

}
}



.aspx



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="insertndaccessimage.aspx.cs" Inherits="insertndaccessimage" %>

<!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></title>
<style type="text/css">
.style1
{
width: 60%;
}
.style2
{
width: 102px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<table align="center" class="style1">
<tr>
<td class="style2" width="30%">
preson id</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2" width="30%">
last name</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2" width="30%">
first name</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2" width="30%">
image</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
</tr>
<tr>
<td class="style2" width="30%">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</td>
<td>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" Width="119px"
onclick="Button1_Click" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
Text="load dridview" Width="117px" />
</td>
</tr>
<tr>
<td class="style2" width="30%">
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:contrlConnectionString %>"
SelectCommand="SELECT * FROM [Person]"></asp:SqlDataSource>
</td>
<td>
<br />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" BackColor="White" BorderColor="#E7E7FF"
BorderStyle="None" BorderWidth="1px" CellPadding="3"
DataSourceID="SqlDataSource1" EnableModelValidation="True"
GridLines="Horizontal" onload="GridView1_Load">
<AlternatingRowStyle BackColor="#F7F7F7" />
<Columns>
<asp:BoundField DataField="PersonID" HeaderText="PersonID"
InsertVisible="False" ReadOnly="True" SortExpression="PersonID" />
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />
<asp:BoundField DataField="FristName" HeaderText="FristName"
SortExpression="FristName" />
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="imge" AlternateText="Image not available" Width="100px" Height="100px" ImageUrl='<%#Eval("Image")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
</asp:GridView>
</td>
</tr>
</table>

</div>
</form>
</body>
</html>
 
Share this answer
 
v2

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