Thursday, 13 December 2012

Calender Control in asp.net


<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/themes/base/jquery-ui.css"
        type="text/css" media="all" />
    <link rel="stylesheet" href="http://static.jquery.com/ui/css/demo-docs-theme/ui.theme.css"
        type="text/css" media="all" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.min.js"
        type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("#<%= txtFrom.ClientID  %>").datepicker();
            $("#<%= txtTo.ClientID  %>").datepicker();
        });
       
    </script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">   
    <p>
        <asp:TextBox ID="txtFrom" runat="server"></asp:TextBox></p>
    <p>
        <asp:TextBox ID="txtTo" runat="server"></asp:TextBox></p>

</asp:Content>

Modify the gridview row data in asp.net


 protected void gvOpenings_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRow row = ((DataRowView)e.Row.DataItem).Row;
                //int i=e.Row.RowIndex;
                for (int i = 0; i <8 ; i++)
                {
                    if (e.Row.Cells[i].Text.Length > 30)
                    {
                        string strStore = e.Row.Cells[i].Text;
                        e.Row.Cells[i].Text = strStore.Substring(0, 27) + "..";
                    }
                }
            }
        }

send mail with attachment


string strSMTPServer = ConfigurationManager.AppSettings["SMTPServer"];
                string strFromAddress = ConfigurationManager.AppSettings["FromAddress"];
                string strUserName = ConfigurationManager.AppSettings["MailCredential_UserName"];
                string strPassword = ConfigurationManager.AppSettings["MailCredential_Password"];
                int intSMTPPort = Convert.ToInt32(ConfigurationManager.AppSettings["SMTPPort"]);
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient(strSMTPServer);
                if (fileResume.HasFile == true)
                {
                    mail.Attachments.Add(new Attachment(fileResume.PostedFile.InputStream, fileResume.PostedFile.FileName));
                    mail.From = new MailAddress(strFromAddress);
                    mail.To.Add(ConfigurationManager.AppSettings["MailToAddress"]);
                    String strAppliedFor=Request.QueryString["jobid"]!=null?Request.QueryString["jobid"]:"General";
                    mail.Subject = "Applied For " + ddlApplyOne.SelectedValue;
                    mail.IsBodyHtml = true;
                    StringBuilder mailBody = new StringBuilder();
                    mailBody.AppendFormat("<div style='width:50px;backround-color:Green'><table border='10' bordercolor='red' cellpadding='10' cellspacing='10'  align='center'>");
                    mailBody.AppendFormat("<tr><td colspan='2' align='center'><h1>Application for the post of {0}</h1></td></tr>", ddlApply.SelectedValue);
                    mailBody.AppendFormat("<tr><td>Name of the Candidate:</td><td>{0}</td></tr>",txtName.Text);
                    mailBody.AppendFormat("<tr><td>EmailID:</td><td>{0}</td></tr>",txtEmail.Text);
                    mailBody.AppendFormat("<tr><td>Contact Ph:</td><td>{0}</td></tr>",txtContact.Text);
                    mailBody.AppendFormat("<tr><td>Educational Qualification:</td><td>{0}</td></tr>",txtEduQu.Text);
                    mailBody.AppendFormat("<tr><td>Technical Skils:</td><td>{0}</td></tr>",txtSkills.Text);
                    mailBody.AppendFormat("<tr><td>Work Experience:</td><td>{0}</td></tr>", ddlExp.SelectedValue);
                    mailBody.AppendFormat("<tr><td>Current Working Status:</td><td>{0}</td></tr>", txtWorkStat.Text);
                    mailBody.AppendFormat("</table></div>");
                    mail.Body = mailBody.ToString();
                    SmtpServer.Port = intSMTPPort;
                    SmtpServer.Credentials = new System.Net.NetworkCredential(strUserName, strPassword);
                    SmtpServer.EnableSsl = true;
                    SmtpServer.Send(mail);
                    txtEmail.Text = "";
                    txtName.Text = "";
                    txtSkills.Text = "";
                    txtWorkStat.Text = "";
                    txtContact.Text="";
                    txtEduQu.Text="";
                    txtWorkStat.Text="";
                    ddlApply.SelectedIndex=0;
                    ddlExp.SelectedIndex=0;
                    lblError.Text = "";
                    ScriptManager.RegisterStartupScript(this,this.GetType(),"MessageBox","alert('Your details are updated to our system. We will get back to you soon..Thank you');",true);

Messagebox in asp.net page

 ScriptManager.RegisterStartupScript(this,this.GetType(),"MessageBox","alert('Your details are updated to our system. We will get back to you soon..Thank you');",true);

Tuesday, 11 December 2012

Mail Sending in C#


Mail Sending in C#


                    string strSMTPServer = ConfigurationManager.AppSettings["SMTPServer"];//smtp server url
                    string strFromAddress = ConfigurationManager.AppSettings["FromAddress"];
//From Address of Mail
                    string strUserName = ConfigurationManager.AppSettings["MailCredential_UserName"];
                    string strPassword = ConfigurationManager.AppSettings["MailCredential_Password"];
                    int intSMTPPort = Convert.ToInt32(ConfigurationManager.AppSettings["SMTPPort"]);
                    MailMessage mail = new MailMessage();
                    SmtpClient SmtpServer = new SmtpClient(strSMTPServer);
                    mail.From = new MailAddress(strFromAddress);
                    mail.To.Add(ConfigurationManager.AppSettings["ToAddress"]);
                    mail.Subject = "Subject of Mail here";
                    mail.Body = "Body of mail here" ;
                    SmtpServer.Port = intSMTPPort;
                    SmtpServer.Credentials = new System.Net.NetworkCredential(strUserName, strPassword);
                    SmtpServer.EnableSsl = true;
                    SmtpServer.Send(mail);
                    lblLoginError.Text = "Password Sent Successfully";

Monday, 10 December 2012

IIS 7.5 error: Handler “PageHandlerFactory-Integrated” has a bad module “ManagedPipelineHandler” in its module list


IIS 7.5 error: Handler “PageHandlerFactory-Integrated” has a bad module “ManagedPipelineHandler” in its module list
Posted on August 12, 2010 by Maris
Today I installed Clean Windows Web Server 2008 R2 64-bit with IIS 7.5. To my surprise opening .NET 4.0 application I received the following error:
IIS 7.5 Detailed Error - 500.21 - Internal Server Error






As it turns out, some glitch caused this problem, and somehow .NET was not registered with IIS.
Running the following commands solved this issue:
%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i
%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe -i

Unique visitor counter in asp.net


In global.asax file 


<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="HitCounterControl.ascx.cs" Inherits="Rr.HitCounterControl" %>


<div style="padding-top: 15px; text-align: center">
    <span style="font-size:small">Visitor Count </span>
    <asp:Literal ID="ltlCounter" runat="server"></asp:Literal>
</div>





 void Session_Start(object sender, EventArgs e)
        {                    
         

//Get & increment visitor count
            int count = 0;
            DataSet tmpDs = new DataSet();
            HttpContext currentContext = HttpContext.Current;
            if (currentContext != null)
            {              
                try
                {
                    tmpDs.ReadXml(Server.MapPath("~/HitCounter.xml"));
                    int i = 0;
                    String ipAddress = currentContext.Request.UserHostAddress;
                    Session["ipAddress"] = ipAddress;
                    while (tmpDs.Tables[0].Rows[i]["ipAddress"].ToString() != null)
                    {
                        if (ipAddress == tmpDs.Tables[0].Rows[i]["ipAddress"].ToString())
                        {
                            count++;
                        }
                        i++;
                    }
                }
                catch (System.IO.FileNotFoundException)
                {
                    throw;
                }
                catch
                {
                    //no need to throw exception for this
                }
            }
            if (currentContext != null && count==0)
            {
                try
                {
                    Session["Start"] = DateTime.Now;
                    //UserVisitCount();
                    // DataSet tmpDs = new DataSet();
                    //read hit count
                    tmpDs.ReadXml(Server.MapPath("~/HitCounter.xml"));
                    tmpDs.Tables[0].Rows.Add(tmpDs.Tables[0].Rows.Count + 1, currentContext.Request.UserHostAddress, DateTime.Now.ToString(), currentContext.Session.SessionID);
                    tmpDs.WriteXml(Server.MapPath("~/HitCounter.xml"));
                    //set in Session                  
                }
                catch { }
            }


            Session["HitCount"] = tmpDs.Tables[0].Rows.Count;


}




In user control






using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;


namespace RR
{
    public partial class HitCounterControl : System.Web.UI.UserControl
    {
         int totalDigits = 6;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ltlCounter.Text = FormatHTMLCounter(Session["HitCount"].ToString());
            }
        }

       /// <summary>
       /// Calculates and formats the vistor count number
       /// </summary>
       /// <param name="visitCount"></param>
       /// <returns></returns>
        private string FormatHTMLCounter(string visitCount)
        {
            int noOfDigits = visitCount.Length;
            int zeroesToPrefix = totalDigits - noOfDigits;
            StringBuilder strCounterHtml = new StringBuilder();
         
            strCounterHtml.Append(@"<table align=""center"" style=""width: 120px; background-color: Black; color: White; text-align: center; font-weight: bold;""><tr>");

            for (int i = 0; i < zeroesToPrefix; i++)
            {
                strCounterHtml.Append(@"<td style=""border:1px solid white; padding:2px"">");
                strCounterHtml.Append("0");
                strCounterHtml.Append("</td>");
            }

            char[] visitCountChar = visitCount.ToCharArray();
            for (int i = 0; i < visitCountChar.Length; i++)
            {
                strCounterHtml.Append(@"<td style=""border:1px solid white; padding:2px"">");
                strCounterHtml.Append(visitCountChar[i]);
                strCounterHtml.Append("</td>");
            }

            strCounterHtml.Append("</tr></table>");
            return strCounterHtml.ToString();
        }
    }
}

//Drag and drop this user control to required web page