TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
priya mishra
NA
61
44.9k
Retrive image from database using c#
Apr 23 2014 11:23 AM
i want to retrive image from database if user uploaded or add image in his/her profile then next time this image retrive from database when user login and display in image control like social site.
My code is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class profileadd : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=trial2;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("Select * from login where username='"+Session["username"]+"'",con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
Image1.ImageUrl = "~/Images" + "FilePath"+"FileName".ToString(); ;
dr.Close();
cmd.ExecuteNonQuery();
con.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile != null)
{
if (FileUpload1.PostedFile != null)
{
string FileName = FileUpload1.FileName.ToString();
FileUpload1.SaveAs(Server.MapPath("~/Images/" + FileName));
SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=trial2;Integrated Security=True");
SqlCommand cmd = new SqlCommand("insert login(FileName, FilePath) values(@FileName, @FilePath)", con);
cmd.Parameters.AddWithValue("@FileName", FileName);
cmd.Parameters.AddWithValue("@FilePath", "images/" + FileName);
cmd.CommandType = CommandType.Text;
try
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
con.Dispose();
}
Image1.ImageUrl = "~/Images/" + "/" + FileUpload1.FileName.ToString();
Label1.Text = "You are successfully uploaded";
}
}
}
}
In pageload i am retriving images but i think some mistakes are here .so image retrive but not displayed .display only image icon.
pls give me appropriate code and if it possible then correct this code
Reply
Answers (
2
)
Dynamic data type
C# and unable to close pdf Files methods Load() & Dispose()