I have a DB with 3 tables one of which has uploaded images from users with a profile page each.I had managed to show the pictures with gridview in login and search but it wasn't very pleasing to the eye so i decided to follow the asp:image method :)
What i can't do now tho, is show the image referred to the user loging in or the user's profile that's been shown after the search.
So if i find a way to pass the id to the handler? or...?
Thanks for any help!
Default.aspx.cs (stuff happens at Imageupload() and ImagebindGrid() i have bypassed the gridview code so i can try to show it with the other way.
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Security.Principal;
using System.Data.Common;
namespace DisplayingImages
{
public partial class Default : System.Web.UI.Page
{
public string query, constr, query1, query2, query3;
public SqlConnection con, conn;
public void connection()
{
constr = ConfigurationManager.ConnectionStrings["Myconnection"].ToString();
con = new SqlConnection(constr);
con.Open();
}
protected void Page_Load(object sender, EventArgs e)
{
Label1.Visible = false;
if (!IsPostBack)
{
EM = (string)(Session["Email_Account"]);
lblemail.Text = EM;
SN = Convert.ToString(Session["Surname"]);
lblname.Text = SN;
PS = (string)(Session["Password"]);
PID = (int)(Session["id"]);
HttpContext context = HttpContext.Current;
context.Session["Email_Account"] = EM;
EM = (string)(context.Session["Email_Account"]);
if (PID == null)
{
Response.Redirect("Login.aspx");
}
imagebindGrid();
PostSelection();
}
}
private void InitializeComponent()
{
throw new NotImplementedException();
}
protected void upload(object sender, EventArgs e) // button για το upload της φωτο. Καλειται η κλαση Imageupload()
{
Imageupload();
}
/* Κλασση για upload εικονας . Ελεγχος και περασμα εικονας */
private void Imageupload()
{
if (FileUpload1.HasFile)
{
PID = (int)(Session["id"]);
if (PID != null)
{
int imagefilelenth = FileUpload1.PostedFile.ContentLength;
byte[] imgarray = new byte[imagefilelenth];
HttpPostedFile image = FileUpload1.PostedFile;
image.InputStream.Read(imgarray, 0, imagefilelenth);
connection();
query = "Insert into ImageToDB (user_id,ImageName,Image) values (@user_id,@Name,@Image)";
SqlCommand com = new SqlCommand(query, con);
com.Parameters.AddWithValue("@Name", SqlDbType.VarChar).Value = TextBox1.Text;
com.Parameters.AddWithValue("@Image", SqlDbType.Image).Value = imgarray;
com.Parameters.AddWithValue("@user_id", PID);
com.ExecuteNonQuery();
Label1.Visible = true;
Label1.Text = "Image Is Uploaded successfully";
imagebindGrid();
}
}
}
/* Gridview για εικονες */
public void imagebindGrid()
{
connection();
query = "Select id,ImageName,Image from ImageToDB where user_id= " + PID;
SqlCommand com = new SqlCommand(query, con);
SqlDataReader dr = com.ExecuteReader();
//Image.ImageUrl = "/Handler.ashx?id_Image=" + PID.ToString();
//Gridview1.DataSource = dr;
//Gridview1.DataBind();
}
/*Logout Button */
protected void Button1_Click(object sender, EventArgs e)
{
System.Web.Security.FormsAuthentication.SignOut();
Session.Clear();
Session.RemoveAll();
Session.Abandon();
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate");
HttpContext.Current.Response.AddHeader("Pragma", "no-cache");
HttpContext.Current.Response.AddHeader("Expires", "0");
FormsAuthentication.SignOut();
HttpContext.Current.User = new GenericPrincipal(new GenericIdentity(string.Empty), null);
Response.Redirect("~/Logout.aspx");
}
/* Κλασση για το Post */
private void Txt()
{
try
{
PID = (int)(Session["id"]);
if (PID != null)
{
connection();
query1 = "Insert into Posttext (user_id,Posttext) values (@user_id,@Your_Post)";
SqlCommand com2 = new SqlCommand(query1, con);
com2.Parameters.AddWithValue("@Your_Post", SqlDbType.VarChar).Value = PostBox.Text;
com2.Parameters.AddWithValue("@user_id", PID);
com2.ExecuteNonQuery();
lbluser.Text = PostBox.Text;
PostSelection();
}
}
catch (Exception ex)
{
//con.Close();
}
}
/* Κανει select τα κειμενα και τα ανεβαζει απο την βαση στο grid */
public void PostSelection()
{
connection();
query2 = "Select Posttext from Posttext where user_id= " + PID;
SqlCommand com1 = new SqlCommand(query2, con);
SqlDataReader Read = com1.ExecuteReader();
grdemployee7.DataSource = Read;
grdemployee7.DataBind();
Read.Close();
}
/* Καλειται η Txt() για να γινει το Post */
protected void Button2_Click(object sender, EventArgs e)
{
Txt();
}
public string USER_PID { get; set; }
public DateTime _myid { get; set; }
/* --------------------Κουμπι για search PROFILE -----------------------------------*/
public void Button3_Click1(object sender, EventArgs e)
{
Response.Redirect("~/WebForm7.aspx");
}
public string SN { get; set; }
public string PS { get; set; }
public string EM { get; set; }
public int PID { get; set; }
protected void PostBox_TextChanged(object sender, EventArgs e)
{
}
}
}
here is my handler
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace DisplayingImages
{
/// <summary>
/// Summary description for Handler1
/// this application is created by vithal wadje for C# corner
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Handler1 : IHttpHandler
{
//createting the object of Default.aspx class page to
//call connection and use strings variable
Default cls = new Default();
public void ProcessRequest(HttpContext context)
{
//storing the querystring value that comes from Defaul.aspx page
string displayimgid = context.Request.QueryString["id_Image"].ToString();
cls.connection();
//retriving the images on the basis of id of uploaded
//images,by using the querysting valaues which comes from Defaut.aspx page
cls.query = "select Image from ImageToDB where id=" + displayimgid;
SqlCommand com = new SqlCommand(cls.query, cls.con);
SqlDataReader dr = com.ExecuteReader();
dr.Read();
context.Response.BinaryWrite((Byte[])dr[0]);
context.Response.End();
}
public bool IsReusable
{
get
{
return true;
}
}
}
}
and here is the way i try to pass the id
<asp:Image ID="Image" runat="server" ImageUrl='<%# "Handler1.ashx?id_Image="+ Eval("id") %>' "/>
The code might seem a bit messy, but im trying some things without any success.
Thanks again!!