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:
<table style="height: 299px; width: 389px">
<tr>
<td>Select File:</td>
<td><asp:FileUpload ID="FileUploadExcel" runat="server" meta:resourcekey="FileUploadExcelResource1" /></td>
</tr>
<tr>
<td colspan="2" class="auto-style1"><asp:Button ID="BtnUpload" runat="server" Text="Upload" OnClick="BtnUpload_Click" meta:resourcekey="BtnUploadResource1" /></td>
</tr>
<tr>
<td>Subject:</td>
<td><asp:TextBox ID="TxtSubject" runat="server" meta:resourcekey="TxtSubjectResource1"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2">
<asp:GridView ID="DataGridView" runat="server" AutoGenerateColumns="False" Height="78px" Width="349px" meta:resourcekey="DataGridViewResource1">
<Columns>
<asp:TemplateField HeaderText="Name" meta:resourcekey="TemplateFieldResource1">
<ItemTemplate>
<asp:Label ID="LblName" runat="server" Text='<%# Bind("F1") %>' meta:resourcekey="LblNameResource1"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email ID" meta:resourcekey="TemplateFieldResource2">
<ItemTemplate>
<asp:Label ID="LblEmail" runat="server" Text='<%# Bind("F2") %>' meta:resourcekey="LblEmailResource1"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td>
<asp:Button ID="BtnSendMail" runat="server" Text="SendMail" OnClick="BtnSendMail_Click" meta:resourcekey="BtnSendMailResource1" />
</td>
<td>
<asp:Label ID="LblMessage" runat="server" meta:resourcekey="LblMessageResource1"></asp:Label>
</td>
</tr>
</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:
using System;
using System.Data;
using System.Data.OleDb;
namespace SendExcelApp
{
public class DAL
{
OleDbDataAdapter DbAdap;
DataTable dt;
internal object Get_File(string MyFile)
{
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);
OleDbConnection DbCon = new OleDbConnection(connString);
DbAdap = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", DbCon);
dt = new DataTable();
DbAdap.Fill(dt);
return dt;
}
}
}
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()
string MyFile;
DAL obj = new DAL();
- Modify the Page_Load() event with the code below:
protected void Page_Load(object sender, EventArgs e)
{
LblMessage.Visible = false;
BtnSendMail.Enabled = false;
}
- 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:
protected void BtnUpload_Click(object sender, EventArgs e)
{
UploadFile();
BtnSendMail.Enabled = true;
}
void UploadFile()
{
HttpPostedFile PostedFile = Request.Files["FileUploadExcel"];
if (PostedFile != null && PostedFile.ContentLength > 0)
{
MyFile = Path.GetFileName(PostedFile.FileName);
PostedFile.SaveAs(Server.MapPath(Path.Combine("~/Data/", MyFile)));
Get_Data(MyFile);
}
else
{
LblMessage.Text = "Missing File";
LblMessage.Visible = true;
}
}
private void Get_Data(string MyFile)
{
DataGridView.DataSource = obj.Get_File(MyFile);
DataGridView.DataBind();
}
Step 4: Run the application and after uploading the Excel file the web form will look like the following screenshot:
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
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
Step 3: Now Rename the string as MyTemplate and add an html code in the value as shown in the following screenshot:
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 :
private string ReadTemplate(string name, string subject)
{
string resourceName = (string)HttpContext.GetGlobalResourceObject("Resource1", "MyTemplate");
string mystring = "";
mystring = resourceName;
mystring = mystring.Replace("$$Member$$", name);
mystring = mystring.Replace("$$Subject$$", TxtSubject.Text);
return mystring;
}
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:
</system.web>
<appSettings>
<add key="FromMail" value="Your-Email-ID"/>
<add key="Password" value="Your-Email-Password"/>
<add key="Host" value="smtp.gmail.com"/>
</appSettings>
Step 2: Create an event for SendMail Button and code to email with the help of following code:
protected void BtnSendMail_Click(object sender, EventArgs e)
{
Send_Mail();
}
void Send_Mail()
{
try
{
string Pass, FromEmailid, HostAdd;
foreach (GridViewRow gr in DataGridView.Rows)
{
HostAdd = ConfigurationManager.AppSettings["Host"].ToString();
FromEmailid = ConfigurationManager.AppSettings["FromMail"].ToString();
Pass = ConfigurationManager.AppSettings["Password"].ToString();
Label LblName = gr.FindControl("LblName") as Label;
Label LblMail = gr.FindControl("LblEmail") as Label;
string Name = LblName.Text;
string Mail = LblMail.Text;
string subject = TxtSubject.Text;
SmtpClient client = new SmtpClient();
MailMessage msg = new MailMessage();
NetworkCredential credentials = new NetworkCredential(FromEmailid, Pass);
client.Host = HostAdd;
client.Port = 25;
client.UseDefaultCredentials = false;
client.Credentials = credentials;
client.EnableSsl = true;
MailAddress from = new MailAddress(FromEmailid);
msg.IsBodyHtml = true;
msg.Subject = subject;
msg.Body = ReadTemplate(Name, subject);
msg.To.Add(Mail);
msg.From = from;
client.Send(msg);
LblMessage.Text = "Email Send Successfully";
LblMessage.Visible = true;
}
}
catch (Exception Ex)
{
LblMessage.Text = Ex.Message;
LblMessage.Visible = true;
}
}
Step 3: Run the application. At first select the Excel file to upload.
Now click on the SendMail button.
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.