SELECT * FROM sys.procedures WHERE Object_definition(object_id) LIKE '%keyword%'
Tuesday 14 November 2017
Sunday 22 October 2017
Distinct List Function Using SQL
Distinct List Function Using SQL
CREATE FUNCTION dbo.DistinctList
(
@List VARCHAR(MAX),
@Delim CHAR
)
RETURNS
VARCHAR(MAX)
AS
BEGIN
DECLARE @ParsedList TABLE
(
Item VARCHAR(MAX)
)
DECLARE @list1 VARCHAR(MAX), @Pos INT, @rList VARCHAR(MAX)
SET @list = LTRIM(RTRIM(@list)) + @Delim
SET @pos = CHARINDEX(@delim, @list, 1)
WHILE @pos > 0
BEGIN
SET @list1 = LTRIM(RTRIM(LEFT(@list, @pos - 1)))
IF @list1 <> ''
INSERT INTO @ParsedList VALUES (CAST(@list1 AS VARCHAR(MAX)))
SET @list = SUBSTRING(@list, @pos+1, LEN(@list))
SET @pos = CHARINDEX(@delim, @list, 1)
END
SELECT @rlist = COALESCE(@rlist+',','') + item
FROM (SELECT DISTINCT Item FROM @ParsedList) t
RETURN @rlist
END
GO
Input : SELECT dbo.DistinctList('ashish,ashish,manish',',')
output: ashish,manish
CREATE FUNCTION dbo.DistinctList
(
@List VARCHAR(MAX),
@Delim CHAR
)
RETURNS
VARCHAR(MAX)
AS
BEGIN
DECLARE @ParsedList TABLE
(
Item VARCHAR(MAX)
)
DECLARE @list1 VARCHAR(MAX), @Pos INT, @rList VARCHAR(MAX)
SET @list = LTRIM(RTRIM(@list)) + @Delim
SET @pos = CHARINDEX(@delim, @list, 1)
WHILE @pos > 0
BEGIN
SET @list1 = LTRIM(RTRIM(LEFT(@list, @pos - 1)))
IF @list1 <> ''
INSERT INTO @ParsedList VALUES (CAST(@list1 AS VARCHAR(MAX)))
SET @list = SUBSTRING(@list, @pos+1, LEN(@list))
SET @pos = CHARINDEX(@delim, @list, 1)
END
SELECT @rlist = COALESCE(@rlist+',','') + item
FROM (SELECT DISTINCT Item FROM @ParsedList) t
RETURN @rlist
END
GO
Input : SELECT dbo.DistinctList('ashish,ashish,manish',',')
output: ashish,manish
Thursday 28 September 2017
Website Performance with ASP.NET - Use Cache Headers (HttpModule CacheHeader define)
Setting Header Programatically
You can also set the cache headers programmatically. This can be useful for generated content and allows more fine granular and flexible control of the cache headers. The example below allows clients as well as proxies to cache all responses for one hour. You can of course use different settings depending on any parameter/content/config value etc.using System;
using System.Web;
using System.Web.UI;
using System.Web.Configuration;
using System.Text;
using System.Security.Cryptography;
using System.IO;
public class PreInitModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += HandlePreRequest;
}
private void SetDefaultCacheHeader(object sender, EventArgs eventArgs)
{
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
HttpContext.Current.Response.Cache.SetMaxAge(TimeSpan.FromSeconds(3600));
HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddSeconds(3600));
}
}
Web.config
<system.web>
<httpModules>
<add name="MyPreInitModule" type="PreInitModule" />
</httpModules>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="MyPreInitModule" type="PreInitModule" />
</modules>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
<mimeMap fileExtension=".otf" mimeType="application/x-font-opentype" />
<mimeMap fileExtension=".woff" mimeType="application/font-woff" />
<mimeMap fileExtension=".woff2" mimeType="application/font-woff2" />
</staticContent>
<caching enabled="true" enableKernelCache="true">
<profiles>
<add extension=".png" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
<add extension=".jpg" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
<add extension=".jpeg" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
<add extension=".gif" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
<add extension=".css" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
<add extension=".js" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
<add extension=".pdf" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
<add extension=".ico" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
<add extension=".woff2" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
<add extension=".eot" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
<add extension=".ttf" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
</profiles>
</caching>
</system.webServer>
Saturday 23 September 2017
asp.net MVC HttpModule example
1) First Create SessionManger class
using System.Web;
using System.Collections.Generic;
namespace ManageSession
{
public class SessionManger
{
public static int AdminID
{
get
{
return (int)System.Web.HttpContext.Current.Session["AdminID"];
}
set { System.Web.HttpContext.Current.Session["AdminID"] = value; }
}
public static int UserType
{
get
{
return (int)System.Web.HttpContext.Current.Session["UserType"];
}
set { System.Web.HttpContext.Current.Session["UserType"] = value; }
}
public static int CompanyID
{
get
{
return (int)System.Web.HttpContext.Current.Session["CompanyID"];
}
set { System.Web.HttpContext.Current.Session["CompanyID"] = value; }
}
public static string UserTheme
{
get
{
return (string)System.Web.HttpContext.Current.Session["UserTheme"];
}
set { System.Web.HttpContext.Current.Session["UserTheme"] = value; }
}
public static int BranchID
{
get
{
return (int)System.Web.HttpContext.Current.Session["BranchID"];
}
set { System.Web.HttpContext.Current.Session["BranchID"] = value; }
}
}
}
using System.Web;
using System.Collections.Generic;
namespace ManageSession
{
public class SessionManger
{
public static int AdminID
{
get
{
return (int)System.Web.HttpContext.Current.Session["AdminID"];
}
set { System.Web.HttpContext.Current.Session["AdminID"] = value; }
}
public static int UserType
{
get
{
return (int)System.Web.HttpContext.Current.Session["UserType"];
}
set { System.Web.HttpContext.Current.Session["UserType"] = value; }
}
public static int CompanyID
{
get
{
return (int)System.Web.HttpContext.Current.Session["CompanyID"];
}
set { System.Web.HttpContext.Current.Session["CompanyID"] = value; }
}
public static string UserTheme
{
get
{
return (string)System.Web.HttpContext.Current.Session["UserTheme"];
}
set { System.Web.HttpContext.Current.Session["UserTheme"] = value; }
}
public static int BranchID
{
get
{
return (int)System.Web.HttpContext.Current.Session["BranchID"];
}
set { System.Web.HttpContext.Current.Session["BranchID"] = value; }
}
}
}
2) PreInitModule class
using System;
using System.Web;
using System.Web.UI;
using System.Web.Configuration;
using System.Text;
using System.Security.Cryptography;
using System.IO;
/// <summary>
/// Ashish Srivastava
/// 21 Sep 2017
///
/// </summary>
public class PreInitModule : IHttpModule
{
#region IHttpModule Members
public void Dispose()
{
//throw new NotImplementedException();
}
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += HandlePreRequest;
}
void HandlePreRequest(object sender, EventArgs e)
{
var page = HttpContext.Current.CurrentHandler as System.Web.Mvc.MvcHandler;
if (page != null)
{
//page.PreInit += delegate
//{
if (HttpContext.Current.Request.Path.ToLower().Contains("/admin/"))
{
if (HttpContext.Current.Session["AdminID"] != null)
{
if (HttpContext.Current.Session["CompanyID"] != null)
{
if (HttpContext.Current.Session["BranchID"] != null) return;
var loginPageBranch = WebConfigurationManager.AppSettings["LoginPagePath"] ?? "~/Admin/Branch";
if (HttpContext.Current.Request.Path.Contains(loginPageBranch.Substring(loginPageBranch.LastIndexOf("/") + 1)) == false
&& HttpContext.Current.Request.Path.Contains("Branch") == false
&& HttpContext.Current.Request.Path.Contains("BillingCompanyReport.aspx") == false
&& HttpContext.Current.Request.Path.Contains("UserCompanyMapping.aspx") == false
&& HttpContext.Current.Request.Path.Contains("AddCompany.aspx") == false
&& HttpContext.Current.Request.Path.Contains("AddBranch.aspx") == false
&& HttpContext.Current.Request.Path.Contains("InvoceHeadSettings.aspx") == false
//&& HttpContext.Current.Request.Path.Contains("ForgetPassword.aspx") == false
)
{
HttpContext.Current.Response.Redirect(loginPageBranch, true);
}
else
{
return;
}
}
var loginPageCompany = WebConfigurationManager.AppSettings["LoginPagePath"] ?? "~/Admin/Company";
if (HttpContext.Current.Request.Path.Contains(loginPageCompany.Substring(loginPageCompany.LastIndexOf("/") + 1)) == false
&& HttpContext.Current.Request.Path.Contains("Company") == false
&& HttpContext.Current.Request.Path.Contains("AddCompany.aspx") == false
&& HttpContext.Current.Request.Path.Contains("BillingCompanyReport.aspx") == false
&& HttpContext.Current.Request.Path.Contains("UserCompanyMapping.aspx") == false
&& HttpContext.Current.Request.Path.Contains("AddCompany.aspx") == false
&& HttpContext.Current.Request.Path.Contains("AddBranch.aspx") == false
&& HttpContext.Current.Request.Path.Contains("InvoceHeadSettings.aspx") == false
)
{
HttpContext.Current.Response.Redirect(loginPageCompany, true);
}
else
{
return;
}
}
var loginPage = WebConfigurationManager.AppSettings["LoginPagePath"] ?? "~/Login?Session=Expire";
if (HttpContext.Current.Request.Path.Contains(loginPage.Substring(loginPage.LastIndexOf("/") + 1)) == false)
{
HttpContext.Current.Response.Redirect(loginPage, true);
}
else
{
}
}
}
}
#endregion
}
3) Web.config
<system.web>
<httpModules>
<add name="MyPreInitModule" type="PreInitModule" />
</httpModules>
</system.web>
<system.webServer>
<modules>
<add name="MyPreInitModule" type="PreInitModule" />
</modules>
</system.webServer>
Wednesday 20 September 2017
Convert JSON String to DataTable in ASP.Net
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Text.RegularExpressions;
- public class ConvertJsonStringToDataTable
- {
- public DataTable JsonStringToDataTable(string jsonString)
- {
- DataTable dt = new DataTable();
- string[] jsonStringArray = Regex.Split(jsonString.Replace("[", "").Replace("]",""), "},{");
- List<string> ColumnsName = new List<string>();
- foreach (string jSA in jsonStringArray)
- {
- string[] jsonStringData = Regex.Split(jSA.Replace("{", "").Replace("}", ""),",");
- foreach (string ColumnsNameData in jsonStringData)
- {
- try
- {
- int idx = ColumnsNameData.IndexOf(":");
- string ColumnsNameString = ColumnsNameData.Substring(0, idx - 1).Replace("\"", "");
- if (!ColumnsName.Contains(ColumnsNameString))
- {
- ColumnsName.Add(ColumnsNameString);
- }
- }
- catch (Exception ex)
- {
- throw new Exception(string.Format("Error Parsing Column Name : {0}", ColumnsNameData));
- }
- }
- break;
- }
- foreach (string AddColumnName in ColumnsName)
- {
- dt.Columns.Add(AddColumnName);
- }
- foreach (string jSA in jsonStringArray)
- {
- string[] RowData = Regex.Split(jSA.Replace("{", "").Replace("}", ""), ",");
- DataRow nr = dt.NewRow();
- foreach (string rowData in RowData)
- {
- try
- {
- int idx = rowData.IndexOf(":");
- string RowColumns = rowData.Substring(0, idx - 1).Replace("\"", "");
- string RowDataString = rowData.Substring(idx + 1).Replace("\"", "");
- nr[RowColumns] = RowDataString;
- }
- catch (Exception ex)
- {
- continue;
- }
- }
- dt.Rows.Add(nr);
- }
- return dt;
- }
- }
Subscribe to:
Posts (Atom)
Upload valid file in C#
protected bool CheckFileExtandLength(HttpPostedFile HtmlDocFile) { try { Dictionary<string, byte[]>...
-
Step 1: Open URL: https://portal.azure.com/#home Step2 : Click Manage Azure Active Director Step3: Click Enterprise Application Step 4: Ne...
-
Run the script file Open a command prompt window. In the Command Prompt window, type: sqlcmd -S myServer\instanceName -i C:\myScript.sq...
-
Injection Injection flaws, such as SQL injection, LDAP injection, and CRLF injection, occur when an attacker sends untrusted data to an ...