Can't Display Profile Image using asp .net

Posted by Webmasters964 under ASP.NET on 9/21/2013 | Points: 10 | Views : 1938 | Status : [Member] | Replies : 2
Hi

Can't Display Profile Image using asp .net

<asp:Image ID="Image1" runat="server" />

 protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!Page.IsPostBack)
{
lb1.Text = "<b><font color=Brown>" + "Hi.." + "</font>" + "<b><font color=red>" + Session["UserName"] + "</font>";
bind();
}
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.Message.ToString() + "');</script>");
}

}


private void bind()
{
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand("select image from Login where UserName='" + Session["UserName"] + "'", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Image1.ImageUrl = dr[0].ToString();
}
con.Close();
}


Any Idea ..




Responses

Posted by: Bandi on: 9/21/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
protectedvoidPage_Load(objectsender,EventArgse){if(Request.QueryString["ImageID"] !=null){stringstrQuery ="select Name, ContentType, Data from tblFiles where id=@id";SqlCommandcmd =newSqlCommand(strQuery);cmd.Parameters.Add("@id",SqlDbType.Int).Value=Convert.ToInt32 (Request.QueryString["ImageID"]);DataTabledt = GetData(cmd);if(dt !=null){Byte[] bytes = (Byte[])dt.Rows[0]["Data"];Response.Buffer =true;Response.Charset ="";Response.Cache.SetCacheability(HttpCacheability.NoCache);Response.ContentType = dt.Rows[0]["ContentType"].ToString();Response.AddHeader("content-disposition","attachment;filename="+ dt.Rows[0]["Name"].ToString());Response.BinaryWrite(bytes);Response.Flush();Response.End();}}}



reference:
http://aspsnippets.com/Articles/Display-Images-from-SQL-Server-Database-using-ASP.Net.aspx
http://stackoverflow.com/questions/6987433/display-image-from-database-in-asp-net-with-c-sharp

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Webmasters964, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 9/21/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Welcome.aspx.cs" Inherits="WebApplication5.Welcome" %>


<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lb1" runat="server" Text="Label"></asp:Label>
<asp:Image ID="Image1" runat="server" />
</div>
</form>
</body>
</html>


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 WebApplication5
{
public partial class Welcome : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlDataAdapter sqlda;
DataTable dt = new DataTable();

protected void Page_Load(object sender, EventArgs e)
{
lb1.Text = "<b><font color=Brown>" + "WELLCOME :: " + "</font>" + "<b><font color=red>" + Session["UserName"] + "</font>";
if (!IsPostBack)
{
bindgrid();
}
}
private void bindgrid()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
//make sure your image path stored like this "~/images/abc.jpg"
SqlCommand sqlcmd = new SqlCommand("select image from employee where UserName='" + Session["UserName"] + "'", con); //make sure your image path stored like this "~/images/Sunset.jpg"
sqlda = new SqlDataAdapter(sqlcmd);
dt.Clear();
sqlda.Fill(dt);
if (dt.Rows.Count > 0)
{
Image1.ImageUrl = dt.Rows[0][0].ToString();
}
con.Close();
}
}
}


If this post helps you mark it as answer
Thanks

Webmasters964, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response