Wednesday, December 23, 2015

Add url to google

For adding url click on below link

Add url to google

Add Website URL

  • After completing this process with 2-3 days Google verify your url and based on your SEO it will ranks your site. 
  • After applying ranking it will add your url to its database



Monday, December 7, 2015

Email sending in C#

Email sending with Attachment

public static bool SendEmailMessage(string SenderEmailAddress, string SenderPassword, string SenderHostName, int SenderPort, string Subject, string Body, string emailTO, bool EnableSSL = false, string CC = "", string BCC = "", MailPriority Priority = MailPriority.Normal, string path = "")
{
            try
            {
                System.Net.Mail.MailMessage objMail = new System.Net.Mail.MailMessage();
                //Set from address
                objMail.From = new MailAddress(SenderEmailAddress);

                // Set to addresses
                if (string.IsNullOrEmpty(emailTO))
                {
                    objMail.To.Add(new MailAddress(emailTO));
                }

                // Set CC addresses
                if (string.IsNullOrEmpty(CC))
                {
                    objMail.CC.Add(new MailAddress(CC));
                }

                // Set BCC addresses
                if (string.IsNullOrEmpty(BCC))
                {
                    objMail.Bcc.Add(new MailAddress(BCC));
                }

                // Set subject
                objMail.Subject = Subject;
                // Set body/message
                objMail.Body = Body;
                objMail.IsBodyHtml = true;

                // Set priority default is "Normal"
                objMail.Priority = Priority;

                // Set attachement
                if (string.IsNullOrEmpty(path))
                {
                    objMail.Attachments.Add(new Attachment(path));
                }

                SmtpClient SMTPClientObj = new SmtpClient();
                SMTPClientObj.UseDefaultCredentials = false;
                SMTPClientObj.Credentials = new System.Net.NetworkCredential(SenderEmailAddress, SenderPassword);  //Credentials  of sender
                SMTPClientObj.Host = SenderHostName; //Host name
                SMTPClientObj.DeliveryMethod = SmtpDeliveryMethod.Network;
                SMTPClientObj.Port = SenderPort; // Port Number
                SMTPClientObj.EnableSsl = EnableSSL;
                SMTPClientObj.Send(objMail);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

Export GridView to CSV/Excel in C#

Step 1 : Bind GridView

            //This allows you to bind all data of GridView into ExcelSheet
            GridView1.AllowPaging = false;

            GridView1.DataSource = //List Object or DataTable
            GridView1.DataBind();
 


Step 2 : Export GridView To Excel

            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename="                           + fileName + ".xls");   // fileNmae = Name of file
            HttpContext.Current.Response.ContentType = "application/excel";
            System.IO.StringWriter
sw = new System.IO.StringWriter();
            HtmlTextWriter
htw = new HtmlTextWriter(sw);
            GridView1.RenderControl(htw);
            HttpContext
.Current.Response.Write(sw.ToString());
            HttpContext
.Current.Response.End();

 

Create DataTable from List in C#

T is type of Class which you want convert i.e. T = Class Name

 public static DataTable ConvertToDataTable<T>(List<T> data)
 {
            //Create property description collection object of you table type object
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));

            DataTable table = new DataTable();

           //Bind object field as header row of DataTable
            foreach (PropertyDescriptor prop in properties)
            {
                table.Columns.Add(prop.Name, typeof(string));
            }

            //Bind data of list into DataTable
            foreach (T item in data)
            {
                //Create dataRow object for Table
                DataRow row = table.NewRow();
                foreach (PropertyDescriptor prop in properties)
                {
                   //Assign list value into dataTable column
                    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                }
                table.Rows.Add(row);
            }
         
           //return DataTable object
            return table;
 }

Image cropping in C#


public static byte[] CropImage(string Img, int Width, int Height, int X, int Y)
{
            try
            {
                //Generate Image from specified Image file path
                //Img = file path
                System.Drawing.Image OriginalImage = System.Drawing.Image.FromFile(Img);

                //Generate Bitmap with given Width and Height
                Bitmap bmp = new Bitmap(Width, Height);
               //Set Resolution of Bitmap
                bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);

                //Create Graphic for Bitmap
                Graphics Graphic = Graphics.FromImage(bmp);
                Graphic.SmoothingMode = SmoothingMode.AntiAlias;
                Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;

                //Draw image from Original image for above Graphic Object
                Graphic.DrawImage(OriginalImage, new Rectangle(0, 0, Width, Height), X, Y, Width, Height, GraphicsUnit.Pixel);

                MemoryStream ms = new MemoryStream();
                //Save bitmap image to specified stream with specified format
                bmp.Save(ms, OriginalImage.RawFormat);
                OriginalImage.Dispose();

               //return image into byte array
                return ms.GetBuffer();
            }
            catch (Exception ex)
            {
                return null;
            }
}

Convert Number into text format in C#

Below function will convert inputted number into text format

public static string NumberToText(int number, bool isUK)
{
            // return "Zero" if number is 0
            if (number == 0) return "Zero";
          
            string and = isUK ? "and " : ""; // deals with UK or US numbering
 
            if (number == -2147483648) return "Minus Two Billion One Hundred " + and +
            "Forty Seven Million Four Hundred " + and + "Eighty Three Thousand " +
            "Six Hundred " + and + "Forty Eight";
            int[] num = new int[4];
            int first = 0;
            int u, h, t;
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            if (number < 0)
            {
                sb.Append("Minus ");
                number = -number;
            }

            string[] words0 = { "", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine " };
            string[] words1 = { "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ",      "Seventeen ", "Eighteen ", "Nineteen " };
            string[] words2 = { "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety " };
            string[] words3 = { "Thousand ", "Million ", "Billion " };
            num[0] = number % 1000;           // units
            num[1] = number / 1000;
            num[2] = number / 1000000;
            num[1] = num[1] - 1000 * num[2];  // thousands
            num[3] = number / 1000000000;     // billions
            num[2] = num[2] - 1000 * num[3];  // millions
 
            for (int i = 3; i > 0; i--)
            {
                if (num[i] != 0)
                {
                    first = i;
                    break;
                }
            }

            for (int i = first; i >= 0; i--)
            {
                if (num[i] == 0) continue;
                u = num[i] % 10;              // ones
                t = num[i] / 10;
                h = num[i] / 100;             // hundreds
                t = t - 10 * h;               // tens
                if (h > 0) sb.Append(words0[h] + "Hundred ");
                if (u > 0 || t > 0)
                {
                    if (h > 0 || i < first) sb.Append(and);
                    if (t == 0)
                        sb.Append(words0[u]);
                    else if (t == 1)
                        sb.Append(words1[u]);
                    else
                        sb.Append(words2[t - 2] + words0[u]);
                }
                if (i != 0) sb.Append(words3[i - 1]);
            }

            return sb.ToString().TrimEnd();
}

Check string is number or not in C#

Int.TryParse() method converts string into integer and if this conversion is not possible then it will return false means your inputted string is not number

string number = "1234";
int a = 0;
bool isNumber = int.TryParse(number, out a);

if (isNumber == true)
{
            //string is valid number
}
else
{
           //Given string is not a number
}