Tuesday 16 July 2013

Import Gridview from Excel using Asp.net with C#

In .aspx page 

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Import Data from Excel</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <b>Please Select Excel File: </b>
        <asp:FileUpload ID="fileuploadExcel" runat="server" />&nbsp;&nbsp;
        <asp:Button ID="btnImport" runat="server" Text="Import Data" OnClick="btnImport_Click" />
        <br />
        <asp:Label ID="lblMessage" runat="server" Visible="False" Font-Bold="True" ForeColor="#009933" Text="Please Wait...."></asp:Label><br />
        <asp:GridView ID="grvExcelData" runat="server" CellPadding="4"
            ForeColor="#333333" GridLines="None">
            <RowStyle BackColor="#EFF3FB" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>

        <br />
        <div align="center">
            <asp:Button ID="btn_Back" runat="server" Text="Back To Home" PostBackUrl="Default.aspx"/>
        </div>
    </div>
    </form>
</body>
</html>

In .aspx.cs page

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Research_BAL;

namespace Research2012
{
    public partial class ImportFromExcel : System.Web.UI.Page
    {
        ImportExcelToGrid oImportExcelToGrid = new ImportExcelToGrid();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnImport_Click(object sender, EventArgs e)
        {
            lblMessage.Visible = true;

            string strFileType = Path.GetExtension(fileuploadExcel.FileName).ToLower();
            string path = Server.MapPath("~/Upload/") + fileuploadExcel.FileName.ToString();
            fileuploadExcel.SaveAs(path);
            string query = "SELECT [UserName],[Education],[Location],[Gender] FROM [Sheet1$]";

            if (!strFileType.Contains(".xls"))
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert", "alert('Please Upload Excel file');", true);
                fileuploadExcel = new FileUpload();
                fileuploadExcel.Attributes.Clear();
            }
            else
            {
                var dt = new DataTable();
                dt = (DataTable)oImportExcelToGrid.ImportToGridView(strFileType, path, query);

                if (dt != null)
                {
                   
                    grvExcelData.DataSource = dt;
                    grvExcelData.DataBind();
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert", "alert('Please check excel file you upload');", true);
                }
            }
            lblMessage.Visible = false;
        }
    }

}

Here I am using tier concept. So I mentioned the below code in BAL Class file in the name of   ImportExcelToGrid.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.OleDb;
using System.IO;
using System.Data;

namespace Research_BAL
{
    public class ImportExcelToGrid
    {
        public object ImportToGridView(string strFileType, string path, string query)
        {
            //Connection String to Excel Workbook
            string connString = "";
            if (strFileType.Trim() == ".xls")
            {
                connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
            }
            else if (strFileType.Trim() == ".xlsx")
            {
                connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
            }

            OleDbConnection conn = new OleDbConnection();
            using (conn = new OleDbConnection(connString))
            {
                //if (conn.State == ConnectionState.Closed)
                //   conn.Open();
                OleDbCommand cmd = new OleDbCommand();
                object res;
                using (cmd = new OleDbCommand(query, conn))
                {
                    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    try
                    {
                        da.Fill(ds);
                        res = ds.Tables[0];
                    }
                    catch (Exception ex)
                    {
                        res = null;
                    }
                    finally
                    {
                        da.Dispose();
                        conn.Close();
                        conn.Dispose();
                        if (File.Exists(path))
                            File.Delete(path);
                    }
                    return res;
                }
            }
        }

    }
}



Saturday 6 July 2013

Convert To Excel and CSV

//Common class library

using System;
using System.Data;
using System.Web;
using System.Text;

using System.Drawing;
using System.Xml.XPath;
using System.Reflection;
//using Excel = Microsoft.Office.Interop.Excel;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI;
/// <summary>
/// Summary description for SpreadToExcel
/// </summary>
public class SpreadToExcel
{
    StringBuilder strb = new StringBuilder();

public SpreadToExcel()
{
//
// TODO: Add constructor logic here
//
}

