C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




How to show images in GridView


Posted Date: 21 May 2008    Resource Type: Code Snippets    Category: ASP.NET GridView
Author: vijethaMember Level: Gold    
Rating: Points: 10




<asp:datagrid id="GridView1" CellPadding="3" Width="304" Runat="server"
BackColor="#FFFFFF" forecolor="#000000"
ShowHeader="True" AutoGenerateColumns="False"
BorderColor="black" HeaderStyle-Font-Bold="true"
HeaderStyle-BackColor="#dcdcdc"
HeaderStyle-ForeColor="#00015E" Font-Size="8pt" Font-Name="arial"
BorderStyle="None" GridLines="None">
<Columns>
<asp:BoundColumn HeaderText="ID" DataField="img_pk" />
<asp:TemplateColumn>
<ItemTemplate>
<asp:Image ID="Image1" Width="100" Height="100" runat="server" ImageUrl='<%#FormatURL(DataBinder.Eval(Container.DataItem, "img_pk")) %>'></asp:Image>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="Name" DataField="img_name"></asp:BoundColumn>
<asp:BoundColumn HeaderText="ContentType" DataField ="img_contenttype"/>
</Columns>
</asp:datagrid>

Add the following code in DatagridWithImages.aspx.cs file
---------------------------------------------------------

protected void Page_Load(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("server=192.168.1.16\\SQLEXPRESS;uid=sa;pwd=Mobile123;database=employs");
SqlDataAdapter da = new SqlDataAdapter("select * from image",cn);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}

public string FormatURL(object strArgument)
{
return ("Image.aspx?id="+strArgument);

}


Add Image.aspx page in website and add the folowing code
-------------------------------------------------------------

protected void Page_Load(object sender, EventArgs e)
{
string strImageID= Request.QueryString[0];
SqlConnection cn = new SqlConnection("server=192.168.1.16\\SQLEXPRESS;uid=sa;pwd=Mobile123;database=employs");
SqlCommand cmd = new SqlCommand("select img_contenttype,img_data from image where img_pk='" + strImageID + "' ", cn);
try
{
cn.Open();
SqlDataReader dr;
dr = cmd.ExecuteReader();
if (dr.Read()) //yup we found our image
{
//retrving the image content type
Response.ContentType = dr["img_contenttype"].ToString();
//displaying the image
Response.BinaryWrite((byte[])dr["img_data"]);
}
cn.Close();
}
catch
{

}
}






Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to Export the gridview results in an excel sheet.
Previous Resource: Uploading and Downloading files
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET GridView


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use