Asp.Net XML Creation

public void xmlWrite(string invoid, string date,
string soldprice, string groupid, string cid, string uid)
{
XmlDocument xmlDoc = new XmlDocument();
XmlNode rootNode = xmlDoc.CreateElement("invoice-"+ invoid);
xmlDoc.AppendChild(rootNode);

XmlNode userNode = xmlDoc.CreateElement("IssureDate");
userNode.InnerText = date;
rootNode.AppendChild(userNode);

XmlNode userNode2 = xmlDoc.CreateElement("SoldPrice");
userNode2.InnerText = soldprice;
rootNode.AppendChild(userNode2);

XmlNode userNode3 = xmlDoc.CreateElement("GroupID");
userNode3.InnerText = groupid;
rootNode.AppendChild(userNode3);

XmlNode userNode4 = xmlDoc.CreateElement("CustomerID");
userNode4.InnerText = cid;
rootNode.AppendChild(userNode4);
XmlNode userNode5 = xmlDoc.CreateElement("SystemUserID");
userNode5.InnerText = uid;
rootNode.AppendChild(userNode5);

xmlDoc.Save(Server.MapPath("~")+"/XMlDOcs/invoice"+invoid+".xml");

}

 

Asp.Net MongoDB connection

using MongoDB.Driver;

//You can install this using NuGet packages : browsing mongodb driver

public void writeMongoDb(string invoid, string date,
string soldprice, string groupid, string cid, string uid)
{
var client = new MongoClient();
var connectionString = "mongodb://localhost:27017";
client = new MongoClient(connectionString);
IMongoDatabase db = client.GetDatabase("ADBMS");
invoice invoice = new invoice
{
invoiceno = invoid,
date = date,
soldprice = soldprice,
groupid = invoid,
cid = cid,
uid = uid
};
var collection = db.GetCollection("invoice");
collection.InsertOne(invoice);
}

 

public class invoice
{
public string invoiceno { get; set; }
public string date { get; set; }
public string soldprice { get; set; }
public string groupid { get; set; }
public string cid { get; set; }
public string uid { get; set; }

}

 

Encrypt & Decrypt App.config in C#

• Rename App.config file to web.config
• Run Command prompt as admin:
For encrypt:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pef “connectionStrings” your project location within quotes and -prov “DataProtectionConfigurationProvider”
Ex:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pef “connectionStrings” “D:\location\location1\location” -prov “DataProtectionConfigurationProvider” 
For Decrypt:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pdf “connectionStrings” your project location within quotes.
Ex:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pdf “connectionStrings” “D:\location1\location” 

Send a Mail using C#

public static void Send(string To, string cc, string Msg, string heading)
{
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("YOUR_MAIL@MYMAIL.COM", "Password");
client.Port = 00;//Your port
client.Host = "00.00.00.0";//Your host
client.DeliveryMethod = SmtpDeliveryMethod.Network;

MailMessage message = new MailMessage();
AlternateView altView = AlternateView.CreateAlternateViewFromString(Msg, null,MediaTypeNames.Text.Html);

message.To.Add(To);

if (cc != "")
{
message.CC.Add(cc);
}
message.From = new MailAddress("YouMail@Mail.com");
message.Subject = heading;
message.Body = Msg;
message.IsBodyHtml = true;

try
{
client.Send(message);
}
catch (Exception ee)
{
throw ee;
}

}

 

XL sheet download in C#

FileInfo file = new FileInfo(PathToExcelFile);
if (file.Exists)
{
   Response.Clear();
   Response.ClearHeaders();
   Response.ClearContent();
   Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
   Response.AddHeader("Content-Type", "application/Excel");
   Response.ContentType = "application/vnd.xls";
   Response.AddHeader("Content-Length", file.Length.ToString());
   Response.WriteFile(file.FullName);
   Response.End();
}
else
{
   Response.Write("This file does not exist.");
}

Restrict direct access (URL) to html page using JavaScript

LoginPage.html

localStorage.setItem(“SecondPageAccess”,”true”);
window.location.href = “SecondPage.html”;

SecondPage.html

//Add this inside page head

window.onload = Validate;

function Validate() {
var valid = localStorage.getItem(“SecondPageAccess”);
if (valid == “true”) {
//Stay page
localStorage.setItem(“signupLoad”, “false”);
}
else {

window.location.href = “LoginPage.html”;
}