Wednesday, November 9, 2016

T-SQL: Calculate distance in KM between 2 points

If you have 2 points (Lang and Lat) and you want to calculate distance between them in KM using SQL function, you can follow the following steps:

1. Execute the following script to declare a function to convert Degree to Radius:

CREATE FUNCTION [dbo].[ConvertDegreeToRadius](@Degree float)
RETURNS float
WITH RETURNS NULL ON NULL INPUT
AS
BEGIN
RETURN (
    SELECT @Degree * (PI() / 180)
)
END


2. Execute the following script to declare a function to calculate the distance in KM:

CREATE FUNCTION [dbo].[CalculateDistance](@firstLat float, @firstLang float, @secondLat float, @secondLang float)
RETURNS float
WITH RETURNS NULL ON NULL INPUT
AS
BEGIN

-- static value, it is earch radius
DECLARE @EarchRadius int = 6371
DECLARE @Lat float = SearchTables.ConvertDegreeToRadius(@secondLat - @firstLat)
DECLARE @Lang float = SearchTables.ConvertDegreeToRadius(@secondLang - @firstLang)

DECLARE @a float = ( SIN(@Lat / 2) * SIN( @Lat / 2) + COS(SearchTables.ConvertDegreeToRadius(@firstLat)) * COS(SearchTables.ConvertDegreeToRadius(@secondLat)) * SIN (@Lang / 2) * SIN(@Lang / 2))
DECLARE @c float = 2 * ATN2(SQRT(@a), SQRT(1 - @a))
DECLARE @d float = @EarchRadius * @c

RETURN (
   

SELECT @d

)
END


3. Now, let us try to test the function and see the result:












Monday, June 27, 2016

MVC: Styles is not applied on layout

If your styles are not applied and you got 404 error message and your file path is correct, just change the following path in your web.config:

<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />

to

<add name="BlockViewHandler" path="*.cshtml" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" /> 

Friday, June 24, 2016

MVC: Passing objects from main view to partial view

There are many ways to pass object from view to partial view, the following is an example how to pass object throw strong type model.

Suppose you have main page, which has Message partial view. Message is used to display messages and it should be shared between other views.

You have to create Model for Message which includes the following:
1. Message, which is message text
2. Type, message type to control the message css class, errors, success, notifications ... etc

Then you need to create View with strong type Message, and don't forget to check Partial View when you create the view.

put the following lines in the partial view:
@if(Model != null)
{
<div>
    @Model.Message
</div>
}

After that, create a controller and leave it as is.

In the main View. you will have the following:
@model Tuple<EventsRegistrations.Users.Model.ProfileInfo, EventsRegistrations.MVC.Models.Shared.Messages>

@{
    Layout = null;
}
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>View1</title>
</head>
<body>
    @Html.Partial("Message", Model.Item2)
    <br />
    @Html.Partial("Profile", Model.Item1)
</body>
</html>


And in the controller set the following:
public Messages msg = new Messages();
        public EventsRegistrations.Users.Model.ProfileInfo obj = new Users.Model.ProfileInfo();

        public ViewResult DisplayProfile()
        {
            try
            {
                obj = EventsRegistrations.Users.BLL.Profile.Instance.Get(1);
                throw new Exception("ex");
                return View("Index", Tuple.Create(obj, msg));
            }
            catch (Exception ex)
            {
                msg.Message = MessageHelper.BuildExceptionMessage(ex);
                msg.Type = 1;
                return View("Index",Tuple.Create(obj, msg));
            }
        }


Sunday, June 19, 2016

MVC: Using AutoMapper

When you using 3-tiers architecture with MVC, you need to map the main model with MVC model.
you can do it by multiple ways, the best is to have centralized method to do mapping between two objects in different class.

The AutoMapper library provide this facility, you can download it via visual studio / NuGet pakage.

It is very easy and dynamic.

Example,
Suppose you have ProfileInfo class in the main model, with the following definition:




and ProfileInfo class in MVC model, which inherited the ProfileInfo class without any additional properties


In the controller, you need to map main ProfileInfo with MVC ProfileInfo after getting data from BLL, then present the MVC ProfileInfo result in the View. You should do the following:



Note, this is an example, but the best practice as mentioned is to have centralized method. So, you can put the map config in common method which will be shared between all classes.

En jo y

Thursday, March 10, 2016

PowerBI: Connect on-premise data using enterprise gateway

Microsoft provides different ways to visualize your data. One of them is SaaS PowerBi
It enables you to connect different data sources and visualize your data

If you want to connect on-premise data, you need to install and configure enterprise gateway by following the following steps

1. Go to this site and install enterprise version of gateway:
https://powerbi.microsoft.com/en-us/gateway/

2. Run the exe file installed and follow the wizard








3. Now, you should connect your data source using PowerBI DESKTOP version, then save the file and upload it to PowerBI.com






4. Now, you can create your visuals,