Tuesday, January 24, 2012

Simple Error Log Creator


1. Add This as Common in Your Application.

public static void WriteError(string errorMessage)
{
try
{
string path = "~/LogFile/" + DateTime.Today.ToString("dd-MM-yy") + ".txt";
if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))
{
File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close();
}
using (StreamWriter w = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path)))
{
w.WriteLine("\r\nLog Entry : ");
w.WriteLine("{0}", DateTime.Now.ToString(CultureInfo.InvariantCulture));
string err = "Error in: " + System.Web.HttpContext.Current.Request.Url.ToString() +
". Error Message:" + errorMessage;
w.WriteLine(err);
w.WriteLine("__________________________");
w.Flush();
w.Close();
}
}
catch (Exception ex)
{
WriteError(ex.Message);
}
}


2. ErrorLog is as Class Name. (Example --> Below)


How to Convert XML String to XML and Get Values With LINQ


XDocument XMLdoc = XDocument.Parse(XMLString);
try
{
string body;
string CcMailid = "";
string Path = Server.MapPath("~/Imgs/Logo.gif");
string rootUrl = Page.Request.ServerVariables["HTTP_HOST"].ToString() + RequestDetails;

var XMLVales = (from _Doc in XMLdoc.Descendants("Product")
select new
{
ProductName = _Doc.Element("ProductName").Value,
Quantity = _Doc.Element("ProductQuanity").Value,
Amount = _Doc.Element("ProductAmt").Value,
TotalAmount = _Doc.Element("ProductTotalAmt").Value
}).Last();

body = ObjHtmlBody.BodyContentNew(RequesterName, SubmitterName, ReqId, XMLVales.ProductName, XMLVales.Quantity, XMLVales.Amount, XMLVales.TotalAmount, rootUrl);
string[] _ReqCode = ddlRequestFrom.SelectedValue.Split('^');
if (Convert.ToString(Session["EmpCode"]) != Convert.ToString(_ReqCode[0]))
{
CcMailid = _ReqCode[1].Trim();
}
ObjSendMail.SendEmail(ToMailId, CcMailid, "New Requst", body, Path);
}
catch (Exception Ex)
{
ErrorLog.WriteError("FrmRequest() --> : buildAndSendEmail() : --> " + Ex);
}

Friday, January 6, 2012

How to check IN Clause Condition in LINQ


List<int> ObjCircleList = null;
ObjCircleList = new List<int>();

for (int i = 0; i < ddlCircle.SelectedValue.Split(',').Count(); i++)
{
ObjCircleList.Add(Convert.ToInt32(ddlCircle.SelectedValue.Split(',')[i]));
}

string CentreNames = "", CentreIds = "";

foreach (Center c in Dbi.Contactss.ListContacts(db, 10).Where(p => ObjCircleList.Contains(p.CorpCenterId)).OrderBy(p => p.Center_name))
{

CentreNames = CentreNames + c.Center_name.Trim().TrimEnd() + "|";
CentreIds = CentreIds + c.id.ToString() + "|";
}