Tuesday, June 26, 2012


ASP.net MVC3 Date Formats validation Issued (dd/mm/yyyy vs mm/dd/yyyy) 


Client side  passing date format as "dd/mm/yyyy" format. but model is return format is incorrect its cheeking mm/dd/yyyy. i have found that issue in mvc web application. 
solution

Reason is that web server culture info is used as "en-us" its validate format "mm/dd/yyyy".  then i found that we can change the culture dynamically in the following way .

Option 1: Page level

The Culture used by the individual Page can be set via the Culture property.
<%@ Page Culture="en-SG" Language="C#" %>

Option 2: Application wide (web.Config)

The Culture can be set Application wide by adding the  node to the web.Config file. The  node must be placed inside .
<system.web>
  <globalization culture="en-SG"/>

Option 3: Thread level

The Culture of the current Thread can be changed programatically be setting the System.Threading.Thread.CurrentThread.CurrentCulture property.
The following example demonstrates how to get the user-agent(browsers) preferred UserLanguage property on each Request and set the Threads CurrentCulture during the Application_BeginRequest Event.
protected void Application_BeginRequest(Object sender, EventArgs e)
{
    CultureInfo requestCulture;
    try
    {        
        requestCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages[0]);
    }
    catch
    {
        // Return server Culture if none available in HttpHeaders.
        requestCulture = CultureInfo.CurrentCulture;
    }
    System.Threading.Thread.CurrentThread.CurrentCulture = requestCulture;
}

Thursday, May 19, 2011

Mapping SQL Data Base Table with LINQ (Linq to Sql)

Here are Show step to mapping linq query and retrieve data from sql data base

Step 01 : mapping linq collection class with sql dataTabel
i.) import following namespace
using System.Data.Linq;
using System.Data.Linq.Mapping;
ex :- [Table(Name = "D01_Invoice")] //data table Name
public class Invoice
{
private int _invoiceID;
[Column(IsPrimaryKey = true, Storage = "_invoiceID", Name = "D01_id")] //data mapping column Name
public int InvoiceID
{
get
{ return this._invoiceID; }
set { this._invoiceID = value; }
}

private string _invoiceNumber;
[Column(Storage = "_invoiceNumber", Name = "D01_InvoiceNumber", DbType = "numeric(18, 0)")]
public string InvoiceNumber
{
get
{
return this._invoiceNumber;
}
set
{
this._invoiceNumber = value;
}
}
}

step 2:
Create "DataContex" object . so that u have to give connection string or file store path
//string ConnSQL = ConfigurationManager.ConnectionStrings["ConnSQL"].ToString();
// SqlConnection sqlConnection = new SqlConnection(ConnSQL);
//sqlConnection.Open();
DataContext dataContex = new DataContext(sqlConnection);

step 3:
call linq query
IEnumerable invoice = dataContex.GetTable(); //it can be list
those are the basic step to mapping data table and getting data table using linq

step 4 :
if u need select data from linq data collection use following way
select query
IEnumerable empQuery =
from emp in Invoice
select emp;

filter Query
IEnumerable empQuery =
from emp in employees
where emp.Department == "IT Department" &&
emp.City == "Redmond"
select emp;

Order Query
IEnumerable empQuery =
from emp in employees
orderby emp.Department
select emp;

Sample code for Group

Sample of C# Code

Sample Code for Join

IEnumerable invoice = dataContex.GetTable();
IEnumerable invoiceDetails = dataContex.GetTable();

var invo = from inv in invoice
join details in invoiceDetails on inv.InvoiceNumber equals details.InvoiceNumberDetails into invDetails
select new { id= inv.InvoiceID,
invoiceNum = inv.InvoiceNumber,
invoiceDetails = invDetails};
foreach (var inv in invo)
{
Console.WriteLine(inv.invoiceNum);
foreach (var indetails in inv.invoiceDetails)
{
Console.WriteLine(indetails.InvoiceNumberDetails + indetails.ItemCode);
}
}

Monday, January 11, 2010

How to configure IIS to make it aware of ASP.NET

I was Install IIS but I couldn't Deploy ASp.net web Site in my IIS sever. It will show error massage it couldn't know ASP.net tag. But i already installed .net frame work and SDK in my machine. Error is is not enable ASP.net to the web server, follow the following step to enable that

Search aspnet_regiis.exe this file and double click it will automatically configure IIS to make aware of ASP.Net. this exe file include inside the C:\WINDOWS\Microsoft.NET\Framework folder,

Page Redirect after Session expired

Redirect loging page after session expired we can check the session variable and if its null can be redirect.
ex. in page load use this code

protected void page_load(...)
{
if(Session["sesionName"] == null) // cheack session is exist or not
{
page.redirect("login.aspx");// redirect required page
}
}

note :- when we use Globle.aspx file before session expired it session_end method execute. even though we cannot use page redirect withing that method.

web page redirect just before session expired

After session expired it have many was to page redirect. but when we are going to page redirect according to the session variable, it be a problem to detect session variable and page redirect, so that we need to write java script to page load to count time and set page timeout time. then we can detect the time of page ideal time and can be get session values and redirect the page what we want.

before use below function u need to set session timeout time duration within your web application. Default timeout time is 20min in asp.net; in the web config file you can be able to set time duration what you need.

below example is used to page redirect according to session variable in the session and it will execute before the page timeout less than 1 min;

Step 01 :- Set session time out using web config


Step 02 :- Reset time Count at page load and start counter
call check SessionTimeout() function at page Load;

private void CheckSessionTimeout()
{
string msgSession = "Warning: Within next 2 minutes, if you do not do anything, session will be expired.";

int int_MilliSecondsTimeReminder = (this.Session.Timeout * 60000) - 2 * 60000;
int int_MilliSecondsTimeOut = (this.Session.Timeout * 60000) - 1 * 60000;
string redirectUrl = ResolveUrl("~/BrowserClose.aspx");

string str_Script = @"
var myTimeReminder, myTimeOut;
clearTimeout(myTimeReminder);
clearTimeout(myTimeOut); " +
"var sessionTimeReminder = " +
int_MilliSecondsTimeReminder.ToString() + "; " +
"var sessionTimeout = " + int_MilliSecondsTimeOut.ToString() + ";" +
"function doReminder(){var user; user ='" + Convert.ToString(Session["VaribleName"])+
"'; if (user !='" + string.Empty + "'){ alert('" + msgSession + "');} }" +
"function doRedirect(){var user; user ='" + Convert.ToString(Session["VaribleName"]) +
"'; if (user !='" + string.Empty + "'){alert('Session has expired !');window.location.href='" + redirectUrl + "'; }}" + @"

myTimeOut=setTimeout('doRedirect()', sessionTimeout); ";

//myTimeReminder=setTimeout('doReminder()', sessionTimeReminder); remove alert before session expired;
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(),
"CheckSessionOut", str_Script, true);
}

Wednesday, December 9, 2009

Responce.redirect(url) ThreadAbortException occured

The ThreadAbortException is thrown when you make a call to Response.Redirect(url) because the system aborts processing of the current web page thread after it sends the redirect to the response stream.

use following way to solve ThreadAbortException :-

//set globle varible as
private bool m_bIsTerminating = false;
protected void NavigateTo(string url)
{
Response.Redirect(url, false);
HttpContext.Current.ApplicationInstance.CompleteRequest();
m_bIsTerminating = true;
}

protected override void RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument)
{
if (m_bIsTerminating == false)
base.RaisePostBackEvent(sourceControl, eventArgument);
}

protected override void Render(HtmlTextWriter writer)
{
if (m_bIsTerminating == false)
base.Render(writer);
}