Photo upload
i have employee details screen in that screen i want to upload Employee photo using c# with example.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Feb 5, 2012, 11:15 PM
Try this...
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Details_with_photo._Default" %>
Default.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Details_with_photo
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
protected void btn_insert_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
int len = Upload.PostedFile.ContentLength;
byte[] pic = new byte[len + 1];
Upload.PostedFile.InputStream.Read(pic, 0, len);
con.Open();
str = "insert into employee values (@name,@address,@photo)";
com=new SqlCommand(str, con);
com.Parameters.AddWithValue("@name", txtname.Text);
com.Parameters.AddWithValue("@address", txtaddress.Text);
com.Parameters.AddWithValue("@photo", pic);
com.ExecuteNonQuery();
con.Close();
lblmsg.Text = "Employee Details Inserted Successfully!";
Response.Redirect("employee.aspx");
}
}
}
employee.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="employee.aspx.cs" Inherits="Details_with_photo.employee" %>
employee.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Details_with_photo
{
public partial class employee : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select id,name,address from employee";
com = new SqlCommand(str, con);
SqlDataReader reader;
reader = com.ExecuteReader();
repeater1.DataSource = reader;
repeater1.DataBind();
con.Close();
}
}
}
image.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="image.aspx.cs" Inherits="Details_with_photo.image" %>
image.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Data.SqlClient;
namespace Details_with_photo
{
public partial class image : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select photo from employee where id = '" + Request.QueryString["id"] + "'";
com=new SqlCommand(str, con);
MemoryStream stream = new MemoryStream();
byte[] image = (byte[])com.ExecuteScalar();
stream.Write(image, 0, image.Length);
Bitmap bitmap = new Bitmap(stream);
Response.ContentType = "image/Jpeg";
bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
con.Close();
stream.Close();
}
}
}
Thanks
If this post helps you mark it as answer