Tuesday, 23 April 2019

Difference between == and Equals() in c#

= = operator  compares if object references are same
.Equals() method compares if the contents are same not the object references
In case of string, it always compares on contents not the object references

Example: -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleAppTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //case-1
            string name = "Ashish";
            string myName = name;

            Console.WriteLine("== operator result is {0}", name == myName); // True
            Console.WriteLine("Equals method result is {0}", name.Equals(myName)); // True

            //case-2
            object name1 = "sandeep";
            char[] values = { 's', 'a', 'n', 'd', 'e', 'e', 'p' };
            object myName1 = new string(values);
            Console.WriteLine("== operator result is {0}", name1 == myName1); //False
            Console.WriteLine("Equals method result is {0}", myName1.Equals(name1)); // True

            //case-3
            string name2 = "sandeep";
            char[] values2 = { 's', 'a', 'n', 'd', 'e', 'e', 'p' };
            string myName2 = new string(values2);
            Console.WriteLine("== operator result is {0}", name2 == myName2); //True
            Console.WriteLine("Equals method result is {0}", myName2.Equals(name2)); // True

            //case-3
            string name3 = "sandeep";
            string myName3 = null;
            Console.WriteLine("== operator result is {0}", name3 == myName3); //False
            Console.WriteLine("Equals method result is {0}", myName3.Equals(name3)); // Throw error


            Console.ReadKey();
        }
    }
}

Monday, 22 April 2019

What is grep function in jquery

jQuery $.grep() Method. The jQuery method of $.grep() is used to filter the contents of an array

Example:- 

<html>
<head>
  <title>jQuery grep() function</title>
  <style>
  div {
    color: blue;
  }
  p {
    color: red;
    margin: 0;
  }

  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>

<div></div>
<p></p>

<script>
var arr1 = [ 1, 7, 4, 8, 6, 1, 9, 5, 3, 7, 3, 8, 5, 8, 2 ];
$( "div" ).text( arr1.join( ", " ) );

arr1 = jQuery.grep(arr1, function( n, i ) {
  return ( n !== 5 && i > 6 );
});


$( "p" ).text( arr1.join( ", " ) );
</script>

</body>

</html>


Output: -

1, 7, 4, 8, 6, 1, 9, 5, 3, 7, 3, 8, 5, 8, 2
3, 7, 3, 8, 8, 2

Wednesday, 28 March 2018

code

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int counter = 1;
            string line;
            DataTable dt = new DataTable();
            DataColumn column1 = new DataColumn("Id", System.Type.GetType("System.Int32"));
            DataColumn column2 = new DataColumn("Record", System.Type.GetType("System.String"));

            dt.Columns.Add(column1);
            dt.Columns.Add(column2);

            // Read the file and display it line by line. 
            System.IO.StreamReader file =
                new System.IO.StreamReader(@"E:/Study/Concepts/ConsoleApplication1/ConsoleApplication1/readfile.txt");
            while ((line = file.ReadLine()) != null)
            {
                System.Console.WriteLine(line);
                DataRow dr = dt.NewRow();
                dr[0] = counter;
                dr[1] = line.ToString();
                dt.Rows.Add(dr);
                counter++;
            }

            file.Close();
            System.Console.WriteLine("There were {0} lines.", counter);
            // Suspend the screen. 
            System.Console.ReadLine();
        }
    }
}

Saturday, 10 March 2018

In mvc have scripts and want to script rendered in footer section

1) _Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("About", "About", "Home")</li>
                    <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                </ul>
                @Html.Partial("_LoginPartial")
            </div>
        </div>
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            @RenderSection("scriptSection", required: false)
            <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>


2)- Index.cshtml -

@{
    ViewBag.Title = "Home Page";
}

@section scriptSection{
    <script type="text/javascript">alert('hello index page');</script>
}

<div class="jumbotron">
    <h1>ASP.NET</h1>
    <p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p>
    <p><a href="http://asp.net" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>
</div>

<div class="row">
    <div class="col-md-4">
        <h2>Getting started</h2>
        <p>
            ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that
            enables a clean separation of concerns and gives you full control over markup
            for enjoyable, agile development.
        </p>
        <p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301865">Learn more &raquo;</a></p>
    </div>
    <div class="col-md-4">
        <h2>Get more libraries</h2>
        <p>NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.</p>
        <p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301866">Learn more &raquo;</a></p>
    </div>
    <div class="col-md-4">
        <h2>Web Hosting</h2>
        <p>You can easily find a web hosting company that offers the right mix of features and price for your applications.</p>
        <p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301867">Learn more &raquo;</a></p>
    </div>
</div>

3)- Contactus.cshtml

@{
    ViewBag.Title = "Contact";
}
<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>

@section scriptSection{
    <script type="text/javascript">alert('hello contactus page');</script>
}
<address>
    One Microsoft Way<br />
    Redmond, WA 98052-6399<br />
    <abbr title="Phone">P:</abbr>
    425.555.0100
</address>

<address>
    <strong>Support:</strong>   <a href="mailto:Support@example.com">Support@example.com</a><br />
    <strong>Marketing:</strong> <a href="mailto:Marketing@example.com">Marketing@example.com</a>
</address>

4) AboutUs.cshtml

in this page not create scriptSection

Once the running application, script which are witten under scriptSection on Index page will be fired.
Now click on About link script will not get executed because in AboutUs.cshtml we do not create scriptSection and if click on Contact link, script section will be executed.

Note: 
 @RenderSection("scriptSection", required: true)
 if set to required true then in every view like Index/About and ContactUs.cshtml pages we have to create scriptSection{} otherwise when click on About link then it will be throw error and for others link home and Contact it will be worked fine because on both views we have created script section{}.




What is unity of work


How to set response time when called from jquery to invoke server side action

$.ajax({
    url: "test.html",
    timeout: 3000,
    error: function(){
        //do something
    },
    success: function(){
        //do something
    }
});
In ajax call do not have any default timeout for request processing and return the 
response. 
In above example, i have set timeout of 3 sec. If the server return response within 3 sec
then success function will be executed otherwise throws error of timeout and error fuction
will be executed.

Why Http is stateless and difference between Http and Https