Saturday, 18 February 2012

To retrive image size from database and show in a gridview

Source Code

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


<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Label ID="Label1" runat="server" Text="Roll"></asp:Label>
        <asp:TextBox ID="txtroll" runat="server" 
            style="position:absolute; top: 8px; left: 70px;"></asp:TextBox>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Name"></asp:Label>
        <asp:TextBox ID="txtname" runat="server" 
            style="position:absolute; top: 30px; left: 70px;"></asp:TextBox>
        <br />
        <asp:Label ID="Label3" runat="server" Text="Image"></asp:Label>
        <asp:FileUpload ID="FileUpload1" runat="server"  
            style="position:absolute; top: 53px; left: 69px;"/>
        <br />
        <asp:Button ID="btnsubmit" runat="server" Text="Submit" 
            style="position:absolute; top: 73px; left: 65px;" onclick="btnsubmit_Click" />
    
    </div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        onselectedindexchanged="GridView1_SelectedIndexChanged">
    <Columns>
    <asp:BoundField  DataField="Roll"/>
    <asp:BoundField  DataField="Name"/>
    <asp:TemplateField>
    <ItemTemplate>
   
   <img id="im1" src='Image/<%#Eval("Imaga")%>' height="80px"/>
    </ItemTemplate>
    </asp:TemplateField>
    
    <asp:BoundField HeaderText="size in Kb" DataField="Size"/>
    
    </Columns>
    </asp:GridView>
    </form>
</body>
</html>
C# Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;

public partial class ImageInsert : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("server=satya-12\\sa;database=sa;uid=lui;pwd=sa@12");
    SqlDataAdapter da;
    SqlCommand com;
    DataSet ds;
    DataTable dt;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
          
        }
        FillGrid();
    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {

        FileInfo fileInfo = new FileInfo(Server.MapPath("Image/" + FileUpload1.FileName + ""));
        double fileSizeKB = fileInfo.Length / 1024;
        double fileSizeMB = fileInfo.Length / (1024 * 1024);


        //FileUpload1.SaveAs(Server.MapPath("Image/" + FileUpload1.FileName + ""));
        string ins = "Insert into Demo2 values('" + txtroll.Text + "','" + txtname.Text + "','" + FileUpload1.FileName + "','"+fileSizeKB+"')";
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        com = new SqlCommand(ins,con);
        com.ExecuteNonQuery();
        //}
    }
    public void FillGrid()
    {
        string sel = "Select * from Demo2";
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        da = new SqlDataAdapter(sel,con);
        dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
database Design

No comments:

Post a Comment