Thursday, June 17, 2010

DataTable to CSV

private void exportDataTableToCsv(DataTable formattedDataTable, string filename)
{
DataTable toExcel = formattedDataTable.Copy();
HttpContext context = HttpContext.Current;
context.Response.Clear();

foreach (DataColumn column in toExcel.Columns)
{
context.Response.Write(column.ColumnName + ",");
}

context.Response.Write(Environment.NewLine);
foreach (DataRow row in toExcel.Rows)
{
for (int i = 0; i < toExcel.Columns.Count; i++)
{
context.Response.Write(row[i].ToString().Replace(",", string.Empty) + ",");
}

context.Response.Write(Environment.NewLine);
}

context.Response.ContentType = "text/csv";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ".csv");
context.Response.End();
}

Wednesday, June 2, 2010

Apply Styles for row - GridView

protected void grdTemp_RowDataBound(object sender, GridViewRowEventArgs e)
{

//Apply Styles for Header
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[17].Text == "RepeatHeader")
{
e.Row.BackColor = System.Drawing.Color.FromName("#003366");
e.Row.ForeColor = System.Drawing.Color.White;

}
}

Tuesday, June 1, 2010

Format Currency

double dblAmt;
string curCulture = System.Threading.Thread.CurrentThread.CurrentCulture.ToString();
System.Globalization.NumberFormatInfo currencyFormat = new System.Globalization.CultureInfo(curCulture).NumberFormat;
currencyFormat.CurrencyNegativePattern = 1;

if (dsCash.Tables[0].Rows[0]["Amount"].ToString().Trim() != "")

{
dblAmt = Convert.ToDouble(dsCash.Tables[0].Rows[0]["Amount"].ToString());
lblCashAlternatives.Text = dblAmt.ToString("c", currencyFormat);
}
else
{
lblCashAlternatives.Text = "$0.0";
}

Saturday, May 15, 2010

Send Mail in C#

using System.Net.Mail;

MailMessage objMessage = new MailMessage();

objMessage.Bcc.Add(dicInputs["Bcc"].ToString().Trim());
objMessage.From = new MailAddress(dicInputs["From"].ToString().Trim());
objMessage.To.Add(dicInputs["To"].ToString().Trim());
objMessage.Subject = dicInputs["Subject"].ToString().Trim();
objMessage.Body = dicInputs["Message"].ToString().Trim();

string[] sAttachments = null;

sAttachments = dicInputs["Attachments"].ToString().Split(',');

for (int intLoop = 0; intLoop <= sAttachments.GetUpperBound(0); intLoop++)
{
strLogOut = strLogOut + "Adding attachment '" + sAttachments[intLoop] + "'";
objMessage.Attachments.Add(new Attachment(sAttachments[intLoop]));
}


SmtpClient objClient = new SmtpClient();
objClient.Host = "";
objClient.Send(objMessage);

Thursday, May 13, 2010

Thursday, May 6, 2010

DateDiff in Months C#

private static long monthDifference(DateTime startDate, DateTime endDate)
{
long monthsApart = 12 * (startDate.Year - endDate.Year) + startDate.Month - endDate.Month;
return Math.Abs(monthsApart);
}