    public Boolean ConvertToCSV(DataTable dt, string FileName)
    {
        try
        {
            string attachment = string.Format("attachment; filename={0}", FileName + ".csv");
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AddHeader("content-disposition", attachment);
            HttpContext.Current.Response.Charset = "";
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            HttpContext.Current.Response.ContentType = "text/csv";
            HttpContext.Current.Response.AddHeader("Pragma", "public");

            HttpContext.Current.Response.Write(Environment.NewLine);
            //write gridview details
            int iColCount = dt.Columns.Count;
            // First we will write the headers.
            for (int i = 0; i < iColCount; i++)
            {
                //HttpContext.Current.Response.Write(dt.Columns[i].ToString());
                AddComma(dt.Columns[i].ToString(), strb);
            }
            HttpContext.Current.Response.Write(strb.ToString());
            HttpContext.Current.Response.Write(Environment.NewLine);
            strb.Remove(0, strb.Length);
            foreach (DataRow dr in dt.Rows)
            {
                for (int i = 0; i < iColCount; i++)
                {
                    //if (!Convert.IsDBNull(dr[i]))
                    //{
                    AddComma(dr[i].ToString(), strb);

                    //}
                    //if (i < iColCount - 1)
                    //{
                    //    HttpContext.Current.Response.Write(",");
                    //}
                }
                HttpContext.Current.Response.Write(strb.ToString());
                HttpContext.Current.Response.Write(Environment.NewLine);
                strb.Remove(0, strb.Length);
            }
            HttpContext.Current.Response.End();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public Boolean ConvertToCSV(DataTable dtHeader, DataTable dt, string FileName)
    {
        try
        {
            string attachment = string.Format("attachment; filename={0}", FileName + ".csv");
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AddHeader("content-disposition", attachment);
            HttpContext.Current.Response.Charset = "";
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            HttpContext.Current.Response.ContentType = "text/csv";
            HttpContext.Current.Response.AddHeader("Pragma", "public");

            //write header values
            int headColCount = dtHeader.Columns.Count;
            foreach (DataRow dr in dtHeader.Rows)
            {
                for (int i = 0; i < headColCount; i++)
                {
                    //if (!Convert.IsDBNull(dr[i]))
                    //{
                    AddComma(dr[i].ToString(), strb);

                    //}
                    //if (i < iColCount - 1)
                    //{
                    //    HttpContext.Current.Response.Write(",");
                    //}
                }
                HttpContext.Current.Response.Write(strb.ToString());
                HttpContext.Current.Response.Write(Environment.NewLine);
                strb.Remove(0, strb.Length);
            }

            HttpContext.Current.Response.Write(Environment.NewLine);
            //write gridview details
            int iColCount = dt.Columns.Count;
            // First we will write the headers.
            for (int i = 0; i < iColCount; i++)
            {
                //HttpContext.Current.Response.Write(dt.Columns[i].ToString());
                AddComma(dt.Columns[i].ToString(), strb);
            }
            HttpContext.Current.Response.Write(strb.ToString());
            HttpContext.Current.Response.Write(Environment.NewLine);
            strb.Remove(0, strb.Length);
            foreach (DataRow dr in dt.Rows)
            {
                for (int i = 0; i < iColCount; i++)
                {
                    //if (!Convert.IsDBNull(dr[i]))
                    //{
                    AddComma(dr[i].ToString(), strb);

                    //}
                    //if (i < iColCount - 1)
                    //{
                    //    HttpContext.Current.Response.Write(",");
                    //}
                }
                HttpContext.Current.Response.Write(strb.ToString());
                HttpContext.Current.Response.Write(Environment.NewLine);
                strb.Remove(0, strb.Length);
            }
            HttpContext.Current.Response.End();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public void ConvertToCSV(DataTable dt, DataTable dt2, string FileName, Boolean showheader)
    {
        try
        {
            string attachment = string.Format("attachment; filename={0}", FileName + ".csv");
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AddHeader("content-disposition", attachment);
            HttpContext.Current.Response.Charset = "";
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            HttpContext.Current.Response.ContentType = "text/csv";
            HttpContext.Current.Response.AddHeader("Pragma", "public");

            HttpContext.Current.Response.Write(Environment.NewLine);
            //write gridview details
           int iColCount = dt.Columns.Count;
            //// First we will write the headers.
           if (showheader)
           {

               AddComma(dt2.Rows[0][0].ToString(), strb);
               HttpContext.Current.Response.Write(strb.ToString());
               HttpContext.Current.Response.Write(Environment.NewLine);

               strb.Remove(0, strb.Length);

               for (int i = 0; i < iColCount; i++)
               {
                   AddComma(dt.Columns[i].ToString(), strb);
                 
               }
               HttpContext.Current.Response.Write(strb.ToString());
               HttpContext.Current.Response.Write(Environment.NewLine);
               strb.Remove(0, strb.Length);
         
             
               foreach (DataRow dr in dt.Rows)
               {
                   for (int i = 0; i < iColCount; i++)
                   {
                       //if (!Convert.IsDBNull(dr[i]))
                       //{
                       AddComma(dr[i].ToString(), strb);

                       //}
                       //if (i < iColCount - 1)
                       //{
                       //    HttpContext.Current.Response.Write(",");
                       //}
                   }
                   HttpContext.Current.Response.Write(strb.ToString());
                   HttpContext.Current.Response.Write(Environment.NewLine);

                   strb.Remove(0, strb.Length);
               }
           }
           else
           {

               AddComma(dt2.Rows[0][0].ToString(), strb);
               HttpContext.Current.Response.Write(strb.ToString());
               HttpContext.Current.Response.Write(Environment.NewLine);

               strb.Remove(0, strb.Length);

               foreach (DataRow dr in dt.Rows)
               {
                   for (int i = 0; i < iColCount; i++)
                   {
                       //if (!Convert.IsDBNull(dr[i]))
                       //{
                       AddComma(dr[i].ToString(), strb);

                       //}
                       //if (i < iColCount - 1)
                       //{
                       //    HttpContext.Current.Response.Write(",");
                       //}
                   }
                   HttpContext.Current.Response.Write(strb.ToString());
                   HttpContext.Current.Response.Write(Environment.NewLine);

                   strb.Remove(0, strb.Length);
               }
           }
            HttpContext.Current.Response.End();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();
        }
        catch
        {
           
        }
    }

    public Boolean ConvertToCSV(DataTable dtHeader, DataTable dt, DataTable dt2, string FileName)
    {
        try
        {
            string attachment = string.Format("attachment; filename={0}", FileName + ".csv");
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AddHeader("content-disposition", attachment);
            HttpContext.Current.Response.Charset = "";
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            HttpContext.Current.Response.ContentType = "text/csv";
            HttpContext.Current.Response.AddHeader("Pragma", "public");
            //write header values
            int headColCount = dtHeader.Columns.Count;
            foreach (DataRow dr in dtHeader.Rows)
            {
                for (int i = 0; i < headColCount; i++)
                {
                    //if (!Convert.IsDBNull(dr[i]))
                    //{
                    AddComma(dr[i].ToString(), strb);

                    //}
                    //if (i < iColCount - 1)
                    //{
                    //    HttpContext.Current.Response.Write(",");
                    //}
                }
                HttpContext.Current.Response.Write(strb.ToString());
                HttpContext.Current.Response.Write(Environment.NewLine);
                strb.Remove(0, strb.Length);
            }

            //write gridview1 details
            int iColCount = dt.Columns.Count;
            // First we will write the headers.
            for (int i = 0; i < iColCount; i++)
            {
                //HttpContext.Current.Response.Write(dt.Columns[i].ToString());
                AddComma(dt.Columns[i].ToString(), strb);
            }
            HttpContext.Current.Response.Write(strb.ToString());
            HttpContext.Current.Response.Write(Environment.NewLine);
            strb.Remove(0, strb.Length);
            foreach (DataRow dr in dt.Rows)
            {
                for (int i = 0; i < iColCount; i++)
                {
                    //if (!Convert.IsDBNull(dr[i]))
                    //{
                    AddComma(dr[i].ToString(), strb);

                    //}
                    //if (i < iColCount - 1)
                    //{
                    //    HttpContext.Current.Response.Write(",");
                    //}
                }
                HttpContext.Current.Response.Write(strb.ToString());
                HttpContext.Current.Response.Write(Environment.NewLine);
                strb.Remove(0, strb.Length);
            }

            HttpContext.Current.Response.Write(Environment.NewLine);

            //write gridview2 details
            iColCount = 0;
            iColCount = dt2.Columns.Count;
            // First we will write the headers.
            for (int i = 0; i < iColCount; i++)
            {
                //HttpContext.Current.Response.Write(dt.Columns[i].ToString());
                AddComma(dt2.Columns[i].ToString(), strb);
            }
            HttpContext.Current.Response.Write(strb.ToString());
            HttpContext.Current.Response.Write(Environment.NewLine);
            strb.Remove(0, strb.Length);
            foreach (DataRow dr in dt2.Rows)
            {
                for (int i = 0; i < iColCount; i++)
                {
                    //if (!Convert.IsDBNull(dr[i]))
                    //{
                    AddComma(dr[i].ToString(), strb);

                    //}
                    //if (i < iColCount - 1)
                    //{
                    //    HttpContext.Current.Response.Write(",");
                    //}
                }
                HttpContext.Current.Response.Write(strb.ToString());
                HttpContext.Current.Response.Write(Environment.NewLine);
                strb.Remove(0, strb.Length);
            }

            HttpContext.Current.Response.End();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();
            return true;
        }
        catch
        {
            return false;
        }
    }

    private static void AddComma(string item, StringBuilder strb)
    {
        strb.Append(item.Replace(',', ' '));
        strb.Append(" ,");
    }

    public Boolean ConvertToExcel(System.Data.DataTable dtHeader, System.Data.DataTable dt, string FileName)
    {
        try
        {
            // string attachment = string.Format("attachment; filename={0}", FileName + ".csv");
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + FileName + ".xls");
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";

            StringWriter stringWriter = new StringWriter();
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
            DataGrid dataExportExcel = new DataGrid();
            StringBuilder sbResponseString = new StringBuilder();

            sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=windows-1252\"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>Test</x:Name><x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head> <body>");
            dataExportExcel.ItemDataBound += new DataGridItemEventHandler(dataGrid_ItemDataBound);
            dataExportExcel.DataSource = dtHeader;
            dataExportExcel.DataBind();
            dataExportExcel.RenderControl(htmlWrite);

            dataExportExcel.ItemDataBound += new DataGridItemEventHandler(dataGrid_ItemDataBound);
            dataExportExcel.DataSource = dt;
            dataExportExcel.DataBind();
            dataExportExcel.RenderControl(htmlWrite);

            sbResponseString.Append(stringWriter);
            sbResponseString.Append("</body></html>");

            HttpContext.Current.Response.Write(sbResponseString.ToString());
            HttpContext.Current.Response.End();

            return true;
        }
        catch
        {
            return false;
        }
    }

    public Boolean ConvertToExcel(System.Data.DataTable dt, string FileName)
    {
        try
        {
             string attachment = string.Format("attachment; filename={0}", FileName + ".csv");
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + FileName + ".xls");
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";

            StringWriter stringWriter = new StringWriter();
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
            DataGrid dataExportExcel = new DataGrid();
            StringBuilder sbResponseString = new StringBuilder();

            sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=windows-1252\"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>Test</x:Name><x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head> <body>");

            dataExportExcel.ItemDataBound += new DataGridItemEventHandler(dataGrid_ItemDataBound);
            dataExportExcel.DataSource = dt;
            dataExportExcel.DataBind();
            dataExportExcel.RenderControl(htmlWrite);

            sbResponseString.Append(stringWriter);
            sbResponseString.Append("</body></html>");

            HttpContext.Current.Response.Write(sbResponseString.ToString());
            //HttpContext.Current.Response.End();

            return true;
        }
        catch(Exception ex)
        {
            return false;
        }
    }

