Click here to Skip to main content
15,900,108 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi, I'm a new user of asp.net and i write code to show news is like this
default.aspx
XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
                    BorderWidth="0" BorderColor="White" ShowHeader="False">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                 <asp:HyperLink runat="server" ID="hl" NavigateUrl='<%#"NewsDetails.aspx?Newsid="+ Eval("news_id") %>' Text='<%# Eval("news_title") %>'></asp:HyperLink>

                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

default.aspx.cs
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            getNewsDetails();
        }
    }
    protected void getNewsDetails()
    {
        try
        {
            SqlCommand cmd1 = new SqlCommand("select top 5 news_id,news_title,news_image from tbl_news where news_status=1 order by news_id desc", conn);
            conn.Open();
            SqlDataReader rdr = cmd1.ExecuteReader();

            GridView1.DataSource = rdr;
            GridView1.DataBind();

            rdr.Close();
        }
        catch (Exception ee)
        {
            Response.Write(ee.Message);
        }
        finally
        {
            conn.Close();
        }
    }

its shows properly but i need to show a new image which i store in database and myfield name is news_image. please tell me how to show image of new .png within news, or tell me different method to show newsbulletin in asp.net.

Thanks
Regards
Umesh Daiya
Posted
Updated 29-Sep-12 18:56pm
v2

1 solution

You need to create a handler to show the image from database.
Here is a ode snippet,

In aspx page
<itemtemplate>
<img id="imgTest" src="showimage.ashx?newsid=<%# Eval(" news_id=") %>">
             width="100px" height="100px" title="<%# Eval("news_title") %>" />
</img></itemtemplate>


Creating the Handler (showimage.ashx)
public void ProcessRequest(HttpContext context)
{
    //establish the data connection
    using (SqlConnection connection = new                 SqlConnection(ConfigurationManager.ConnectionStrings["CONN_STRING"].ConnectionString))
    {
        //get the image through newsid passed as querystring
        SqlCommand command = new SqlCommand("Select news_image from tbl_news where newis_id = '" + context.Request.QueryString["newsid"] + "'", connection);
 
        connection.Open();
        SqlDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection);
        while (dr.Read())
        {
            if (dr["news_image"].ToString().Length > 0)
            {
                context.Response.BinaryWrite((byte[])dr["newsimage"]);
            }
        }
    }
}


This will show the image in gridvuew.

Hope this helps.
cheers
 
Share this answer
 
v2
Comments
UDTWS 30-Sep-12 5:23am    
Thanks For Reply: but plz check this
showimage.ashx

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

using System;
using System.Web;
using System.Data.SqlClient;

public class showimage : IHttpHandler {

public void ProcessRequest(HttpContext context)
{
//establish the data connection
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["CONN_STRING"].ConnectionString))
{
//get the image through newsid passed as querystring
SqlCommand command = new SqlCommand("Select news_image from tbl_news where news_id = '" + context.Request.QueryString["newsid"] + "'", connection);

connection.Open();
SqlDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection);
while (dr.Read())
{
if (dr["news_image"].ToString().Length > 0)
{
context.Response.BinaryWrite((byte[])dr["news_image"]);
}
}
}
}
}
default.aspx
<pre lang="xml"><asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
BorderWidth="0" BorderColor="White" ShowHeader="False" Width="100%">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink runat="server" ID="hl" NavigateUrl='<%#"NewsDetails.aspx?Newsid="+ Eval("news_id") %>' Text='<%# Eval("news_title") %>'></asp:HyperLink>
<img id="imgTest" src="showimage.ashx?newsid=<%# Eval("news_id=") %>" width="100px" height="100px" title="<%# Eval("news_title") %>" />
</img>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView></pre>

i do this as you suggested is it right. but i have got a error message "Error 26 showimage does not implement interface member System.Web.IHttpHandler.IsReusable.
"
now what i should do to correct this i use first time this handler i did not know about it more.
Sandip.Nascar 30-Sep-12 5:39am    
Add this code,

public class showimage : IHttpHandler {
{
public bool IsReusable
{
get { return false; }
}
//add your code
}

This will work. For more details, google httphandler in asp.net.
cheers

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900