Wednesday, 25 May 2016

SQL function for calculating Distance based on Two L attitude and Longitude

CREATE FUNCTION dbo.GetDistance(@lat1 DECIMAL(18,6), @lat2 DECIMAL(18,6), @lon1 DECIMAL(18,6), @lon2 DECIMAL(18,6))
RETURNS DECIMAL(18,6)
AS
BEGIN
declare @Result DECIMAL(18,6)
 IF  ((@lat1!=@lat2)and(@lon1!=@lon2 ))
    begin
        set @Result= ACOS(SIN(PI()*@lat1/180.0)*SIN(PI()*@lat2/180.0)+COS(PI()*@lat1/180.0)*COS(PI()*@lat2/180.0)*COS(PI()*@lon2/180.0-PI()*@lon1/180.0))*6371    
    end
 ELSE
    begin
           set @Result= 0
    end
return @result
END

Mobile or Phone Number validations formats in regular expressions

HTML Patterns:

 1. example : +91 96 5541 6053
pattern="[\+][ 0-9 ]{2,8}[' '][0-9 ]{2}[' '][0-9 ]{4}[' '][0-9 ]{4}";

// where  [\+ 0-9 ' ']  for STD or country code may be used
// [0-9 ]{2}[' '][0-9 ]{4}[' '][0-9 ]{4} for 10 digit number with space

<input type="text" name="phone" required
pattern="[\+][ 0-9 ]{2,8}[' '][0-9 ]{2}[' '][0-9 ]{4}[' '][0-9 ]{4}"
 title="Enter phone number format(+91 92 6545 7412)">

 2. example : 9655416053//only 10 numbers
pattern="[0-9 ]{10}";
 <input type="text" pattern="[0-9 ]{10}" onkeypress='return event.charCode >= 48 && event.charCode <= 57'></input>
//[0-9 ]{10} -for 10 digits
//onkeypress='return event.charCode >= 48 && event.charCode <= 57' - for enter only numbers

3.Minimum 10 or 10+ numbers
pattern="[0-9 ]{10,}";
<input type="text" pattern="[0-9 ]{10,}" onkeypress='return (event.charCode >= 46 && event.charCode <= 57 || event.charCode==8 || event.which==8 || event.keyCode==8 ||event.charCode==118 || event.which==118 || event.keyCode==118 )'></input>

//where event.charCode==8 for back space 118 for paste

Friday, 20 May 2016

Data Table to JSON in C#

using Newtonsoft.JSON;

//function for read data from db
 public DataView Get_DataView(string qry)
    {
       string con="your connection string";
        SqlConnection con = new SqlConnection(constr);
        SqlDataAdapter adp;
        try
        {
            con.Open();
            adp = new SqlDataAdapter(qry, con);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            DataTable dt = new DataTable();
            DataView dv = new DataView();
            dt = ds.Tables[0];
            dv = dt.AsDataView();
            return dv;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
        }
    } 
//in your controller
 DataView dv = UC.Get_DataView("select * from PRODUCT_MAS where STATUS='A'");
                model.productmasterlist = getProductList(dv);
                string JSONString = string.Empty;
                JSONString = JsonConvert.SerializeObject(dv.ToTable());  

Data Table to List without looping in C#


//function for read data from db
 public DataView Get_DataView(string qry)
    {
       string con="your connection string";
        SqlConnection con = new SqlConnection(constr);
        SqlDataAdapter adp;
        try
        {
            con.Open();
            adp = new SqlDataAdapter(qry, con);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            DataTable dt = new DataTable();
            DataView dv = new DataView();
            dt = ds.Tables[0];
            dv = dt.AsDataView();
            return dv;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
        }
    } 
//in your controller
DataTable unitDt = Get_DataView("select UNIT_ID,UNIT_NAME from UNIT_MAS ").ToTable();
                List<SelectListItem> unitList = unitDt.AsEnumerable().Select(row => new SelectListItem
                {
                    Value = row.Field<Int32>(0).ToString(),
                    Text = row.Field<string>(1),
                }).ToList();

Friday, 13 May 2016

Preventing Page Review after Logout with Forms Authentication: ASP.Net MVC

 //put this function within Global.asax.cs
//every click of back button it will reload. So you can each control check session is available or not
//based on session value redirect the page
protected void Application_BeginRequest()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();
        }