Hello Everyone…..

Myself Ramesh Sai working as a Software Developer in a company located in Hyderabad.I like coding and learning new techniques.I am having good command on C#,ASP.NET,SQL server,MVC,LINQ,Entity Framework,JSON

Advertisements

Cookies Handling

Creating Cookie : window.localStorage.setItem(“Cookiename”, value);

Getting Cookie Value : window.localStorage.getItem(“Cookiename”);

Clearing particular Cookie Value : localStorage.clear(“Cookiename”);

Clearing all Cookie Values : localStorage.clear();

 

Date Formatting in C#

Eg: @DateTime.Now.ToString(“hh:mm:ss.fff”)
Specifier Description Output
d Short Date 08/04/2007
D Long Date 08 April 2007
t Short Time 21:08
T Long Time 21:08:59
f Full date and time 08 April 2007 21:08
F Full date and time (long) 08 April 2007 21:08:59
g Default date and time 08/04/2007 21:08
G Default date and time (long) 08/04/2007 21:08:59
M Day / Month 08 April
r RFC1123 date Sun, 08 Apr 2007 21:08:59 GMT
s Sortable date/time 2007-04-08T21:08:59
u Universal time, local timezone 2007-04-08 21:08:59Z
Y Month / Year April 2007
dd Day 08
ddd Short Day Name Sun
dddd Full Day Name Sunday
hh 2 digit hour 09
HH 2 digit hour (24 hour) 21
mm 2 digit minute 08
MM Month 04
MMM Short Month name Apr
MMMM Month name April
ss seconds 59
fff milliseconds 120
FFF milliseconds without trailing zero 12
tt AM/PM PM
yy 2 digit year 07
yyyy 4 digit year 2007
: Hours, minutes, seconds separator, e.g. {0:hh:mm:ss} 09:08:59
/ Year, month , day separator, e.g. {0:dd/MM/yyyy} 08/04/2007
. milliseconds separator

Get Paths and URL fragments from the HttpRequest object

Input: http://localhost:96/Cambia3/Temp/Test.aspx?q=item#fragment

Some HttpRequest path and URL properties:
Request.ApplicationPath: /Cambia3
Request.CurrentExecutionFilePath: /Cambia3/Temp/Test.aspx
Request.FilePath: /Cambia3/Temp/Test.aspx
Request.Path: /Cambia3/Temp/Test.aspx
Request.PathInfo:
Request.PhysicalApplicationPath: D:\Inetpub\wwwroot\CambiaWeb\Cambia3\
Request.RawUrl: /Cambia3/Temp/Test.aspx?query=arg
Request.Url.AbsolutePath: /Cambia3/Temp/Test.aspx
Request.Url.AbsoluteUri: http://localhost:96/Cambia3/Temp/Test.aspx?query=arg
Request.Url.Fragment:
Request.Url.Host: localhost
Request.Url.Authority: localhost:96
Request.Url.LocalPath: /Cambia3/Temp/Test.aspx
Request.Url.PathAndQuery: /Cambia3/Temp/Test.aspx?query=arg
Request.Url.Port: 96
Request.Url.Query: ?query=arg
Request.Url.Scheme: http
Request.Url.Segments: /Cambia3/Temp/Test.aspx

Input: http://localhost:96/Cambia3/Temp/Test.aspx/path/info?q=item#fragment

Some HttpRequest path and URL properties:
Request.ApplicationPath: /Cambia3
Request.CurrentExecutionFilePath: /Cambia3/Temp/Test.aspx
Request.FilePath: /Cambia3/Temp/Test.aspx
Request.Path: /Cambia3/Temp/Test.aspx/path/info
Request.PathInfo: /path/info
Request.PhysicalApplicationPath: D:\Inetpub\wwwroot\CambiaWeb\Cambia3\
Request.RawUrl: /Cambia3/Temp/Test.aspx/path/info?query=arg
Request.Url.AbsolutePath: /Cambia3/Temp/Test.aspx/path/info
Request.Url.AbsoluteUri: http://localhost:96/Cambia3/Temp/Test.aspx/path/info?query=arg
Request.Url.Fragment:
Request.Url.Host: localhost
Request.Url.LocalPath: /Cambia3/Temp/Test.aspx/path/info
Request.Url.PathAndQuery: /Cambia3/Temp/Test.aspx/path/info?query=arg
Request.Url.Port: 96
Request.Url.Query: ?query=arg
Request.Url.Scheme: http
Request.Url.Segments: /Cambia3/Temp/Test.aspx/path/info

Display Uploaded images using Jquery

 

1)script part:

$(function () {
$(“#fileUpload”).change(function () {
if (typeof (FileReader) != “undefined”) {
var dvPreview = $(“#dvPreview”);
dvPreview.html(“”);
var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png|.bmp)$/;
$($(this)[0].files).each(function () {
var file = $(this);
if (regex.test(file[0].name.toLowerCase())) {
var reader = new FileReader();
reader.onload = function (e) {
var img = $(““);
img.attr(“style”, “height:50px;width:50px max-height: 75px;border: 2px solid;margin: 10px 10px 0 0;padding: 1px;”);
img.attr(“src”, e.target.result);
img.addClass();
dvPreview.append(img);
}
reader.readAsDataURL(file[0]);
} else {
alert(file[0].name + ” is not a valid image file.”);
dvPreview.html(“”);
return false;
}
});
} else {
alert(“This browser does not support HTML5 FileReader.”);
}
});
});

2)Add new div with id=”dvPreview”

Output:-

Capture

steps to get current logged in user ID C#

 

Step 1: After Login Get User details and store in a list.
List<LoginUsers> loginUsers = new  List<LoginUsers>

step 2: Serialize the list and create cookie

string userData = JsonConvert.SerializeObject(loginUsers);

ValidUser.SetAuthCookie(userData, userMaster.ID);

public static void SetAuthCookie(string userResponse, string userId)
{
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
userId,
DateTime.Now,
DateTime.Now.AddMinutes(15),
false, //pass here true, if want to implement remember me functionality
userResponse); // the path for the cookie
string encryptedTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
cookie.HttpOnly = true;
HttpContext.Current.Response.Cookies.Add(cookie);
}

step 3: Open Global.asax and paste this

protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null && !string.IsNullOrEmpty(authCookie.Value))
{
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
if (authTicket != null)
{
var serializeModel = JsonConvert.DeserializeObject<UserMaster>(authTicket.UserData);
var newUser = new CustomPrinciple(authTicket.Name)
{
//Mention Required Fields for eg
ID = serializeModel.ID
};
HttpContext.Current.User = newUser;
}
}
}
step 4:Create a class file with name customprinciple and mention required fields

That’s it Now Application is Authenticated and logged in user ID can be accessed using

var user = (CustomPrinciple)System.Web.HttpContext.Current.User;

string ID = user.ID

for Authentication

if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
//your code
}

That’s It Folks!!! Have a nice day

Query to get only numbers from a string in SQL Query

declare @getstringvalue nvarchar(20)
set @getstringvalue = N’pod123fgfd4h5h6h78h6h9hh4gg45hg5gh45g’

WHILE PATINDEX(‘%[^0-9]%’, @getstringvalue ) > 0 SET @getstringvalue = REPLACE(@getstringvalue , SUBSTRING(@getstringvalue , PATINDEX(‘%[^0-9]%’, @getstringvalue ), 1), ” )

SELECT @getstringvalue as output

or

short & simple process:-

select MAX(CAST(REPLACE(REPLACE(column_name , ‘string part in column’, ”), ”, ”) as int)) from table_name