    private void dataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Header)
        {
            //Header Text Format can be done as follows
            e.Item.Font.Bold = true;

            //Adding Filter/Sorting functionality for the Excel
            int cellIndex = 0;
            while (cellIndex < e.Item.Cells.Count)
            {
                // e.Item.Cells[cellIndex].Attributes.Add("x:autofilter", "all");
                e.Item.Cells[cellIndex].Width = 200;
                e.Item.Cells[cellIndex].BackColor = Color.DarkOrange;
                e.Item.Cells[cellIndex].ForeColor = Color.White;
                e.Item.Cells[cellIndex].HorizontalAlign = HorizontalAlign.Center;
                cellIndex++;
            }
        }

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            int cellIndex = 0;
            while (cellIndex < e.Item.Cells.Count)
            {
                //Any Cell specific formatting should be done here
                e.Item.Cells[cellIndex].HorizontalAlign = HorizontalAlign.Left;
                if (e.Item.ItemType == ListItemType.AlternatingItem && e.Item.Cells.Count != 1)
                {
                    e.Item.Cells[cellIndex].BackColor = Color.LightGray;
                }
                else
                {
                    e.Item.Cells[cellIndex].BackColor = Color.White;
                }
                cellIndex++;
            }
        }

        if (e.Item.Cells.Count == 1)
        {
            e.Item.Cells[0].ColumnSpan = 9;
        }

    }
}

