Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
i am trying to bind the images to datalist and the problem i am facing is that either its not getting binded or handler file is not not getting executed. tell me simple solution for this or please give me simple link for reference .i know its very easy but don't know what i am doing wrong
ASP.NET
<asp:DataList ID="DataList1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" 
BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" Visible="False" 

OnItemDataBound="DataList1_ItemDataBound"
 
Font-Names="Times New Roman" Font-Size="Larger" Font-Bold="False" GridLines="Both" RepeatColumns="1"
RepeatDirection="Horizontal" Width="960px">

<HeaderStyle BackColor="#A55129" Font-Size="Large" ForeColor="White"
HorizontalAlign="Center" VerticalAlign="Middle" />


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


 
QuestionText:
 
<asp:Label ID="QuestionTextLabel" runat="server" 
Text='<%# Eval("QuestionText") %>'> 


QuestionType: <asp:Label ID="QuestionTypeLabel" 
Text='<%# Eval("QuestionType") %>' runat="server" >


Option1:
<asp:Label ID="Option1Label" runat="server" Text='<%# Eval("Option1") %>'> 


Option2: <asp:Label ID="Option2Label" Text='<%# Eval("Option2") %>' 
runat="server">


Option3:
<asp:Label ID="Option3Label" runat="server" Text='<%# Eval("Option3") %>'> 


Option4: <asp:Label ID="Option4Label" Text='<%# Eval("Option4") %>' 
runat="server">


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


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


 
<asp:Image ID="imgemp" runat="server" ImageUrl=' <%#DataBinder.Eval
(Container.DataItem, "images") %>' />
<%-- QuestionImage:
<asp:Image ID="imgEmp" runat="server" Width="100px" Height="120px" 
style="padding-left:40px"/>



<%-- <img src="GridViewImage123.ashx?autoid=<%# Eval("Id").ToString() %>&tableName=<%# Eval("TableName").ToString() %>"--%>




<SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />




C#
string connString =
ConfigurationManager.ConnectionStrings["QuestionExam"].ConnectionString;
DataTable dsProducts = new DataTable();
//Open SQL Connection
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
//Initialize command object
using (SqlCommand cmd = new SqlCommand("QuestionPaper1", conn))
{
string session = (string)Session["SubjectName"];
cmd.CommandType = CommandType.StoredProcedure;
//add input parameters to the SP
cmd.Parameters.AddWithValue("@Subjectname", session);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
//Fill the result set
adapter.Fill(dsProducts);
 
}
}
DataColumn imageCol = new DataColumn("images", typeof(string));
dsProducts.Columns.Add(imageCol);
 
if (dsProducts.Rows.Count > 0)
{
for (int i = 0; i < dsProducts.Rows.Count; i++)
{
try
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView dr = (DataRowView)e.Item.DataItem;
Image ImageData = (Image)e.Item.FindControl("imgemp");
 
ImageData.ImageUrl = Page.ResolveUrl("~/ImgHandler.ashx?QueNo=" + dsProducts.Rows[i]["QueNo"] + "");
ImageData.Visible = true;
}
}
 
catch (Exception)
{ }
}
}
}



in handler


C#
public void ProcessRequest (HttpContext context) {
System.Data.SqlClient.SqlDataReader rdr = null;
System.Data.SqlClient.SqlConnection conn = null;
System.Data.SqlClient.SqlCommand selcmd = null;
try
{
string sess = (string)context.Session["SubjectName"].ToString();
string section = (string)context.Session["SectionName"];
string id = HttpContext.Current.Request.QueryString["QueNo"];
conn = new System.Data.SqlClient.SqlConnection
(System.Configuration.ConfigurationManager.ConnectionStrings
["QuestionExam"]"ConnectionString);
selcmd = new System.Data.SqlClient.SqlCommand
//("select * from Upimage where ContestName = '"+HttpContext.Current.Request.Cookies["Contest"].Value+"'" 
//, conn);
("select * from V_QuestionPaper where SubjectName = '" + HttpContext.Current.Session["SubjectName"] + "' and QueNo=" + id + ""
, conn);
conn.Open();
rdr = selcmd.ExecuteReader();
while (rdr.Read())
{
context.Response.ContentType = "image/jpg";
context.Response.BinaryWrite((byte[])rdr["QuestionImage"]);
System.IO.MemoryStream stream = new System.IO.MemoryStream((byte[])rdr["QuestionImage"], true);
stream.Write((byte[])rdr["QuestionImage"], 0, 100);
 
// Create a bitmap from the stream
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(stream);
 
System.Drawing.Size new_size = new System.Drawing.Size();
int outputSize = 100;
//resize based on the longer dimension
if (bmp.Width > bmp.Height)
{
 
new_size.Width = outputSize;
new_size.Height = (int)(((double)outputSize / (double)bmp.Width) * (double)bmp.Height);
}
else
{
new_size.Width = (int)(((double)outputSize / (double)bmp.Height) * (double)bmp.Width);
new_size.Height = outputSize;
}
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(new_size.Width, new_size.Height, bmp.PixelFormat);
System.Drawing.Graphics new_g = System.Drawing.Graphics.FromImage(bitmap);
 
new_g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
new_g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
new_g.DrawImage(bmp, -1, -1, bitmap.Width + 1, bitmap.Height + 1);
 
bmp.Dispose();
 
//Draw the bitmapt to the outputstream
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();
new_g.Dispose();
 
// Close the stream
stream.Close();
 

 

 

}
}
catch (Exception ex)
{ } 

finally
{
if (conn != null)
conn.Close();
}
Posted
Updated 26-Oct-14 21:14pm
v2

1 solution

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