.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 >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Display image in webpage when we enter the name of the image

Posted By:satyapriyanayak       Posted Date: November 29, 2013    Points: 40    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

In this code Snippets we will know how to display image in webpage when we enter the name of the image.
 

Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Image_name._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="lb1" runat="server" Font-Bold="True" ForeColor="#FF3300"></asp:Label><br />
    <asp:TextBox ID="TextBox_image_name" runat="server"></asp:TextBox><br />
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
        Text="Show image" />
    </div>
    
    </form>
</body>
</html>

Default.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Image_name
{
    public partial class _Default : System.Web.UI.Page
    {
        string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        string str;
        SqlCommand com;

        protected void Button1_Click(object sender, EventArgs e)
        {
            object obj = null;
            SqlConnection con = new SqlConnection(strConnString);
            con.Open();
            str = "select count(*) from imagetest where imagename=@imagename";
            com = new SqlCommand(str, con);
            com.CommandType = CommandType.Text;
            Session["imagename"] = TextBox_image_name.Text;
            com.Parameters.AddWithValue("@imagename", Session["imagename"]);
            obj = com.ExecuteScalar();
            if ((int)(obj) != 0)
            {
                Response.Redirect("image.aspx");
            }
            else
            {
                lb1.Text = "Invalid imagename";
                clear();
            }
            con.Close();
        }
        private void clear()
        {
            TextBox_image_name.Text = "";
            
        }
    }
}

image.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="image.aspx.cs" Inherits="Image_name.image" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    BackColor="#CC3300" ForeColor="Black" ShowHeader="False" >
    <Columns>
   <asp:TemplateField>
    <ItemTemplate>
    <img alt ="" src ='images/<%#Eval("image") %>' height="50px" width="50px"/>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
    </div>
    </form>
</body>
</html>

image.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Image_name
{
    public partial class image : System.Web.UI.Page
    {
        string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        string str;
        SqlDataAdapter sqlda;
        DataTable dt;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindgrid();
            }
        }
        private void bindgrid()
        {
            string imagename = (string)Session["imagename"];
            SqlConnection con = new SqlConnection(strConnString);
            con.Open();
            str = "select image from imagetest where imagename='" + imagename + "'";
            sqlda = new SqlDataAdapter(str, con);
            dt = new DataTable();
            sqlda.Fill(dt);
            sqlda.Dispose();
            GridView1.DataSource = dt;
            GridView1.DataBind();
            con.Close();
        }
    }
}



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

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