//In .net  Click event we call using the following code

 public Boolean ExportToData(string formatType, string status)
        {
            var res = false;
            DataTable dt = (DataTable)oExportDataTo.ExportData(status);
            createHeader();
            SetHeaderName(dt);
            if (formatType == "Excel")
              res= oEDI.ConvertToExcel(dtHeader,dt, "Report");
            else if (formatType == "CSV")
              res=oEDI.ConvertToCSV(dt, "Report");

            return res;
        }

        private void createHeader()
        {
            dtHeader = new DataTable();

            string str = "Celestica Internal Users - " + String.Format("{0:dd-MMM-yyyy}", DateTime.Now);
            dtHeader.Columns.Add(new DataColumn(str, Type.GetType("System.String")));
            DataColumn c = new DataColumn();
            DataRow drow = dtHeader.NewRow();

            drow = dtHeader.NewRow();
            drow[0] = "Generated By : Admin";
            dtHeader.Rows.Add(drow);
        }

        private void SetHeaderName(DataTable dt)
        {
            dt.Columns.Remove("CLUM_Id");
            dt.Columns.Remove("CLUM_SpecialPassword");
            dt.Columns.Remove("CLUM_Password");
            dt.Columns.Remove("CLUM_FrgtPwd_Ques");
            dt.Columns.Remove("CLUM_FrgtPwd_Ans");
            dt.Columns.Remove("CLUM_PermittedLogin");
            dt.Columns.Remove("CLUM_BuyerCode");
            dt.Columns.Remove("CLUM_Notes");

            dt.Columns[0].ColumnName = "First Name";
            dt.Columns[1].ColumnName = "Last Name";
            dt.Columns[2].ColumnName = "Title";
            dt.Columns[3].ColumnName = "eMail";
            dt.Columns[4].ColumnName = "Phone No";
            dt.Columns[5].ColumnName = "Mobile No";
        }

