0 Replies - 672 Views - Last Post: 30 August 2011 - 08:29 PM Rate Topic: -----

#1 hamsterpenguin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-August 11

image call out from database but failed

Posted 30 August 2011 - 08:29 PM

i have difficulty of image appear in the web page after i have upload the photo. the image display a cross but the albumName is out.

database
name datatype
pid int
pict image
albumName nvarchar(50)

my upload page

<%@ Page Title="" Language="C#" MasterPageFile="~/admin.master" AutoEventWireup="true" CodeFile="tadmin_uploadphoto.aspx.cs" Inherits="admin_uploadphoto" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

<style type="text/css">

.style4

{

width: 56px; 

}

.style6


{

width: 101px; 

height: 21px; 

}

</style> 

</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">





<table class="style1" style="height: 217px; width: 688%">

<tr>

<td class="style6">

&nbsp;</td>

<td>

&nbsp;</td>

</tr>

<tr>

<td class="style6">

File</td>

<td>

<asp:FileUpload ID="FileUpload1" runat="server" />

</td>

</tr>

<tr>

<td class="style6">

album Name</td>

<td>

<asp:TextBox ID="txt_albumname" runat="server"></asp:TextBox>

</td>

</tr>

<tr>

<td class="style6">

&nbsp;</td>

<td>

<asp:Button ID="btn_upload" runat="server" Text="upload" 

onclick="btn_upload_Click" />


<asp:Button ID="btn_load" runat="server" Text="Button" 

onclick="btn_load_Click" />


</td>

</tr>

<tr>

<td class="style6">

&nbsp;</td>

<td>

<asp:Label ID="lbl_msg" runat="server"></asp:Label>

</td>

</tr>

<tr>

<td class="style6">

&nbsp;</td>

<td>

&nbsp;</td>

</tr>

</table> 

</asp:Content>


 

[u]codes behind[/u]


using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

using System.IO; 

 


public partial class admin_uploadphoto : System.Web.UI.Page

{

SqlConnection conn = new SqlConnection("Data Source=EN12-2-24-WS07\\SQLEXPRESS; Initial Catalog=SchWeb; Integrated Security=SSPI"); 

protected void Page_Load(object sender, EventArgs e) 

{

}

protected void btn_load_Click(object sender, EventArgs e) 

{

 

 

 

 

}

protected void btn_upload_Click(object sender, EventArgs e) 

{

string stralbumName = txt_albumname.Text.ToString(); 

if (FileUpload1.PostedFile != null && 

FileUpload1.PostedFile.FileName != "") 

{

string strExtension = System.IO.Path.GetExtension(FileUpload1.FileName); 

if ((strExtension.ToUpper() == ".JPG") | (strExtension.ToUpper() == ".GIF")) 

{

System.Drawing.Image imageToBeResized = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream); 

int imageHeight = imageToBeResized.Height; 

int imageWidth = imageToBeResized.Width; 

int maxHeight = 150; 

int maxWidth = 150; 

imageHeight = (imageHeight * maxWidth) / imageWidth;

imageWidth = maxWidth;

if (imageHeight > maxHeight) 

{

imageWidth = (imageWidth * maxHeight) / imageHeight;

imageHeight = maxHeight;

}

Bitmap bitmap = new Bitmap(imageToBeResized, imageWidth, imageHeight); 

System.IO.MemoryStream stream = new MemoryStream(); 

bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); 

stream.Position = 0;

byte[] imageSize = new byte[stream.Length + 1]; 

stream.Read(imageSize, 0, imageSize.Length);

 

SqlCommand cmd = new SqlCommand(); 

cmd.CommandText = "INSERT INTO tb_photogallery(pict, albumName)" + 

" VALUES (@pict,@albumName)"; 

cmd.CommandType = CommandType.Text; 

cmd.Connection = conn;

 

SqlParameter UploadedImage = new SqlParameter
























("@pict", SqlDbType.Image, imageSize.Length); 

UploadedImage.Value = imageSize;

cmd.Parameters.Add(UploadedImage);

SqlParameter albumName = new SqlParameter


("@albumName", SqlDbType.VarChar, 50); 

albumName.Value = stralbumName.ToString();

cmd.Parameters.Add(albumName);

 

conn.Open();

int result = cmd.ExecuteNonQuery(); 

conn.Close();

}

lbl_msg.Text = "Successfully added"; 

 

 

}

}

}


