Помогите разобраться в чем причина. Задача состоит в том что бы вывести изображение в GridView Есть таблица в базе данных со следующими колонками Column Name Description Data Type Img_Id Identity column for Image Id int Image_Content Store the Image in Binary Format image Image_Type Store the Image format (i.e. jpeg, gif, png, etc.) varchar Image_Size Store the Image File Size bigint Для воода и вывода изображения пишу следующий код Код | using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.IO;
public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) {
GridView1.DataSource = FetchAllImagesInfo(); GridView1.DataBind();
} } public DataTable FetchAllImagesInfo() { string conStr = "my con string"; string sql = "Select * from ImagesOne"; SqlDataAdapter da = new SqlDataAdapter(sql, conStr); DataTable dt = new DataTable(); da.Fill(dt); return dt; } protected void Button1_Click(object sender, EventArgs e) { GridView1.DataSource = FetchAllImagesInfo(); GridView1.DataBind(); string conStr = "my con string"; if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "") { byte[] myimage = new byte[FileUpload1.PostedFile.ContentLength]; HttpPostedFile Image = FileUpload1.PostedFile; Image.InputStream.Read(myimage, 0, (int)FileUpload1.PostedFile.ContentLength); SqlConnection myConnection = new SqlConnection(conStr); SqlCommand storeimage = new SqlCommand("INSERT INTO ImagesOne " + "(Img_Id, Image_Content, Image_Type, Image_Size) " + " values (@ImageId, @image, @imagetype, @imagesize)", myConnection);
storeimage.Parameters.Add("@ImageId", SqlDbType.Int, 100).Value = Convert.ToInt16(TextBox1.Text); TextBox1.Text = "";
storeimage.Parameters.Add("@image", SqlDbType.Image, myimage.Length).Value = myimage; storeimage.Parameters.Add("@imagetype", SqlDbType.VarChar, 100).Value = FileUpload1.PostedFile.ContentType; storeimage.Parameters.Add("@imagesize", SqlDbType.BigInt, 99999).Value = FileUpload1.PostedFile.ContentLength;
myConnection.Open(); storeimage.ExecuteNonQuery(); myConnection.Close(); } } }
|
хэндлер имеет следующий вид Код | <%@ WebHandler Language="C#" Class="Handler" Debug = "true" %>
using System; using System.Web; using System.Data.SqlClient; using System.Data;
public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) { string conStr = "my con string"; SqlConnection myConnection = new SqlConnection(conStr); myConnection.Open(); string sql = "Select Image_Content from ImagesOne where Img_Id=@ImageId"; SqlCommand cmd = new SqlCommand(sql, myConnection); cmd.Parameters.Add("Img_Id", SqlDbType.Int).Value = context.Request.QueryString["id"]; cmd.Prepare(); SqlDataReader dr = cmd.ExecuteReader(); dr.Read(); context.Response.ContentType = dr["Image_Type"].ToString(); context.Response.BinaryWrite((byte[])dr["Image_Content"]); } public bool IsReusable { get { return false; } }
}
|
и код файла aspx следующий Код | <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Debug="true" %>
<!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> <input id="FileUpload1" type="file" runat= "server"/> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Upload.." /></div> <asp:GridView ID="GridView1" runat="server" Style="z-index: 100; left: 565px; position: absolute; top: 177px" AutoGenerateColumns="False" Width="106px"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:Image ID="Image1" runat="server" ImageUrl='<%# "Handler.ashx?id=" + Eval("Img_Id") %>'/> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:TextBox ID="TextBox1" runat="server" Style="z-index: 101; left: 12px; position: absolute; top: 65px"></asp:TextBox> </form> </body> </html>
|
но выводятся только пустые квадратики если строчку в хэндлере Код | string sql = "Select Image_Content from ImagesOne where Img_Id=@ImageId";
|
заменить на Код |
string sql ="Select Image_Content from ImagesOne";
|
то выводится только первое изображение причем столько раз сколько изображений расположено в таблице. Это сообщение отредактировал(а) IUser - 17.3.2008, 13:15
|