.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

how to "Image Not Found" in gridview/datalist.

Posted By: kavitha     Posted Date: November 14, 2010    Points:2   Category :ASP.Net
hi friends,

how to "Image Not Found" in gridview/datalist.

if i not uploaded my image gridview / datalist that display Defaultimage(like noimage).

how to create it,please give in detail.

thanks in advance.

Responses
Author: Manoranjan Sahoo             
Posted Date: November 14, 2010     Points: 5   

hi,

just try below :
<asp:DataList ID="DataList1" runat="server" RepeatColumns="2" RepeatDirection="Horizontal">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='Handler1.ashx?id=<%#Eval("UserId")%>' Height="100px" Width="150px" /><br />
<asp:Label ID="Label1" runat="server" Text='<%#Eval("UserName") %>'></asp:Label>
</ItemTemplate>
</asp:DataList>

Now add noimage.gif image in your main directory's images folder.
Then create one asp.net hanlder and paste the below code :

public class Handler : IHttpHandler {

public void ProcessRequest (HttpContext context)
{
try
{

SqlConnection myConnection = new SqlConnection(ConfigurationManager.AppSettings["sqlconnstr"].ToString());
if (myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
string sql = "Select ImageData from Images where UserId='" + context.Request.QueryString["id"] + "'";
SqlCommand cmd = new SqlCommand(sql, myConnection);
cmd.Prepare();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (dr["ImageData"] != null && dr["ImageData"].ToString() != "" && (byte[])(dr["ImageData"]) != null)
{
context.Response.BinaryWrite((byte[])dr["ImageData"]);
}
else
{
byte[] myimage = File.ReadAllBytes(context.Server.MapPath("~") + "/images/noimage.gif");
context.Response.BinaryWrite(myimage);
}
}
if (!dr.HasRows)
{

byte[] myimage=File.ReadAllBytes(context.Server.MapPath("~") + "/images/noimage.gif");
context.Response.BinaryWrite(myimage);
}

dr.Close();
if (myConnection.State == ConnectionState.Open)
{
myConnection.Close();
}
}
catch { }

}

public bool IsReusable {
get {
return false;
}
}

}


Join My Site : http://dotnetsquare.com/
Author: kavitha             
Posted Date: November 15, 2010     Points: 5   

thanks for your response,

but its nor working,doesn't show image.

please correctit.

thanks.



Author: Manoranjan Sahoo             
Posted Date: November 15, 2010     Points: 5   

This code is working for me.
i think you have some mistake. can you post your code?

Join My Site : http://dotnetsquare.com/


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend