What is the difference between First() and FirstOrDefault()


Hi

Onetime i was giving interview then i got this question.Then i told both is used for same purpose. But it was not the exact answer.

In simple word, FirstOrDefault() will handle the exception but First() will not handle the exception.

First() and FirstOrDefault() are two extension methods of the Enumerable class.

So lets test with some example in asp.net

 protected void Page_Load(object sender, EventArgs e)
    {
      
        int[] number = { 1, 5, 6 };
        var num = number.Where(n => n > 10).FirstOrDefault();;
        Response.Write(num);

    }


Note: This code will not throws the exception. So if we have to handle the exception then we can use FirstOrDefault();.

Now lets test with First()

 protected void Page_Load(object sender, EventArgs e)
    {
      
        int[] number = { 1, 5, 6 };
        var num = number.Where(n => n > 10).First();
        Response.Write(num);

    }

Note: This code will throw the exception.

How to fetch data from StoreProcedure using Entity Framework ?


Hi

If you will create the Data model in EF using general approach then you will be not able to fetch data from SP in EF.

For fetching data from SP using EF. we can do like this

Step 1 : Add the SP and Table Name in Data Model like this

EF1

Step 2: In the data model designer, right-click on Model Browser then go to storeprocedure

EF2

Step 3: click on “Add Function Import” and configure the SP maping like image

EF3

Step 4: Click on save all and build the application

Step 5: Write the code in code behind file like this


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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        using (DatabaseModel.DatabaseEntities dc = new DatabaseModel.DatabaseEntities())
        {
            //var query = dc.VW_tblEmp.Select(m => m);
            var query = dc.GetEmpName();
            Grid1.DataSource = query;
            Grid1.DataBind();
        }
    }
}

Note: Here i hope that you are aware of doing configuration on model in EF.

Drag and drop using HTML 5.0


Drag and Drop
Hi

One of the cool feature added in HTML 5.0 i.e drag and drop. Previously we were doing this task by using pure Javascript and Jquery. But we can doing using HTML 5.0 and Javascript in very simple way.

Here just write 3 functions in Javascript i.e
a. allowDrop(ev)
b. drag(ev)
c. drop(ev)

For example if we have to make image as dragable then make there draggable=”True” and ondragstart just call drag function. like this


<img id="img1" src="Water lilies.jpg" draggable="true"
ondragstart="drag(event)" width="336" height="100">


For div where we have to keep this image ondrop just call drop(event) function and on ondragover just call allowDrop function

like this


<div class="DivClass1" ondrop="drop(event)"
        ondragover="allowDrop(event)">Drop Here</div>


The complete HTML code is like this


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Drag and drop</title>
    <style type="text/css">
    .DivClass 
    {
        width:350px;
        height:120px;
        padding:10px;
        border:1px solid #aaaaaa;
     }
     
     .DivClass1 
    {
        width:350px;
        height:120px;
        padding:10px;
        border:5px solid #aaaaaa;
     }
    </style>

    <script type="text/javascript">

        function allowDrop(ev) {
            ev.preventDefault();
        }

        function drag(ev) {
            ev.dataTransfer.setData("Text", ev.target.id);
        }

        function drop(ev) {
            ev.preventDefault();
            var data = ev.dataTransfer.getData("Text");
            ev.target.appendChild(document.getElementById(data));
        }
      </script>
</head>
<body>
    <form id="form1" runat="server">
   <div class="DivClass" ondrop="drop(event)"
        ondragover="allowDrop(event)">Drop Here </div>
        <br />
        

<div class="DivClass1" ondrop="drop(event)"
        ondragover="allowDrop(event)">Drop Here</div>


<img id="img1" src="Water lilies.jpg" draggable="true"
ondragstart="drag(event)" width="336" height="100">


    </form>
</body>
</html>


Some hidden concepts in HTML


Hi

There are so many concepts available in HTML, which one we donot know. But if you are going to give some interview as web developer then they can ask this type of simple question

1. What is the Attribute in HTML ?

>> Attribute provide the additional information about element in HTML. It will be available in start tag

Example

<a href="https://chandradev819.wordpress.com">This is my blog</a> 

Note: Here href is the attribute

Some other example of attribute in HTML are
a. Class
b. Id
c. Style
d. Title

2. What is the HTML Meta element ?
>> Meta data is the information about data.

The tag provides metadata about the HTML document. Metadata will not be displayed on the page, but will be machine parsable.

The metadata can be used by browsers (how to display content or reload page), search engines (keywords), or other web services

One very good example of meta data to refresh page on every 2 second

We can use like this


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <meta http-equiv="refresh" content="2">
</head>
<body>
<a href="https://chandradev819.wordpress.com">This is my blog</a> 
</body>
</html>