Wednesday, 8 May 2013

Validate Extension Of Selected File In File Uploader Before Uploading in Asp .Net C#

Validate Extension Of Selected File In File Uploader Before Uploading in Asp .Net C#Using RegularExpressionValidator ..
  
 <asp:FileUpload ID="fuParon" runat="server" />

<asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server"
            ErrorMessage="Only Excel file is allowed!"
            ValidationExpression ="^.+(.xls|.xlsx)$" ControlToValidate="fuParon"
            style="color: #FF5050" ValidationGroup="upload" ></asp:RegularExpressionValidator>


<asp:ImageButton ID="BtnUpload" runat="server"   ImageUrl="uploadimage.png"  Width=30px                  Height=30px  ValidationGroup="upload"  />

Sunday, 5 May 2013

Get Next Auto Increment Value of a Table in MySql

  SELECT AUTO_INCREMENT FROM information_schema.`tables`   WHERE TABLE_SCHEMA ='DatabaseName'  AND TABLE_NAME = 'TableName'

Friday, 12 April 2013

Show Message Box In ASP .Net with C# From Server Side

ClientScript.RegisterStartupScript(Page.GetType(), "alert", "<script language='javascript'>alert('HAI MUKESH')</script>");

Friday, 5 April 2013

Generate Barcode in C# ASP.Net

How to Generate Barcode , Save It Into a folder and Display Generated Barcode

              1. Initially You have to download the open source DLL iTextSharp(For Create Pdf)
              2. Add the iTextSharp  DLL To the project.
              3. Create a folder with name barcodes to store barcode inside the  pdf.
              4. Add iTextSharp namespace to the Project.
                       using iTextSharp.text;
                       using iTextSharp.text.pdf;
                       using iTextSharp.text.html.simpleparser;

              5. Store the value you want to Generate Barcode in to the Variable Code 
              6. Then you can generate barcodes.
              7.Go and see the barcodes Folder In the Project to see the generated Barcode inside Pdf
 
Name Spaces
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Imaging;
using System.IO;
using System.Data;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.Text;
using System.IO.Ports;
using System.Net;



Barcode Generating Code

        string id = Guid.NewGuid().ToString();

        MemoryStream MS = new MemoryStream();
        Document pdfDoc = new Document(PageSize.A4, 10, 10, 10, 10);
        PdfWriter.GetInstance(pdfDoc, MS);

        pdfDoc.Open();

            // string code = dtExcelData.Rows[j].ItemArray[0].ToString();
            string sWorkPath = "";

            sWorkPath = this.Context.Server.MapPath("");

            //  string Code = code;
            string Code ="12345";
            // Multiply the lenght of the code by 40 (just to have enough width)
            int w = Code.Length * 40;


            // int w = Code.Length * 25;


            // Create a bitmap object of the width that we calculated and height of 100
            Bitmap oBitmap = new Bitmap(w, 100);


            // then create a Graphic object for the bitmap we just created.
            Graphics oGraphics = Graphics.FromImage(oBitmap);


            PrivateFontCollection fnts = new PrivateFontCollection();
            fnts.AddFontFile(sWorkPath + @"\IDAutomationHC39M.ttf");
            FontFamily fntfam = new FontFamily("IDAutomationHC39M", fnts);
            System.Drawing.Font oFont = new System.Drawing.Font(fntfam, 18);

            PointF oPoint = new PointF(2f, 2f);
            SolidBrush oBrushWrite = new SolidBrush(Color.Black);
            SolidBrush oBrush = new SolidBrush(Color.White);



            oGraphics.FillRectangle(oBrush, 0, 0, w, 100);



            oGraphics.DrawString("*" + Code + "*", oFont, oBrushWrite, oPoint);

            System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
            using (MemoryStream ms = new MemoryStream())
            {
                oBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                byte[] byteImage = ms.ToArray();

                Convert.ToBase64String(byteImage);
                imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
            }

            iTextSharp.text.Image i = iTextSharp.text.Image.GetInstance(oBitmap, System.Drawing.Imaging.ImageFormat.Bmp);


            pdfDoc.Add(i);

       


        pdfDoc.Close();


        Response.Buffer = true;

        byte[] content = MS.ToArray();


        using (FileStream fs = File.Create(Server.MapPath("~/barcodes/" + id + ".pdf")))
        {
            fs.Write(content, 0, (int)content.Length);

        }


        System.Diagnostics.Process.Start(Server.MapPath("~/barcodes/" + id + ".pdf"));




Output


Thursday, 28 March 2013

Send Email with attachment In Asp .Net with c#

Send Email with attachment In Asp .Net with c#       

 string filePath  ="C:\\abc.doc";
        string senderEmailid="senderemailid@gmail.com";
        string senderPassword="password";
        string recieverEmailid="recievermailid@gmail.com";
        var client = new SmtpClient("smtp.gmail.com", 587)
        {
            Credentials = new NetworkCredential(senderEmailid, senderPassword),
            EnableSsl = true
        };

        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(senderEmailid,             recieverEmailid);

        mail.Attachments.Add(new Attachment(Server.MapPath(filePath)));

        mail.Subject ="Subject of Mail"
        mail.Body ="<b>Hai</b>";
   
       
        mail.IsBodyHtml = true; // To make html

        client.Send(mail);

Sunday, 24 March 2013

Method to Convert Number to Words Asp .Net C#

Method to Convert Number to Words Asp .Net C#

 private string NumberToWords(int number)
    {
        if (number == 0)
            return "zero";

        if (number < 0)
            return "minus " + NumberToWords(Math.Abs(number));

        string words = "";
        if ((number / 1000000000) > 0)
        {
            words += NumberToWords(number / 1000000000) + " Billion ";
            number %= 1000000000;
        }

        if ((number / 10000000) > 0)
        {
            words += NumberToWords(number / 10000000) + " Crore ";
            number %= 10000000;
        }

        if ((number / 1000000) > 0)
        {
            words += NumberToWords(number / 1000000) + " million ";
            number %= 1000000;
        }


        if ((number / 100000) > 0)
        {
            words += NumberToWords(number / 100000) + " lakh ";
            number %= 100000;
        }


        if ((number / 1000) > 0)
        {
            words += NumberToWords(number / 1000) + " thousand ";
            number %= 1000;
        }

        if ((number / 100) > 0)
        {
            words += NumberToWords(number / 100) + " hundred ";
            number %= 100;
        }

        if (number > 0)
        {
            if (words != "")
                words += "and ";

            var unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
            var tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

            if (number < 20)
                words += unitsMap[number];
            else
            {
                words += tensMap[number / 10];
                if ((number % 10) > 0)
                    words += "-" + unitsMap[number % 10];
            }
        }

        return words;
    }



Example :

NumberToWords(33333); //Calling Method

Output

thirty-three thousand three hundred and thirty-three

IE7 Issues Text Indent

                 Unfortunately IE 7 is still widespread among the users hence while theming we have to give special importance to the grea...