Accessing Resource File Key and Email In ASP.Net Application

Introduction

Today we'll discuss about to access the resource file content or we can say any string in the ASP.NET. In these days I am working, on a project and in that I have to access the content stored in the resource file which is created in the App_GlobalResource (ASP.NET Folder).

In that context, we'll proceed here with the following sections:

  • Create ASP.NET Application
  • Connect ASP.NET to Excel
  • Working With App_GlobalResource
  • Emailing

Create ASP.NET Application

In this section, we'll create an application by using the following steps:

Step 1: Create a new ASP.NET Application with an Empty Project Template as named SendExcelApp

Step 2: Now create a Web Form and add the following code in the body:

  1. <table style="height: 299px; width: 389px">  
  2.     <tr>  
  3.         <td>Select File:</td>  
  4.         <td><asp:FileUpload ID="FileUploadExcel" runat="server" meta:resourcekey="FileUploadExcelResource1" /></td>  
  5.     </tr>  
  6.     <tr>  
  7.         <td colspan="2" class="auto-style1"><asp:Button ID="BtnUpload" runat="server" Text="Upload" OnClick="BtnUpload_Click" meta:resourcekey="BtnUploadResource1" /></td>  
  8.     </tr>  
  9.     <tr>  
  10.         <td>Subject:</td>  
  11.         <td><asp:TextBox ID="TxtSubject" runat="server" meta:resourcekey="TxtSubjectResource1"></asp:TextBox></td>  
  12.     </tr>  
  13.     <tr>  
  14.         <td colspan="2">  
  15.             <asp:GridView ID="DataGridView" runat="server" AutoGenerateColumns="False" Height="78px" Width="349px" meta:resourcekey="DataGridViewResource1">  
  16.                 <Columns>  
  17.                     <asp:TemplateField HeaderText="Name" meta:resourcekey="TemplateFieldResource1">  
  18.                         <ItemTemplate>  
  19.                             <asp:Label ID="LblName" runat="server" Text='<%# Bind("F1") %>' meta:resourcekey="LblNameResource1"></asp:Label>  
  20.                         </ItemTemplate>  
  21.                    </asp:TemplateField>  
  22.                     <asp:TemplateField HeaderText="Email ID" meta:resourcekey="TemplateFieldResource2">  
  23.                         <ItemTemplate>  
  24.                             <asp:Label ID="LblEmail" runat="server" Text='<%# Bind("F2") %>' meta:resourcekey="LblEmailResource1"></asp:Label>  
  25.                         </ItemTemplate>  
  26.                     </asp:TemplateField>  
  27.                 </Columns>  
  28.             </asp:GridView>  
  29.         </td>  
  30.     </tr>  
  31.     <tr>  
  32.         <td>  
  33.             <asp:Button ID="BtnSendMail" runat="server" Text="SendMail" OnClick="BtnSendMail_Click" meta:resourcekey="BtnSendMailResource1" />  
  34.         </td>  
  35.         <td>  
  36.             <asp:Label ID="LblMessage" runat="server" meta:resourcekey="LblMessageResource1"></asp:Label>  
  37.         </td>  
  38.     </tr>  
  39. </table> 

Connect ASP.NET to Excel

In this section we'll connect the Excel file to the application. Proceed with the following steps:

Step 1: Create a folder named Data in the project

Step 2: Add a class named DAL and replace the code with the code below:

  1. using System;  
  2. using System.Data;  
  3. using System.Data.OleDb;  
  4. namespace SendExcelApp  
  5. {  
  6.     public class DAL  
  7.     {  
  8.         OleDbDataAdapter DbAdap;  
  9.         DataTable dt;  
  10.         internal object Get_File(string MyFile)  
  11.         {  
  12.             string connString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=(Your Application Path)/Data/{0} ;Extended Properties=\"Excel 12.0;HDR=NO;IMEX=1\";", MyFile);  
  13.             OleDbConnection DbCon = new OleDbConnection(connString);  
  14.             DbAdap = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", DbCon);  
  15.             dt = new DataTable();  
  16.             DbAdap.Fill(dt);  
  17.             return dt;  
  18.         }  
  19.     }  
  20. } 

Note: You must have the Office driver to access the Excel file. Please refer Excel Connectivity

Step 3: In the WebForm.cs file, we'll upload the Excel file and bind it to the Grid View. Proceed with the following sections:

  • Add the following code above the Page_Load()
    1. string MyFile;  
    2. DAL obj = new DAL();
  • Modify the Page_Load() event with the code below:
    1. protected void Page_Load(object sender, EventArgs e)  
    2. {  
    3.     LblMessage.Visible = false;  
    4.     BtnSendMail.Enabled = false;  
    5. }
  • Now we'll create an event for the Upload button which is used to upload the Excel file and then bind it to the Grid View by using the following code:
    1. protected void BtnUpload_Click(object sender, EventArgs e)  
    2. {  
    3.     UploadFile();  
    4.     BtnSendMail.Enabled = true;  
    5. }  
    6. void UploadFile()  
    7. {  
    8.     HttpPostedFile PostedFile = Request.Files["FileUploadExcel"];  
    9.     if (PostedFile != null && PostedFile.ContentLength > 0)  
    10.     {  
    11.         MyFile = Path.GetFileName(PostedFile.FileName);  
    12.         PostedFile.SaveAs(Server.MapPath(Path.Combine("~/Data/", MyFile)));  
    13.         Get_Data(MyFile);  
    14.     }  
    15.     else  
    16.     {  
    17.         LblMessage.Text = "Missing File";  
    18.         LblMessage.Visible = true;  
    19.     }  
    20. }  
    21. private void Get_Data(string MyFile)  
    22. {  
    23.     DataGridView.DataSource = obj.Get_File(MyFile);  
    24.     DataGridView.DataBind();  
    25. }

