Friday, August 31, 2012

File downloading in ASP.Net using C# code

using System.Net;
protected void btnDowmLoadFile_Click(object sender, EventArgs e)
{
    try
    {
        string strUrl = “path/filename.xls”;
        WebClient request=new WebClient();
        HttpResponse response = HttpContext.Current.Response;
        response.Clear();
        response.ClearContent();
        response.ClearHeaders();
        response.Buffer= true;
        response.AddHeader("Content-Disposition","attachment;filename=\"" + Server.MapPath(strUrl) + "\"");
        byte[] dataFile= request.DownloadData(Server.MapPath(strUrl));
        response.BinaryWrite(dataFile);
        response.End();
    }
    catch(Exception ex)
    {
     }
}

sending email with attachment file in .net


First Step, we have ASP.NET applications you configure this in your application’s web.config file.
Config code
  <system.net>
    <mailSettings>
      <smtp from="anil.singh@gmail.com" deliveryMethod="Network">
        <network host="smtp.gmail.com" port="587" userName=anil.singh@gmail.com password="password#123" defaultCredentials="false"/>
      </smtp>
    </mailSettings>
  </system.net>

Second Step, c# code
using System.Net.Mail;
protected void btnMailSend_Click(object sender, EventArgs e)
        {
            try
            {
                string contributionFile = string.Empty;
                if (flAttachment.HasFile && Path.GetExtension(flAttachment.FileName) == ".xls")
                {
                    flAttachment.SaveAs(Server.MapPath("~/FolderPath/") + flAttachment.FileName);
contributionFile = Server.MapPath("~/FolderPath/") + flAttachment.FileName;
                }
                MailMessage mm = new MailMessage();
                mm.From = new MailAddress("anil.singh@gmail.com");

                mm.To.Add(txtMailTo.Text);
                mm.Subject = "This is your File" + monthName;
                mm.Body = txtMailMessage.Text;
                mm.IsBodyHtml = true;
                mm.Priority = MailPriority.High;
                Attachment attach = new Attachment(contributionFile);
                mm.Attachments.Add(attach);
                SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                client.EnableSsl = true;
                client.Credentials = new NetworkCredential("anil.singh@gmail.com", "password#123");
                client.Send(mm);
                client.Timeout = 120;
            }
            catch (Exception ex)
            {

            }
        }




If you want to send this mail to Different person at a time, then we write to this..

using System.Net.Mail;
protected void btnMailSend_Click(object sender, EventArgs e)
        {
            try
            {
                string contributionFile = string.Empty;
                if (flAttachment.HasFile && Path.GetExtension(flAttachment.FileName) == ".xls")
                {
                    flAttachment.SaveAs(Server.MapPath("~/FolderPath/") + flAttachment.FileName);
contributionFile = Server.MapPath("~/FolderPath/") + flAttachment.FileName;
                }
                MailMessage mm = new MailMessage();
                mm.From = new MailAddress("anil.singh@gmail.com");

  mm.To.Add(new MailAddress("hrTeam@gmail.com"));
  mm.To.Add(new MailAddress("adminTeam@gmail.com"));
  mm.To.Add(new MailAddress("Manager@gmail.com")); 
  mm.CC.Add(new MailAddress("Boss@gmail.com"));
                mm.Subject = "This is your File" + monthName;
                mm.Body = txtMailMessage.Text;
                mm.IsBodyHtml = true;
                mm.Priority = MailPriority.High;
                Attachment attach = new Attachment(contributionFile);
                mm.Attachments.Add(attach);
                SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                client.EnableSsl = true;
                client.Credentials = new NetworkCredential("anil.singh@gmail.com", "password#123");
                client.Send(mm);
                client.Timeout = 120;
            }
            catch (Exception ex)
            {

            }
        }


Thanks,
Anil

Wednesday, August 15, 2012

MVC 4 .Net


Model View Controller is a design pattern.

MVC applications are divided into three categories.

1.  Model
2.  View
3.  Controller

Model: Responsible for Data related actions. It contains information related with data and validations.

View: View is the HTML UI.

Controller: Will responsible to pass the query from model to view and again. In simple word it contains the information about the code flow. It plays an important role between Model & View.

Features of MVC 4.0
  1.       ASP.NET Web API.
  2.       Refreshed and modernized default project templates.
  3.       New mobile project template.
  4.       Many new features to support mobile apps.
  5.       Enhanced support for asynchronous methods.
  6.       Read the full feature list in the release notes.

ASP.NET Web API
ASP.NET Web API is a framework that makes it easy Build HTTP services that reach a broad range of clients, including browsers and mobile devices.

Before you start, make sure you've installed the prerequisites listed below.

Find new notes about MVC 4.0, Microsoft released, and MVC 4.0 ReleaseNotes of ASP.NET MVC 4.0.
 
For Building More scalable web site. You can Install MVC 4.0 viaweb platform.
  

Some imp links for MVC 4.0 are 




The different between .net 2010 and .net 2012.


.NET 3.0 was .NET 2.0 SP1 plus some additional assemblies. If you installed it, your .NET 2.0 applications would start using the service pack versions.

.NET 3.5 was .NET 2.0 SP2 plus .NET 3.0 SP1 plus some additional assemblies. If you installed it, your .NET 2.0 applications and .NET 3.0 applications would be using the service pack versions.

The big difference is that .NET 3.0 and .NET 3.5 was avalible to all OS that supported .NET 2.0.

.NET 4.0 used a different CLR, so I don't know that it upgraded any of the .NET 2.0, 3.0 or 3.5 assemblies.

It appears that .NET 4.5 is something like .NET 4.0 SP1 plus some additional assemblies.



The only configuration for .net 4.0 (but not XP) using Visual Studio 2012.

Machine1

Install Visual Studio 2012 (and by extension .net 4.5)
Windows 7 (Or Higher)
Code Here

Notes:

This is not a Windows XP solution.  Just .net 4.0
You have to resist the temptation to just debug on your Machine1

Tuesday, August 14, 2012

How to validate US Zip Code in C#.net

       
      /// <summary>
        /// This Text event is use for USA-Zip code validation in C#.Net.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtZipcode_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtZipcode.Text))
            {
                string replaceLength = txtZipcode.Text;
                txtZipcode.Text = replaceLength.Replace("-", "");
                string findLength = txtZipcode.Text;
                int length = findLength.Length;
                if (length >= 5 && length <= 11)
                {
                    if (length == 5)
                    {
                        txtZipcode.Text = txtZipcode.Text + "-0000";
                    }
                    else
                    {
                        txtZipcode.Text = txtZipcode.Text.Substring(0, 5) + "-" +
                                            txtZipcode.Text.Substring(5, txtZipcode.Text.Length - 5);
                        string zipL = txtZipcode.Text;
                        int zipLength = zipL.Length;
                        if (zipLength == 10)
                        {
                            txtZipcode.Text = txtZipcode.Text.Substring(0, 10);
                        }
                        else
                        {
                            for (int i = 0; i < 10 - zipLength; i++)
                            {
                                txtZipcode.Text += "0";
                            }
                        } 
                    }
                     lblZip.Visible = false;
                }
                else
                {
                    lblZip.Visible = true;
                    lblZip.Text = "Invalid Zip!";
                    txtZipcode.Text = string.Empty;
                }
         }
  }