Search This Blog

Monday 31 December 2012

You work in a company, and need to send an email to a user, and a copy of that email to your manager. Which code should you use?


A.  MailMessage msg = new MailMessage();
msg.From = "from@yourcompany.com";
msg.To = "to@to.com, manager@yourcompany.com";
msg.Subject = "...";
msg.Body = "...";
SmtpClient client = new SmtpClient();
client.Send(msg);
B.  MailMessage msg = new MailMessage("from@yourcompany.com", "to@to.com");
msg.CC.Add(new MailAddress("manager@yourcompany.com"));
msg.Subject = "...";
msg.Body = "...";
SmtpClient client = new SmtpClient();
client.Send(msg);
C.  MailMessage msg = new MailMessage("from@yourcompany.com", "to@to.com");
msg.CC.Add(new MailAddress("manager@yourcompany.com"));
msg.Subject = "...";
msg.Body = "...";
SmtpClient client = new SmtpClient();
msg.Send(client);
D.  MailMessage msg = new MailMessage();
msg.From = "from@yourcompany.com";
msg.To = "to@to.com, manager@yourcompany.com";
msg.Subject = "...";
msg.Body = "...";
SmtpClient client = new SmtpClient();
msg.Send(client);

Wednesday 26 December 2012

Thursday 20 December 2012

Memory Cleaner that forces GC to collect objects in asp.net or Wcf or Window service

The following example demonstrates how to use the Collect method to perform a collection on all generations of memory. The code generates a number of unused objects, and then calls the Collect method to clean them from memory.

[DllImportAttribute("Kernel32.dll", EntryPoint = "SetProcessWorkingSetSize", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
    private static extern int ProcessWorkingSetSize(IntPtr Process, int minimumWorkingSetSize, int maximumWorkingSetSize);
    public void Flushmemory()
    {
        GC.Collect();
        GC.WaitForPendingFinalizers();
        if (Environment.OSVersion.Platform == PlatformID.Win32NT)
        {
            ProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
        }
    }

Monday 17 December 2012

RowDataBound Event of GridView and take footer sum value in asp.net with C#

We are using RowDataBound event to perform summation in footer row.
we will show you how to display a summary or the sum of values in a particular column, in the GridView footer row. Also known as running total of a column, these accumulated figures needs to be displayed below all the pages, if GridView Paging is set as “True”.

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TotalsCnt += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "DataBaseValue")));
                TotaloCnt += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "DataBaseValue")));
                TotalcCnt += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "DataBaseValue")));
                TotalSaCnt += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "DataBaseValue")));
                TotalAmt += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "DataBaseValue")));
                TotalCAmt += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "DataBaseValue")));
                TotalPAmt += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "DataBaseValue")));
                TotalAfAmt += Convert.ToInt32((DataBinder.Eval(e.Row.DataItem, "DataBaseValue")));
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[2].Text = string.Format("{0:N0}", TotalsentCnt);
                e.Row.Cells[3].Text = string.Format("{0:N0}", TotalopenCnt);
                e.Row.Cells[4].Text = string.Format("{0:N0}", TotalclickCnt);
                e.Row.Cells[5].Text = string.Format("{0:N0}", TotalSalesCnt);
                e.Row.Cells[6].Text = string.Format("{0:C2}", TotalAmt);
                e.Row.Cells[7].Text = string.Format("{0:C2}", TotalCustomerAmt);
                e.Row.Cells[8].Text = string.Format("{0:C2}", TotalPublisherAmt);
                e.Row.Cells[9].Text = string.Format("{0:C2}", TotalAffiliateAmt);
            }
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }

Friday 14 December 2012

Remove QueryString value using Asp.net

To clear all query strings, you can invoke the Request.QueryString.Clear();
which will remove all the querystrings at the url
To remove a specific querystring invoke Request.QueryString.Remove("name of the querystring").

Please follow below Example


PropertyInfo isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
            // make collection editable
            isreadonly.SetValue(this.Request.QueryString, false, null);
            // remove
            this.Request.QueryString.Remove("op");

Monday 10 December 2012

How to check a checkbox on gridview using javascript


 <script type="text/javascript">
        function Delete(id) {    
            var frm = document.forms[0];
            for (i = 0; i < frm.elements.length; i++) {
                if (frm.elements[i].type == "checkbox") {
                    if (frm.elements[i].checked) {
                        if (confirm('Are you sure you want to delete the records?')) {
                            return true;
                        } else {
                            for (i = 0; i < frm.elements.length; i++) {
                                frm.elements[i].checked = false;
                            }
                            return false;
                        }
                    }
                }
            }
            alert("Please select a records to delete");
            return false;
        }
    </script>

 <asp:ImageButton ID="ImgBtnDelete" runat="server" ToolTip="Delete"
OnClientClick="javascript:return Delete(this);"/>

Reading Files in Asp.net or C# and Search for a particular text in files in a specific directory


using (StreamReader sr = new StreamReader("FilePath" + "Config.dat"))
{
    sConfigText = sr.ReadToEnd();
}
try
{
objEditvmta = new StringBuilder();
string Pickfile = "identifystring";
if (sConfigText.Contains(Pickfile))
{
   StartPoint = sConfigText.Substring(0, sConfigText.IndexOf(Pickfile)) + Environment.NewLine;
   RemainingText = sConfigText.Substring(StartPoint.Length - 1);
   EntairText = RemainingText.Substring(RemainingText.IndexOf("EndingstringValue") + "EndingstringValue Length");
   objVmta.Append(StartPoint.TrimEnd());
   objVmta.Append(EntairText.TrimEnd());
   sConfigEditData = objVmta.ToString();
   FinalDeletePattern = sConfigEditData;
   FileStream fsPattern = File.OpenWrite("FilePath" + "FileName");
   Encoding encPattern = Encoding.ASCII;
   byte[] restPattern = encPattern.GetBytes(sConfigEditData.ToString());
   fsPattern.Write(restPattern, 0, sConfigEditData.Length);
   fsPattern.Flush();
   fsPattern.Close();
   fsPattern.Dispose();
}
sConfigEditData = string.Empty;
objEditvmta = null;
StartPoint = string.Empty;
RemainingText = string.Empty;
EntairText = string.Empty;
}
catch (Exception ex)
{

thow;

}