Saturday, 14 April 2012

image upload and retrive in gridview

//.aspx file//

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="upload" %>

<!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>
        <table>
            <tr>
                <td style="width: 100px">
                    images</td>
                <td style="width: 100px">
                    <asp:FileUpload ID="FileUpload1" runat="server" /></td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging">
                        <Columns>
                            <asp:BoundField DataField="id" HeaderText="id" />
                            <asp:TemplateField>
                           <HeaderTemplate>
                               <asp:Label ID="Label1" runat="server" Text="Image"></asp:Label>
                           </HeaderTemplate>
                           <ItemTemplate >
                           <asp:Image ID ="img1" runat ="server"   ImageUrl='<%# Eval("img","~/Upload/{0}") %>' Width="50px"  Height ="50px"/>
                           </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2">
                    <asp:Button ID="btn_submit" runat="server" OnClick="btn_submit_Click" Text="submit" /></td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>
//.aspx.cs file//


using System;
using System.Collections;
using System.Configuration;
using System.Data;
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.Data.SqlClient;
using System.Drawing;
using System.IO;
public partial class upload : System.Web.UI.Page
{
    SqlConnection cn = new SqlConnection(ConfigurationManager .ConnectionStrings [1].ConnectionString );
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();
        }
    }
    protected void btn_submit_Click(object sender, EventArgs e)
    {
        cn.Open();
        //SqlCommand cmd = new SqlCommand("insert into image_tbl values('" + FileUpload1.FileName + "')", cn);

        //cmd.ExecuteNonQuery();


        string filePath = MapPath("~/UPLOAD") + "/";
        
        string fname = FileUpload1.FileName;


      string extention = Path.GetExtension (fname).ToLower();
            

        if (FileUpload1.HasFile)
        {
            if (extention == ".jpg" || extention == ".png" || extention == ".bmp" || extention ==".gif ")
            {
                SqlCommand cmd = new SqlCommand("insert into image_tbl values('" + FileUpload1.FileName + "')", cn);

                cmd.ExecuteNonQuery();
                //FileUpload1.SaveAs(Path + fname);


                FileUpload1.SaveAs(Server.MapPath("~/UPLOAD"+ "/") + fname);

                Response.Write("<script>alert('file is uploaded successfully')</script>");
                cn.Close();
                bind();
            }
            else
            { Response.Write("<script>alert('this format not valide')</script>"); }
        }
        //cn.Close();
        //bind();

    }
    public void bind()
    {

        cn.Open();
        SqlCommand cmd = new SqlCommand("select * from image_tbl",cn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);

        GridView1.DataSource = ds;

        GridView1.DataBind();
    
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        bind();
    }
}

No comments:

Post a Comment