<asp:GridView ID="grdQuestion" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="Q_No" SortExpression="QuestionID">
<ItemTemplate>
<asp:Label ID="lblQuestionID" runat="server" Text='<%# Bind("QuestionID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Question" SortExpression="Question">
<ItemTemplate>
<img id="Imagebinary" runat="server" src='<%#Bind("Imagebinary")%>' alt="" style="width:50px;height:50px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
DataSet ds;
DataSet objDataSet=new DataSet();
ds=("select Query which will return QuestionID and Imagebinary,In the Sql Srver 2005 table the datatype of QuestionID is string and Imagebinary is varbinary(max))
DataTable objDataTable = new DataTable("GridDetails");
DataRow objDR;
objDataSet.Tables.Add(objDataTable);
objDataTable.Columns.Add("QuestionID", typeof(string));
objDataTable.Columns.Add("Imagebinary", typeof(string));
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
objDR = objDataTable.NewRow();
objDR["QuestionID"] = ds.Tables[0].Rows[i]["QUESTIONID"].ToString();
objDR["Imagebinary"] = Request.Url.ToString().Substring(0, Request.Url.ToString().LastIndexOf("/") + 1) + "QuestionImage.aspx?ImageName=E&QuestionID=" + ds.Tables[0].Rows[i]["QUESTIONID"].ToString();
objDataTable.Rows.Add(objDR);
}
We have a QuestionImage.aspx page where actually I am calling a Storedproc that return Imagebinary against a QuestionID(getting from the query string)
Following is the server side coding of QuestionImage.aspx
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class Admin_QuestionImage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(Request.QueryString["ImageName"].ToString()))
{
byte[] image=null;
if ((Request.QueryString["ImageName"].ToString() != "") && (Request.QueryString["QuestionID"].ToString() != ""))
{
using (QuestionBLL objQuestionBLL = new QuestionBLL())
{
QuestionBO objQuestionBo = new QuestionBO();
objQuestionBo.QUESTIONID = Request.QueryString["QuestionID"].ToString();
objQuestionBo.IMAGEBINARY_EH_INDICATOR = Request.QueryString["ImageName"].ToString();
objQuestionBLL.getQuestionImage(objQuestionBo);
image =(objQuestionBo.ResultDS.Tables[0].Rows[0]["IMAGEBIN"].ToString()!="")?(byte[])objQuestionBo.ResultDS.Tables[0].Rows[0]["IMAGEBIN"]:null;
}
}
if (image != null)
{
Response.ContentType = "image/jpeg";
Response.BinaryWrite(image);
}
}
}
}
Export the gridview data:
If you are trying to export the gridview data to excel,you can probably get a problem i.e the images will be overlapped.So better to export the gridview (that contain images) to the MS Word.
Code for exporting gridview to doc
string fileName = string.Empty;
HtmlForm frm = new HtmlForm();
this.Controls.Add(frm);
{
frm.Controls.Add(grdQuestion);
frm.Controls.Add(tblExcelFirst);
fileName = "StudentAns" + System.DateTime.Now.ToString("ddMMyyyyHHMM");
}
Page.Response.Clear();
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.doc", fileName));
Response.Charset = "";
Response.ContentType = "application/vnd.word";
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
frm.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.Flush();
Response.End();
No comments:
Post a Comment