Friday, 25 January 2013

Mail send C# Code


using System;
using System.Data;
using System.Configuration;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


/// <summary>
/// Summary description for SendMail
/// </summary>
public class SendMail
{
    public static bool sendMail(string[] toEmail, string[] ccEmail, string[] bccEmail, string Subject, string MessageBody, string AttachFilePath)
    {
        SmtpClient objSMTP;
        MailMessage objMail;

        objMail = new MailMessage();
        objSMTP = new SmtpClient();
        try
        {
            if (ccEmail.Length > 0)
                for (int toCount = 0; toCount < ccEmail.Length; toCount++)
                    objMail.To.Add(toEmail[toCount]);

            if (ccEmail.Length > 0)
                for (int ccCount = 0; ccCount < ccEmail.Length; ccCount++)
                    if (!string.IsNullOrEmpty(ccEmail[ccCount]))
                        objMail.CC.Add(ccEmail[ccCount]);

            if (bccEmail.Length > 0)
                for (int bccCount = 0; bccCount < bccEmail.Length; bccCount++)
                    if (!string.IsNullOrEmpty(bccEmail[bccCount]))
                        objMail.Bcc.Add(bccEmail[bccCount]);
            objMail.From = new MailAddress(Membership.GetUser().Email); //Membership.GetUser().Email);
            objMail.IsBodyHtml = true;
            objMail.Subject = Subject;
            objMail.Body = MessageBody;
            objSMTP.Send(objMail);
            return true;
        }
        catch (Exception ex)
        {
       
        }
    }

    public static bool sendMailwithoutattachment(string[] toEmail, string[] ccEmail, string Subject, string MessageBody)
    {
        SmtpClient objSMTP;
        MailMessage objMail;

        objMail = new MailMessage();
        objSMTP = new SmtpClient();
        try
        {
            if (ccEmail.Length > 0)
                for (int toCount = 0; toCount < ccEmail.Length; toCount++)
                    objMail.To.Add(toEmail[toCount]);

            if (ccEmail.Length > 0)
                for (int ccCount = 0; ccCount < ccEmail.Length; ccCount++)
                    if (!string.IsNullOrEmpty(ccEmail[ccCount]))
                        objMail.CC.Add(ccEmail[ccCount]);
            var frommail = "xxx@gmail.com"; //for testing
            objMail.From = new MailAddress(frommail);
            objMail.IsBodyHtml = true;
            objMail.Subject = Subject;
            objMail.Body = MessageBody;
            objSMTP.Send(objMail);
            return true;
        }
        catch (Exception ex)
        {
       
        }
    }
    public static bool sendMailBcc(string[] toEmail, string[] bccEmail, string Subject, string MessageBody)
    {
        SmtpClient objSMTP;
        MailMessage objMail;

        objMail = new MailMessage();
        objSMTP = new SmtpClient();
        try
        {
            if (bccEmail.Length > 0)
                for (int toCount = 0; toCount < bccEmail.Length; toCount++)
                    objMail.To.Add(toEmail[toCount]);

            if (bccEmail.Length > 0)
                for (int bccCount = 0; bccCount < bccEmail.Length; bccCount++)
                    if (!string.IsNullOrEmpty(bccEmail[bccCount]))
                        objMail.Bcc.Add(bccEmail[bccCount]);
            var frommail = "xxx@gmail.com."; //for testing
            objMail.From = new MailAddress(frommail);
            objMail.IsBodyHtml = true;
            objMail.Subject = Subject;
            objMail.Body = MessageBody;
            objSMTP.Send(objMail);
            return true;
        }
        catch (Exception ex)
        {
 
        }
    }
}

Friday, 18 January 2013

Only Number validation in Textbox of ASP.NET Using Regular Expression validator


Only numbers can enter into that Textbox
We can use Regular expression validator for this:
In the validation expression property keep ^\d+$.

<asp:TextBox ID="TextBox1" runat="server" Style="z-index: 100; left: 259px; position: absolute;
top: 283px" ValidationGroup="check"></asp:TextBox>


<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="Please Enter Only Numbers" Style="z-index: 101; left: 424px; position: absolute;
top: 285px" ValidationExpression="^\d+$" ValidationGroup="check"></asp:RegularExpressionValidator>

Sunday, 6 January 2013

How To display Image using Linq To Sql




Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title></title>
</head>
<body>
   <form id="form1" runat="server">
   <div>
       <asp:GridView ID="grdCategories" runat="server" DataKeyNames="CategoryID" Width="600px"
           AutoGenerateColumns="false">
           <Columns>
               <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
               <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />
               <asp:TemplateField>
                   <ItemTemplate>
                       <asp:Image ID="Image1" runat="server" ImageUrl='<%#"ImageHandler.ashx?CategoryID="+ Eval("CategoryID")  %>' />
                   </ItemTemplate>
               </asp:TemplateField>
           </Columns>
       </asp:GridView>
   </div>
   </form>
</body>
</html>

Default.aspx.cs




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
   public partial class _Default : System.Web.UI.Page
   {
       protected void Page_Load(object sender, EventArgs e)
       {
           NorthwindDataContext db = new NorthwindDataContext();
           var cate = from c in db.Categories
                      select new
                      {
                          CategoryName = c.CategoryName,
                          CategoryID = c.CategoryID,
                          Image = c.Picture,
                      };
           grdCategories.DataSource = cate;
           grdCategories.DataBind();
       }
   }
}
ImageHandler.ashx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Drawing.Imaging;

namespace WebApplication1
{

   public class ImageHandler : IHttpHandler
   {

       public void ProcessRequest(HttpContext context)
       {
           HttpRequest req = context.Request;
           // string categoryID = "1";
           string categoryID = req.QueryString["CategoryID"].ToString();
           // Get information about the specified category
           NorthwindDataContext db = new NorthwindDataContext();
           var category = from c in db.Categories
                          where Convert.ToInt32(c.CategoryID) == Convert.ToInt32(categoryID)
                          select c.Picture;
           int len = category.First().Length;
           // Output the binary data
           // But first we need to strip out the OLE header
           int OleHeaderLength = 78;
           int strippedImageLength = len - OleHeaderLength;
           byte[] imagdata = new byte[strippedImageLength];
           Array.Copy(category.First().ToArray(), OleHeaderLength, imagdata, 0, strippedImageLength);
           if ((imagdata) != null)
           {
               MemoryStream m = new MemoryStream(imagdata);
               System.Drawing.Image image = System.Drawing.Image.FromStream(m);
               image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
           }
       }

       public bool IsReusable
       {
           get
           {
               return false;
           }
       }
   }
}