Tuesday, 3 January 2017

Javascript Number Validation on Key Press

onkeypress="return isNumber(event)"

function isNumber(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    else
        return true;
}   

Saturday, 3 December 2016

Log4Net - Logging in to Log text File in Asp.net Application step by step

FrameWorkUsed in this application 2.0

Step 1 : Click here to Download Log4net Dll from official site.

Step 2 : Unzip the Downloaded Zip File. Navigate to this path .
C:\Users\Gokul\Downloads\Compressed\log4net-1.2.15-bin-newkey_2\log4net-1.2.15\bin\net

Step 3 : Select your asp.net framework version folder and click on release folder ,

C:\Users\Gokul\Downloads\Compressed\log4net-1.2.15-bin-newkey_2\log4net-1.2.15\bin\net\2.0\release

and copy log4net.dll from that folder and paste it in the bin folder of your applciation.

For Ex : C:\Users\Gokul\Documents\visual studio 2010\Projects\Log4net2.0\Log4net2.0\bin

Step 4 : Now add reference of log44Net dll to your applcaition by  ,

Right Clicking on you Project , Add Reference > click on Browse  .

Navigate to the path of Bin where You pasted the Log4Net dll.
For Eg : Here C:\Users\Gokul\Documents\visual studio 2010\Projects\Log4net2.0\Log4net2.0\bin

Select the Log4net dll from that path and then click ok to the Log4net dll Reference to our Project.

Step 5 : Now in Your Assemblyinfo.cs File , add this line of code

[assembly: log4net.Config.XmlConfigurator(Watch = true)] as showed in image Below.



Step 6 :
Now add this line of Code in your Global.asax in the Application_Start

 protected void Application_Start(object sender, EventArgs e)
        {
            log4net.Config.XmlConfigurator.Configure();

         }

Step 7 : Now in web.config file add this code in , <configuration> Section

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />

  </configSections>
<log4net>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="D:/log.txt"/>
      <appendToFile value="true"/>
      <rollingStyle value="Size"/>
      <maxSizeRollBackups value="10"/>
      <maximumFileSize value="1MB"/>
      <staticLogFileName value="true"/>
      <layout type="log4net.Layout.SimpleLayout"/>
    </appender>
    <root>
      <level value="ALL"/>
      <appender-ref ref="RollingFileAppender"/>
    </root>

  </log4net>

Like below in my Web.cofig file ,



Step 7 : In the web pages where you want to Log your Information of the user activities .
Add this Line of code , as below in image of my sample Default.aspx page.

 private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);



log.Info(" Your Message here to log when user visited this page .");


Thursday, 3 November 2016

Javascript validation for Date Of Birth

function ValidateDate() {
        var input = document.getElementById('<%= txtAppDOB.ClientID %>').value;
        var pattern =/(?=\d)^(?:(?!(?:10\D(?:0?[5-9]|1[0-4])\D(?:1582))|(?:0?9\D(?:0?[3-9]|1[0-3])\D(?:1752)))((?:0?[13578]|1[02])|(?:0?[469]|11)(?!\/31)(?!-31)(?!\.31)|(?:0?2(?=.?(?:(?:29.(?!000[04]|(?:(?:1[^0-6]|[2468][^048]|[3579][^26])00))(?:(?:(?:\d\d)(?:[02468][048]|[13579][26])(?!\x20BC))|(?:00(?:42|3[0369]|2[147]|1[258]|09)\x20BC))))))|(?:0?2(?=.(?:(?:\d\D)|(?:[01]\d)|(?:2[0-8])))))([-.\/])(0?[1-9]|[12]\d|3[01])\2(?!0000)((?=(?:00(?:4[0-5]|[0-3]?\d)\x20BC)|(?:\d{4}(?!\x20BC)))\d{4}(?:\x20BC)?)(?:$|(?=\x20\d)\x20))?((?:(?:0?[1-9]|1[012])(?::[0-5]\d){0,2}(?:\x20[aApP][mM]))|(?:[01]\d|2[0-3])(?::[0-5]\d){1,2})?$/;
        var ss=pattern.test(input).toString();

        if(ss=="false")
        {
            alert(Invalid Dob);
        }
        else
        {
          alert(Valid Dob);
        }

Tuesday, 30 August 2016

Insert update select c#

web.config
<connectionStrings>

    <add name="conString"
       connectionString="Data Source=GOKUL-PC\GOKUL;
                          database=InvoicePrj;Integrated Security=true"/>
  </connectionStrings>


protected void crudoperations(string status,string itemid, string ItemCode, string ItemDesc, string UOM, string Qty, double Rate)
        {
            String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(strConnString))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("crudoperations", con);
                cmd.CommandType = CommandType.StoredProcedure;
                if (status == "INSERT")
                {
                    cmd.Parameters.AddWithValue("@ItemId", itemid);
                    cmd.Parameters.AddWithValue("@ItemCode", ItemCode);
                    cmd.Parameters.AddWithValue("@ItemDesc", ItemDesc);
                    cmd.Parameters.AddWithValue("@UOM", UOM);
                    cmd.Parameters.AddWithValue("@Qty", Qty);
                    cmd.Parameters.AddWithValue("@Rate", Rate);
                    cmd.Parameters.AddWithValue("@status", status);
                    SqlParameter outPutParameter = new SqlParameter();
                    outPutParameter.ParameterName = "@ErrStatus";
                    outPutParameter.SqlDbType = System.Data.SqlDbType.VarChar;
                    outPutParameter.Size = 500;
                    outPutParameter.Direction = System.Data.ParameterDirection.Output;
                    cmd.Parameters.Add(outPutParameter);
                    cmd.ExecuteNonQuery();
                    string Errstatus = outPutParameter.Value.ToString();
                    if (Errstatus == "1")
                    {
                        Label1.Text = "New Dc Added!!";
                        string message = "New Dc Added!!"; System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("<script type = 'text/javascript'>"); sb.Append("window.onload=function(){"); sb.Append("alert('"); sb.Append(message); sb.Append("')};"); sb.Append("</script>"); ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
                    }
                    else
                    {
                        Label1.Text = "Error Adding Dc !!";
                        string message = "Error Adding Dc !!"; System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("<script type = 'text/javascript'>"); sb.Append("window.onload=function(){"); sb.Append("alert('"); sb.Append(message); sb.Append("')};"); sb.Append("</script>"); ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());

                    }
                    gvDetails.EditIndex = -1;
                    int id = Convert.ToInt32(TextBox2.Text);
                    BindGridview(id);
                }
}

