Click here to Skip to main content
15,896,111 members
Articles / Web Development / ASP.NET

Display/Store and Retrieve Image Data from Database to Gridview, and also on Mouse Over

Rate me:
Please Sign up or sign in to vote.
4.44/5 (21 votes)
15 Oct 2011CPOL4 min read 128.3K   9.9K   50  
How to Display/Store and Retrieve Image Data from Database to Gridview, and also on mouse over
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Text;
using System.Data;
using Utils;
//using Algem.WebControls;

/// <summary>
/// Author: Algem Mojedo
/// Date: 9/25/2011
/// </summary>
public partial class DisplayRecord : System.Web.UI.Page
{
    string sqlConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection conn;
    SqlCommand cmd;
    List<User> lstUser = new List<User>();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (SessionManager.PageIndex != string.Empty)
            {
                this.hfKeyId.Value = SessionManager.KeyPrimary;
                this.GridView1.PageIndex = Convert.ToInt32(SessionManager.PageIndex);
            }
            BindGrid();
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("DisplayRecord.aspx");
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindGrid();
    }

    protected void BindGrid()
    {

        List<User> lst = new List<User>();
        try
        {
            using (conn = new SqlConnection(sqlConnection))
            {
                conn.Open();

                StringBuilder sbQry = new StringBuilder();
                sbQry.Append("select ");
                sbQry.Append("UserID,Username,Password,LastName,FirstName,MiddleName,");
                sbQry.Append("WorksiteCode,AccessLevel,Active,DateCreated,DateUpdated,Worksitedesc, ");
                sbQry.Append("Picture,ImageFull FROM dbo.[User]");
                using (cmd = new SqlCommand(sbQry.ToString(), conn))
                {
                    cmd.CommandType = CommandType.Text;

                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            User temp = new User();
                            temp.UserID = Tools.Tools.IifInt(dr["UserID"]);
                            temp.Username = Tools.Tools.IifStr(dr["Username"]);
                            temp.Password = Tools.Tools.IifStr(dr["Password"]);
                            temp.LastName = Tools.Tools.IifStr(dr["LastName"]);
                            temp.FirstName = Tools.Tools.IifStr(dr["FirstName"]);
                            temp.MiddleName = Tools.Tools.IifStr(dr["MiddleName"]);
                            temp.WorksiteCode = Tools.Tools.IifStr(dr["WorksiteCode"]);
                            temp.AccessLevel = Tools.Tools.IifInt(dr["AccessLevel"]);
                            temp.Active = Tools.Tools.IifStr(dr["Active"]);
                            temp.DateCreated = Tools.Tools.IifDT(dr["DateCreated"]);
                            temp.DateUpdated = Tools.Tools.IifDT(dr["DateUpdated"]);
                            temp.WorksiteCode = Tools.Tools.IifStr(dr["Worksitedesc"]);
                            temp.Picture = dr["Picture"];
                            temp.ImageFull = dr["ImageFull"];
                            lst.Add(temp);
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        Session["ListUser"] = lst;
        this.GridView1.DataSource = lst;
        this.GridView1.DataBind();


    }


    protected void gImgBtnDelete_Click(object sender, EventArgs e)
    {
        ImageButton iBtnDelete = sender as ImageButton;
        GridViewRow row = (GridViewRow)iBtnDelete.NamingContainer;
        int idNo = Convert.ToInt32(row.Cells[1].Text);
        bool success = DeleteEntry(idNo);
        if (success)
        {
            SessionManager.KeyPrimary = this.hfKeyId.Value;
            SessionManager.PageIndex = this.hfPageIndex.Value;
            Response.Redirect("DisplayRecord.aspx");
        }
    }



    private bool DeleteEntry(int id)
    {
        bool result = false;

        conn = new SqlConnection(sqlConnection);
        conn.Open();
        SqlTransaction trans = conn.BeginTransaction();
        using (conn)
        {
            try
            {

                string script = string.Empty;

                script = "DELETE FROM dbo.[User] WHERE UserId = " + id.ToString();
                using (cmd = new SqlCommand(script, conn))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Transaction = trans;
                    cmd.ExecuteNonQuery();
                }

                trans.Commit();
                result = true;

            }
            catch (Exception)
            {
                trans.Rollback();
                result = false;
            }
        }
        this.upUserGrid.Update();
        return result;
    }


    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        ucUserAccountUC.DisplayInfo(sender, e);
        mpeUserAccountUC.Show();
    }

    public void CloseModalUserAccountUC()
    {
        mpeUserAccountUC.Hide();
        mpeUserAccountUC.Dispose();
        Response.Redirect("DisplayRecord.aspx");
    }

    public void IvokeAdd()
    {
        mpeUserAccountUC.Show();
        this.upUserGrid.Update();
    }
    public void CloseModalEditUserAcct()
    {
        mpeUserAccountUC.Hide();
        mpeUserAccountUC.Dispose();
        Response.Redirect("DisplayRecord.aspx");
    }

    
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) ***
Philippines Philippines
MCTS - Microsoft Certified Technology Specialist.
An Accountant.
Had been developed Payroll Accounting System Application
Live in: Quezon City, Metro Manila Philippines
Could reached at email address: ag_mojedo@live.com

Comments and Discussions