Pages


string Filename = System.Web.HttpContext.Current.Server.MapPath(@"~\TextFiles\new.txt");
        FileStream fs = null;
        StreamWriter sw = null;
        try
        {

            fs = new FileStream(Filename, FileMode.Append, FileAccess.Write);
            sw = new StreamWriter(fs);
            try
            {
                sw.WriteLine("Hello...!");
            }
            catch { }
            finally { sw.Flush(); sw.Dispose(); sw.Close(); fs.Close(); }
        }
        catch { }

private void create_csv(DataTable dt, string fname)
        {
            //if (string.IsNullOrEmpty(fname))
            //{
            //    fname = Application.StartupPath + "\\pdftemplate_" + DateTime.Now.Ticks + ".csv";
            //}
            //else { fname += ".csv"; } fullfilename = fname;
            //FileInfo f = new FileInfo(fname);
            //try
            //{
            //    if (f.Exists)
            //    {
            //        f.Delete();
            //    }
            //}
            //catch { f.Delete(); }
            fname = GenarateFilename(fname, ".csv");
            fullfilename = fname;

            FileStream fs = new FileStream(fname, FileMode.CreateNew, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            StringBuilder sb = new StringBuilder();
            try
            {

                //first line coloumn names
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    sb.Append("," + dt.Columns[i].ColumnName.ToString());
                }
                sw.WriteLine(sb.ToString().Substring(1));
                sb.Length = 0;
                // insert datarows in csv file
                foreach (DataRow dr in dt.Rows)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        sb.Append("," + dr[i].ToString());
                    }
                    sw.WriteLine(sb.ToString().Substring(1));
                    sb.Length = 0;
                }
            }
            catch { }
            finally
            {
                sw.Flush();
                sw.Close();
                fs.Close();
                fs.Dispose();
                sb = null;
            }

        }

First we should add a reference to Microsoft.Office.Interop


using Excel = Microsoft.Office.Interop.Excel;

private void create_excel(DataTable dt, string fname)
        {
            //if (string.IsNullOrEmpty(fname))
            //{
            //    fname = Application.StartupPath + "\\pdftemplate_" + DateTime.Now.Ticks + ".xls";
            //}
            //else { fname += ".xls"; }
            //fullfilename = fname;
            fname = GenarateFilename(fname, ".xls");
            fullfilename = fname;

            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Add(misValue);

            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            Excel.Range chartRange;

            for (int i = 0; i < dt.Columns.Count; i++)
            {
                xlWorkSheet.Cells[1, i + 1] = dt.Columns[i].ColumnName;

            }
            chartRange = xlWorkSheet.get_Range("A1", cellcount(dt.Columns.Count) + "1");
            chartRange.Font.Bold = true;
            chartRange.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Maroon);


            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    xlWorkSheet.Cells[i + 2, j + 1] = dt.Rows[i][j].ToString();
                }
            }

            chartRange.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic);
            xlWorkBook.SaveAs(fname, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
        }



First you should add the reference to iTextSharp.dll
Then the following method will create a PDF document and the document contains a table with the supplied data.

using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;




private void create_pdf(DataTable dt, string fname)
        {

            fname = GenarateFilename(fname, ".pdf");
            fullfilename = fname;

            Document pdfdoc = new Document(iTextSharp.text.PageSize.A3, 10.0F, 10.0F, 10.0F, 10.0F);
            PdfWriter pw = PdfWriter.GetInstance(pdfdoc, new FileStream(fname, FileMode.Create));
            pdfdoc.Open();
            iTextSharp.text.pdf.PdfPTable table = new PdfPTable(dt.Columns.Count);
            table.TotalWidth = PageSize.A3.Width;
            table.WidthPercentage = 100;
            table.SpacingBefore = 20f;
            table.SpacingAfter = 30f;
           
            //table.HorizontalAlignment = Element.ALIGN_CENTER;
            table.DefaultCell.Border = iTextSharp.text.Rectangle.LEFT_BORDER | iTextSharp.text.Rectangle.RIGHT_BORDER | iTextSharp.text.Rectangle.TOP_BORDER | iTextSharp.text.Rectangle.BOTTOM_BORDER;
            iTextSharp.text.BaseColor CLR = new iTextSharp.text.BaseColor(105, 56, 40);

            iTextSharp.text.Font colfont = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA.ToString(), 9, 1, CLR);
            foreach (DataColumn dc in dt.Columns)
            {
                PdfPCell cell = new PdfPCell();
                cell.Border = iTextSharp.text.Rectangle.LEFT_BORDER | iTextSharp.text.Rectangle.RIGHT_BORDER | iTextSharp.text.Rectangle.TOP_BORDER | iTextSharp.text.Rectangle.BOTTOM_BORDER;
                cell.BorderWidth = 1f;
                Chunk chunkcols = new Chunk(dc.ToString().ToUpperInvariant(), colfont);
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.BorderColor = new BaseColor(222, 189, 140);
                cell.BackgroundColor = new BaseColor(210, 165, 112);
                cell.AddElement(chunkcols);
                table.AddCell(cell);
            }

            Chunk chunkrows;
            iTextSharp.text.BaseColor color__1 = new iTextSharp.text.BaseColor(105, 56, 40);
            iTextSharp.text.Font rowfont = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA.ToString(), 7, color__1);
            foreach (DataRow dr in dt.Rows)
            {
                for (int M = 0; M < dt.Columns.Count; M++)
                {
                    PdfPCell cell1 = new PdfPCell();
                    cell1.Border = iTextSharp.text.Rectangle.LEFT_BORDER | iTextSharp.text.Rectangle.RIGHT_BORDER | iTextSharp.text.Rectangle.TOP_BORDER | iTextSharp.text.Rectangle.BOTTOM_BORDER;
                    //cell1.Phrase = new Phrase(dr[M].ToString());
                    chunkrows = new Chunk(dr[M].ToString(), rowfont);
                    cell1.HorizontalAlignment = (int)cellalignments(dr[M].GetType().BaseType.ToString(), "P.D.F."); //iTextSharp.text.pdf.PdfPCell.ALIGN_RIGHT;// Element.ALIGN_RIGHT; //Convert.ToInt32(cellalignments(dr[M].GetType().BaseType.ToString(), "P.D.F."));//Element.ALIGN_LEFT;
                    cell1.BorderColor = new BaseColor(222, 189, 140);
                    cell1.AddElement(chunkrows);
                    table.AddCell(cell1);
                    cell1 = null;
                }
            }
            pdfdoc.Add(table);
            pdfdoc.Close();

        }




