Saturday, 1 March 2014

Save Image in DB and Show in Gridview in Asp.Net

Ans:-

Default.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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>Save Images folder and images path in DB and show Images to front end..</title>
    <script type="text/javascript" language="javascript">
        function validate() {
            var uploadcntrl = document.getElementById('<%=FileUploadImages.ClientID%>').value;
            var reg = /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.jpg|.JPG|.jpeg|.JPEG|.gif|.GIF| .png|.PNG)$/;
           
           
            if(uploadcntrl.length>0)
            {
                if (reg.test(uploadcntrl)) {
                    return true;
                }
                else {
                    alert("Only .jpg|.jpeg.gif|.png| files are allowed!");
                    return false;
                }
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <center>
    Upload Images:
        <asp:FileUpload ID="FileUploadImages" runat="server" />
        <br />
        <asp:Button ID="btnButton" runat="server" Text="Submit"
            onclick="btnButton_Click" OnClientClick="return validate();" />
        <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="id" HeaderText="id" />
                <asp:BoundField DataField="imgname" HeaderText="imgname" />
               
                <asp:ImageField DataImageUrlField="imgpath" ControlStyle-Width="100px" ControlStyle-Height="100px" HeaderText="imgpath">
                </asp:ImageField>
            </Columns>
        </asp:GridView>
   
    </center>
    </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.IO;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection cn;
    SqlDataAdapter da;
    SqlCommand cmd;
    DataTable dt;
    Class1 obj = new Class1();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
    void BindData()
    {
        cn = new SqlConnection(obj.GetConnection());
        cn.Open();
        cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "GetImgData";
        cmd.Connection = cn;
        da = new SqlDataAdapter(cmd);
        dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }


    protected void btnButton_Click(object sender, EventArgs e)
    {
        string filename = Path.GetFileName(FileUploadImages.PostedFile.FileName);
        FileUploadImages.SaveAs(Server.MapPath("Images/" + filename));
        cn = new SqlConnection(obj.GetConnection());
        cn.Open();
        cmd = new SqlCommand("insert into ImagePath(imgname,imgpath)values(@imgname,@imgpath)", cn);
        cmd.Parameters.AddWithValue("@imgname", filename);
        cmd.Parameters.AddWithValue("@imgpath", "Images/" + filename);
        cmd.ExecuteNonQuery();
        BindData();
        cn.Close();
    }
    
}

Get Connection Through Class File:-

using System;
using System.Data;
using System.Configuration;
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;

/// <summary>
/// Summary description for Class1
/// </summary>
public class Class1
{
public Class1()
{
}
    public string GetConnection()
    {
        string strCon = "server=localhost;database=subsdb;user id=sa;pwd=123";
        return strCon;
    }
}

Create Table & Stored Procedure:-

use subsdb

create table ImagePath(id int identity(1,1),imgname varchar(50),imgpath nvarchar(max))

create proc GetImgData
as
begin
select * from ImagePath;
end

No comments:

Post a Comment