Tuesday, 18 January 2022

Asp.Net Core 6: How to Search/Find Data Between Two Dates In MS Sql , Linq, C#.net

1/18/2022 - By Pranav Singh 0

In this article i will show you how you can search or find data between two dates in asp.net core 6/ MVC using MS Sql database using c# with entity framework. In this asp.net core 6 tutorial i will user entity framework to access data and filter data using linq in c#.net.


Other articlesHow To Read / Access Email Setting In Appsettings.json In Asp.Net Core 6 in Controller Using C#.NetSearch And Display Data In Table/Tabular Format in Asp.net Core 6/MVC using C# (Ms Sql Server)Display Data In Table/Tabular Format In Asp Net Core 6/MVC Using C#.Net (Ms Sql Database)Asp.net Core 6: Ajax Login Form Using jQuery, C# Validate From Ms Sql Server Database.


Now for this article first we will create a new asp.net core 6/MVC application. Now we will user Scamffold commend to make entity object. 


Scaffold-DbContext "Server=LAPTOP-DCE5GAKD\SQLEXPRESS;Database=TestDB;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir data  -force


Now we will get all our table entity in data folder. 


Asp.Net Core 6: How to Search/Find Data Between Two Dates In MS Sql , Linq, C#.net

Now check the data present in the table. We will apply code to get the data from data base as per passed date.


Employee table


Now we will create a model class in this we will define the properties into it.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

 

namespace Project.Models

{

    public class EmployeeModel

    {

        public List<EmployeeData> EmployeeDataList { get; set; }

    }

    public class EmployeeData

    {

        public int Id { get; set; }

        public string EmployeeName { get; set; }

        public string Department { get; set; }

        public string Location { get; set; }

        public DateTime CreatedDate { get; set; }

    }

}


Now we ill create  a new controller class and add the below code into the httpget page.

[HttpGet]

        public IActionResult Index()

        {

            TestDBContext testDBContext = new TestDBContext();

            EmployeeModel employeeModel = new EmployeeModel();

            employeeModel.EmployeeDataList = new List<EmployeeData>(); 

            //DateTime date = new DateTime();

            DateTime fromdate =  DateTime.Now.AddDays(-45);

            DateTime todate = DateTime.Now;

            var data = testDBContext.Employees.Where(m => m.CreatedDate >= fromdate && m.CreatedDate <= todate);

            foreach (var item in data)

            {

                employeeModel.EmployeeDataList.Add(new EmployeeData

                {

                    Id = item.Id,

                    CreatedDate = item.CreatedDate,

                    Department = item.EmployeeName,

                    EmployeeName = item.EmployeeName,

                    Location = item.Location

                }); 

            }

            return View(employeeModel);

        }


In above code i have taken two date from date and to date. Now i have used linq query to filter the data. In my linq query i have applied condition to get data between two date as per the created data column.

After writing our controller code we will prepare the view. 

@model Project.Models.EmployeeModel

@{

    ViewData["Title"] = "Home Page";

}

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @id = "formxmldata", @enctype = "multipart/form-data" }))

{

    <table class="table table-bordered">

        <thead>

            <tr>

                <th>Id</th>

                <th>Employee Name</th>

                <th>Department</th>

                <th>Location</th>

                <th>Created Date</th>

            </tr>

        </thead>

        <tbody>

            @foreach (var item in Model.EmployeeDataList)

            {

                <tr>

                    <td>@item.Id</td>

                    <td>@item.EmployeeName</td>

                    <td>@item.Department</td>

                    <td>@item.Location</td>

                    <td>@item.CreatedDate</td>

                </tr>

            }

        </tbody>

    </table>

}


In above code i have applied foreach lop to bind the data to the table in asp.net core Now run the code to check the output. You can check the break point results. 


In above code we are getting three record as per the passed date. Now press F5 and check the final output.

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top