my display photo page



<%@ Page Title="" Language="C#" MasterPageFile="~/student.master" AutoEventWireup="true" CodeFile="galleryyyy.aspx.cs" Inherits="galleryyyy" %> 


<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> 


<style type="text/css"> 


.style4 


{

width: 100%; 

}

</style> 

</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> 


<table class="style4"> 


<tr> 


<td> 


&nbsp;</td> 


</tr> 


<tr> 


<td> 


<asp:DataList ID="DataList1" runat="server" RepeatColumns="2" 

RepeatDirection="Horizontal"> 


<ItemTemplate> 


<asp:Image ID="Image2" runat="server" ImageUrl='<%# "photo.ashx?id=" + Eval("pid")%>'/> 


<br /> 


<asp:Label ID="lbl_photoname" runat="server" Text='<%# Eval("albumName") %>'></asp:Label> 


</ItemTemplate> 


</asp:DataList> 


</td> 


</tr> 


<tr> 


<td> 


<asp:Label ID="lbl_name" runat="server"></asp:Label> 


</td> 


</tr> 


<tr> 


<td> 


&nbsp;</td> 


</tr> 


</table> 

</asp:Content> 


 






































































codes behind


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;

public partial class galleryyyy : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e) 

{

//lbl_username.Text = "" + Session["userName"];


 

 

// String id = Request.QueryString["a"];

// String title = Request.QueryString["b"];

 

 

if (Page.IsPostBack == false) 

{

SqlConnection connShow = new SqlConnection("Data Source=EN12-2-24-WS07\\SQLEXPRESS; Initial Catalog=SchWeb; Integrated Security=SSPI"); 

connShow.Open();

SqlCommand cmdProfile = new SqlCommand("SELECT * FROM tb_photogallery", connShow); 

SqlDataReader dr; 

dr = cmdProfile.ExecuteReader();

if (dr.Read()) 

{

 

lbl_name.Text = dr["albumName"].ToString(); 

 

 

 

 

}

dr.Close();

connShow.Close();

}

if (!IsPostBack) 

{

BindGrid();

}

}

void BindGrid() 

{

//String id = Request.QueryString["a"];









SqlConnection conn = new SqlConnection("Data Source=EN12-2-24-WS07\\SQLEXPRESS; Initial Catalog=SchWeb; Integrated Security=SSPI"); 

 

SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tb_photogallery", conn); 

DataSet ds = new DataSet(); 

//loading the records into dataset




da.Fill(ds);

DataList1.DataSource = ds.Tables[0];

DataList1.DataBind();

}

}



handler


<%@ WebHandler Language="C#" Class="photo" %>

using System;

using System.Web;

using System.Data.SqlClient;

using System.Configuration; 

 


public class photo : IHttpHandler { 

public void ProcessRequest(HttpContext context) 

{

 

SqlConnection con = new SqlConnection("Data Source=EN12-2-24-WS02\\SQLEXPRESS; Initial Catalog=GolfBS; Integrated Security=SSPI; MultipleActiveResultSets=true;"); 

SqlCommand cmd = new SqlCommand(); 

cmd.CommandText = "Select albumName,pict from tb_photogallery where pid =@pid"; 

cmd.CommandType = System.Data.CommandType.Text; 

cmd.Connection = con;

SqlParameter ImageID = new SqlParameter("@pid", System.Data.SqlDbType.Int); 

ImageID.Value = context.Request.QueryString["pid"]; 

cmd.Parameters.Add(ImageID);

con.Open();

SqlDataReader dReader = cmd.ExecuteReader(); 

dReader.Read();

context.Response.BinaryWrite((byte[])dReader["pict"]); 

dReader.Close();

con.Close();

 

}

 

public bool IsReusable { 

get { 

return false; 

}

}

}



 

This post has been edited by stayscrisp: 31 August 2011 - 01:08 AM
Reason for edit:: Use code tags!!


Is This A Good Question/Topic? 0
  • +

Page 1 of 1