Step 4: Run the application and after uploading the Excel file the web form will look like the following screenshot:

Get Excel in WebForm

Working With App_GlobalResource

In this section, we'll add an ASP.NET Folder named App_GlobalResource and in which we'll add a Resource1.resx file to create a string. Proceed the steps given below:

Step 1: Just right click on your project to add an App_GlobalResource

Adding App_GloabalResource in WebApp

Note: The folder is already exist on my project, so that it's unable to show.

Step 2: Now, add a Resources file named Resource1.resx

Adding resource File

Step 3: Now Rename the string as MyTemplate and add an html code in the value as shown in the following screenshot:

Working with Resource File

Step 4: Now in this step we'll add the code to read the key (MyTemplate) and replace it with the UserName and subject. Create the following method in your class :

  1. private string ReadTemplate(string name, string subject)  
  2. {  
  3.     string resourceName = (string)HttpContext.GetGlobalResourceObject("Resource1""MyTemplate");  
  4.     string mystring = "";  
  5.     mystring = resourceName;  
  6.     mystring = mystring.Replace("$$Member$$", name);  
  7.     mystring = mystring.Replace("$$Subject$$", TxtSubject.Text);  
  8.     return mystring;  
  9. } 

In the above code, we mentioned the key and the resource name in the GetGlobalResourceObject() and store it in the string varibale.

Emailing

Now in this section we'll use the body of the resource key as a email body and email it. Proceed with the following steps:

Step 1: At first we'll configure the Web.Config file. Modify the Web.Config file with the following code:

  1. <appSettings>  
  2.   <add key="FromMail" value="Your-Email-ID"/>  
  3.   <add key="Password" value="Your-Email-Password"/>  
  4.   <add key="Host" value="smtp.gmail.com"/>  
  5. </appSettings> 

Step 2: Create an event for SendMail Button and code to email with the help of following code:

  1. protected void BtnSendMail_Click(object sender, EventArgs e)  
  2. {  
  3.     Send_Mail();  
  4. }  
  5. void Send_Mail()  
  6. {  
  7.     try  
  8.     {  
  9.         string Pass, FromEmailid, HostAdd;  
  10.         foreach (GridViewRow gr in DataGridView.Rows)  
  11.         {  
  12.             HostAdd = ConfigurationManager.AppSettings["Host"].ToString();  
  13.             FromEmailid = ConfigurationManager.AppSettings["FromMail"].ToString();  
  14.             Pass = ConfigurationManager.AppSettings["Password"].ToString();  
  15.             Label LblName = gr.FindControl("LblName"as Label;  
  16.             Label LblMail = gr.FindControl("LblEmail"as Label;  
  17.             string Name = LblName.Text;  
  18.             string Mail = LblMail.Text;  
  19.             string subject = TxtSubject.Text;  
  20.             SmtpClient client = new SmtpClient();  
  21.             MailMessage msg = new MailMessage();  
  22.             NetworkCredential credentials = new NetworkCredential(FromEmailid, Pass);  
  23.             client.Host = HostAdd;  
  24.             client.Port = 25;  
  25.             client.UseDefaultCredentials = false;  
  26.             client.Credentials = credentials;  
  27.             client.EnableSsl = true;  
  28.             MailAddress from = new MailAddress(FromEmailid);  
  29.             msg.IsBodyHtml = true;  
  30.             msg.Subject = subject;  
  31.             msg.Body = ReadTemplate(Name, subject);  
  32.             msg.To.Add(Mail);  
  33.             msg.From = from;  
  34.             client.Send(msg);  
  35.             LblMessage.Text = "Email Send Successfully";  
  36.             LblMessage.Visible = true;  
  37.         }  
  38.     }  
  39.     catch (Exception Ex)  
  40.     {  
  41.         LblMessage.Text = Ex.Message;  
  42.         LblMessage.Visible = true;  
  43.     }  
  44. }   

Step 3: Run the application. At first select the Excel file to upload.

Binding Excel File in ASP.NET

Now click on the SendMail button.

Emailing in ASP.NET

Summary

So far this article will help you to connect the Excel file to the ASP.NET Application. You can also access the Resource file Key stored in the App_GlobalResource in the web application and use the html body as an email body and emailing concept. Happy Coding and thanks for reading.


Similar Articles