Add the below code under aspx page with the reference of  jquery-1.6.2.min.js plug in and Add ProductsOnScroll.js


Sample.aspx Page:




<head runat="server">
    <title>Untitled Page</title>
    <link href="CSS/Stylesheet1.css" rel="stylesheet" type="text/css" />
    <script src="Scripts/jquery-1.6.2.min.js" type="text/javascript"></script>
    <script src="Scripts/AddProductsOnScroll.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server" >
      <div id="mainDiv" style="width:980px">
         <div id="wrapperDiv" runat="server" style="padding-left:12px;"> </div>
      </div>
      </form>
</body>
</html>




//Under  Sample.cs  code:




public static int counter = 0;
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                counter = 0;
                fill_firstTime();
            }
        }
        [WebMethod]
        public static string GetDataFromServer()
        {
            string str1 = string.Empty, str2 = string.Empty, resp = string.Empty;
           
            str1 = @"
            <div style='float:left;width:160px;  ' >
                <table align='center' style='border-bottom:Dotted 1px Grey;'>
                <tr>
                    <td colspan='2' class='DealHeading'>
                        <span>Name of the deal</span>
                    </td>
                </tr>
                <tr>
                    <td colspan='2' class='DealImage'>
                          <img src='";
            str2 = @"' Height='150px' Width='150px' style='border:Solid 1px Grey' alt='' />
                    </td>
                </tr>
                </table>
            </div>  ";

            DataSet ds = db.getItems(counter);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                resp += str1 + ds.Tables[0].Rows[i]["dealimage1"].ToString() + str2;
                counter = counter + 1;
            }
            return resp;
        }
        private void fill_firstTime()
        {
            string str1 = string.Empty, str2 = string.Empty, resp = string.Empty;
            str1 = @"
            <div style='float:left;width:160px;  ' >
                <table align='center' style='border-bottom:Dotted 1px Grey;'>
                <tr>
                    <td colspan='2' class='DealHeading'>
                        <span>Name of the deal</span>
                    </td>
                </tr>
                <tr>
                    <td colspan='2' class='DealImage'>
                          <img src='";
            str2 = @"' Height='150px' Width='150px' style='border:Solid 1px Grey' alt='' />
                    </td>
                </tr>
                </table>
            </div>  ";
            DataSet ds = db.getItems(counter);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                resp += str1 + ds.Tables[0].Rows[i]["dealimage1"].ToString() + str2;
                counter = counter + 1;
            }
          
            wrapperDiv.InnerHtml = resp + resp;
        }







// JScript File


LoadOnScroll.js

$(document).ready   (
                        function()
                        {
                            $contentLoadTriggered = false;
                            $("#mainDiv").scroll    (
                                                        function()
                                                        {
                                                            if($("#mainDiv").scrollTop() >= ($("#wrapperDiv").height() - $("#mainDiv").height()) && $contentLoadTriggered == false)
                                                            {
                                                                $contentLoadTriggered = true;
                                                                $.ajax  (
                                                                            {
                                                                                type: "POST",
                                                                                url: "LoadOnScroll.aspx/GetDataFromServer",
                                                                                data: "{}",
                                                                                contentType: "application/json; charset=utf-8",
                                                                                dataType: "json",
                                                                                async: true,
                                                                                cache: false,
                                                                                success:    function (msg) 
                                                                                            {
                                                                                                $("#wrapperDiv").append(msg.d);
                                                                                                $contentLoadTriggered = false;
                                                                                            },
                                                                                error:  function (x, e)
                                                                                        {
                                                                                            alert("The call to the server side failed. " + x.responseText);
                                                                                        }
                                                                            }
                                                                        );
                                                            }
                                                        }
                                                    );
                        }
                    );


and also add the Jquery file 
jquery-1.6.2.min.js. click here to download jquery http://code.jquery.com/jquery-1.6.2.min.js


For Source code please drop ur mail id 
using System.Net.Mail;  

public static void sendMail(string to, string subject, string body)
   {
       
           MailMessage mMailMessage = new MailMessage();
           mMailMessage.To.Add(new MailAddress(to));
           mMailMessage.Subject = subject;
           mMailMessage.Body = body;
           mMailMessage.IsBodyHtml = true;
           mMailMessage.Priority = MailPriority.Normal;
mMailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
           SmtpClient mSmtpClient = new SmtpClient();
           mSmtpClient.EnableSsl = true;
           mSmtpClient.Send(mMailMessage);
          
               

   }



Under web.config file


<system.net>
<mailSettings>
<smtp from="mohanrajunadimpally@gmail.com">

<network host="smtp.gmail.com" port="587" userName="UserName" password="Password"/>
</smtp>
</mailSettings>
</system.net>