Friday, April 17, 2020

How to loop through HTML table columns?

To loop through each table columns follow the below code.

<html>
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
        integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
    <br>
    <div class="container">
        <div class="col-12">
            <table class="table table-bordered">
                <tr>
                    <td>1</td>
                    <td>2</td>
                </tr>
                <tr>
                    <td>a</td>
                    <td>b</td>
                </tr>
                <tr>
                    <td>c</td>
                    <td>d</td>
                </tr>
            </table>
        </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.0/jquery.min.js"
        integrity="sha256-xNzN2a4ltkB44Mc/Jz3pT4iU1cmeR0FkXs4pru/JxaQ=" crossorigin="anonymous"></script>

    <script>
        var nrow = $("table")[0].rows.length;
        var ncol = $("table")[0].rows[0].cells.length;

        for (let c = 0c < ncolc++) {

            for (let r = 0r < nrowr++) {

                console.log($($("table")[0].rows[r].cells[c]).text());
            }
        }

    </script>
</body>
</html>

Output:





How to run HTML file from VSCode?

We can easily run the HTML file by using an extention called Live Server by Ritwick Dey.

Note: To run the file it must be inside a directory or else it wont run.

https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer 

Sunday, February 2, 2020

Disable the browser back button

Use this script to disable the browser back button once the user has logged out. So that even if he tries to click the back button and go back to the previous page he will not be able to go.



<script>
    history.pushState(null, null, location.href);
    window.onpopstate = function () {
        history.go(1);
    };
</script>

Forms Authentication in MVC

index.cshtml

@using (Html.BeginForm("index","home",FormMethod.Post))
{
    @Html.AntiForgeryToken()

    <span>Enter Name</span>
    <input type="text" name="name"  required />

    <input type="submit"  value="Submit" />

}

HomeController.cs
        

        using System.Web.Security;

 [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(FormCollection formCollection)
        {

            if (ModelState.IsValid)
            {
                  var authTicket = new FormsAuthenticationTicket(
                    1,// version
                    formCollection["name"].ToString(), // user name
                    DateTime.Now, // created
                    DateTime.Now.AddMinutes(20), // expires
                    false// persistent?
                    "User"  // can be used to store roles
                    );

                string encryptedTicket = FormsAuthentication.Encrypt(authTicket);

                var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

                System.Web.HttpContext.Current.Response.Cookies.Add(authCookie);         

                return RedirectToAction("about");

            }
            else
            {
                return View();
            }
        }

      
 [Authorize(Roles ="User")]
        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

       
[Authorize]
        public ActionResult logout()
        {

            FormsAuthentication.SignOut();
            return RedirectToAction("index");      
       
        }


Inside the Global.asax.cs


using System.Security.Principal;
using System.Web.Security;

        protected void Application_AuthenticateRequest(Object sender, EventArgs e)
        {
            HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];

            if (authCookie == null || authCookie.Value == "")
                return;

            FormsAuthenticationTicket authTicket;

            try
            {
                authTicket = FormsAuthentication.Decrypt(authCookie.Value);
            }
            catch
            {
                return;
            }

            // retrieve roles from UserData
            string[] roles = authTicket.UserData.Split(';');

            if (Context.User != null)
                Context.User = new GenericPrincipal(Context.User.Identity, roles);

        }




Inside the web.config


  <authentication mode="Forms" >
    <forms defaultUrl="home/index" loginUrl="home/index"  protection="All"></forms>
  </authentication>

  <authorization>
    <allow users="*"/>
    <deny users="?"/>
  </authorization>

Saturday, December 7, 2019

Deploy Django application on XAMPP apache Server (Windows)

Below are the steps to deploy the Django application on XAMPP apache server.

Assuming that you have already downloaded and installed the following.




  • After copying the file from the folder rename it with mod_wsgi.so and paste the file where the XAMPP server is installed on your system inside the modules.


  • Make change in the httpd.conf file



  • Add the below line in httpd.conf


  • Create a Django project inside the htdocs folder.

  • After completion of the project when ready to deploy once again open the httpd.conf and add few more lines of code.




  • After add the above lines of code then restart the system and run the XAMPP server to check 



Sunday, November 17, 2019

Excluding fields in MongoDB using Asp.net



using MongoDB.Bson;
using MongoDB.Driver;                             
          
static MongoClient client = new MongoClient("mongodb://localhost:27017"); //Set the connection string

static IMongoDatabase db = client.GetDatabase("test"); //Database name

public void Exclude()
{
   IMongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("user");

   var filter = Builders<BsonDocument>.Filter.Empty;   

 // uncomment the below two lines to exclude the required things

// var project = Builders<BsonDocument>.Projection.Exclude("_id"); //replace the field which you want to exclude in place of _id and it must match the field name in the MongoDB document

//var res = collection.Find<BsonDocument>(filter).Project(project).ToList();

    var res = collection.Find(filter).ToList(); //Comment this after un commenting the above two line

}

Before excluding _id field

After 



How to loop through HTML table columns?

To loop through each table columns follow the below code. <html> <head>      <link   rel = "stylesheet"   ...