Thursday 4 July 2013

View the deleted records from a sql server table?

DBCC LOG(MyDatabase,-1)
SQL Server keeps log for each deleted records. you can query these logs via 'fn_dblog' sql server function.Select [RowLog Contents 0] FROM   sys.fn_dblog(NULL, NULL) WHERE  AllocUnitName = 'dbo.TableName'      AND Context IN ( 'LCX_MARK_AS_GHOST', 'LCX_HEAP' ) AND Operation in ( 'LOP_DELETE_ROWS' ) 
But this log is in Hex format. and you need to convert this Hex format to your actual data.Given below is the article that can help you to recover the deleted records in the same way defined above.
http://raresql.com/2011/10/22/how-to-recover-deleted-data-from-sql-sever/

Get unknowingly (Closed) tab query

SELECT cp.objtype AS PlanType,
       OBJECT_NAME(st.objectid,st.dbid) AS ObjectName,
       cp.refcounts AS ReferenceCounts,
       cp.usecounts AS UseCounts,
       st.text AS SQLBatch,
       qp.query_plan AS QueryPlan
FROM sys.dm_exec_cached_plans AS cp
CROSS APPLY sys.dm_exec_query_plan(cp.plan_handle) AS qp
CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) AS st;

From the output find from plan_type field as prepared or adhoc