Update---
else if (status == "UPDATE")
                {
                    cmd.Parameters.AddWithValue("@ItemCode", ItemCode);
                    cmd.Parameters.AddWithValue("@ItemDesc", ItemDesc);
                    cmd.Parameters.AddWithValue("@UOM", UOM);
                    cmd.Parameters.AddWithValue("@Qty", Qty);
                    cmd.Parameters.AddWithValue("@Rate", Rate);
                    cmd.Parameters.AddWithValue("@status", status);
                    SqlParameter outPutParameter = new SqlParameter();
                    outPutParameter.ParameterName = "@ErrStatus";
                    outPutParameter.SqlDbType = System.Data.SqlDbType.VarChar;
                    outPutParameter.Size = 500;
                    outPutParameter.Direction = System.Data.ParameterDirection.Output;
                    cmd.Parameters.Add(outPutParameter);
                    cmd.ExecuteNonQuery();
                    string Errstatus = outPutParameter.Value.ToString();
                    if (Errstatus == "1")
                    {
                        Label1.Text = " Dc Updated!!";
                        string message = " Dc Updated!!"; System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("<script type = 'text/javascript'>"); sb.Append("window.onload=function(){"); sb.Append("alert('"); sb.Append(message); sb.Append("')};"); sb.Append("</script>"); ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
                    }
                    else
                    {
                        Label1.Text = " Error Updating Dc !!";
                        string message = "Error Updating Dc !!"; System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("<script type = 'text/javascript'>"); sb.Append("window.onload=function(){"); sb.Append("alert('"); sb.Append(message); sb.Append("')};"); sb.Append("</script>"); ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());

                    }
                    gvDetails.EditIndex = -1;
                    int id = Convert.ToInt32(TextBox2.Text);
                    BindGridview(id);
                }
}

--select---

 Label1.Text = "";
            Session["User_Id"] = txtUserId.Text.ToString();
            String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
            SqlConnection con = new SqlConnection(strConnString);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Validate_Login_Details";
            cmd.Parameters.Add("@Role", SqlDbType.Char).Value = "";
            cmd.Parameters.Add("@User_Id", SqlDbType.Char).Value = txtUserId.Text;
            string pwd = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPwd.Text.ToString(), "SHA1");
            cmd.Parameters.Add("@Password", SqlDbType.Char).Value = pwd.ToString();


            SqlParameter outPutParameter = new SqlParameter();
            outPutParameter.ParameterName = "@Status";
            outPutParameter.SqlDbType = System.Data.SqlDbType.Int;
            outPutParameter.Direction = System.Data.ParameterDirection.Output;
            cmd.Parameters.Add(outPutParameter);
            SqlParameter outPutParameter1 = new SqlParameter();
            outPutParameter1.ParameterName = "@UID";
            outPutParameter1.SqlDbType = System.Data.SqlDbType.VarChar;
            outPutParameter1.Size = 400;
            outPutParameter1.Direction = System.Data.ParameterDirection.Output;
            cmd.Parameters.Add(outPutParameter1);
            cmd.Connection = con;
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
                string status = outPutParameter.Value.ToString();
                string uid = outPutParameter1.Value.ToString();
                string Redirecturl = "http://localhost:52418/ResetPassword.aspx?UID=" + uid + "";
                if (status == "1111")
                {
                    Response.Redirect("AdministratorPanel.aspx");
                }
                else if (status == "10")
                {
                    Label1.ForeColor = System.Drawing.Color.LightGreen;
                    Response.Redirect("UserHome.aspx");
                }
                else if (status == "20")
                {
                    Label1.Text = "User Account is Locked !! Contact System Adminstrator!!";
                }
}
}


Saturday, 27 August 2016

Regular Expression Validation To Validate Only Numbers in ASP.Net


Only number  :  ValidationExpression="((\d+))$"                                          
10 digit number  :  ValidationExpression="^\(\d{3}\) ?\d{3}( |-)?\d{4}|^\d{3}( |-)?\d{3}( |-)?\d{4}"