Wednesday, September 28, 2011

How to used Cultures of different language in ASP.net

1 comments Posted by Rahul Kharde at 3:36 AM
We want develop a system in different languages. Naturally, all the content of these sites will be in the respective language. The content includes static text, form labels, prompts, error messages and similar things. In addition you would also like to take care of things such as date formats and number formats. Typically you will choose one of the following approaches to develop such web sites

The culture can be set for a thread using Thread.CurrentThread.CurrentCulture property.

using System.Threading.CurrentThread

The culture can be set for a thread using Thread.CurrentThread.CurrentCulture property.

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

HTML

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<%=DateTime.Now.ToLongDateString() %>

<br />

<%=dt.ToLongDateString() %>

<br />

<asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC"

BorderColor="#FFCC66" BorderWidth="1px" DayNameFormat="Shortest"

Font-Names="Verdana" Font-Size="8pt" ForeColor="#663399" Height="112px"

ShowGridLines="True" Width="125px">

<SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />

<SelectorStyle BackColor="#FFCC66" />

<TodayDayStyle BackColor="#FFCC66" ForeColor="White" />

<OtherMonthDayStyle ForeColor="#CC9966" />

<NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />

<DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />

<TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt"

ForeColor="#FFFFCC" />

</asp:Calendar>

<br />

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"

onselectedindexchanged="DropDownList1_SelectedIndexChanged">

<asp:ListItem Value="en-US" Selected="True">English</asp:ListItem>

<asp:ListItem Value="es-MX">EspaƱol</asp:ListItem>

<asp:ListItem Value="de-DE">Deutsch</asp:ListItem>

<asp:ListItem Value="mr-IN">Marathi</asp:ListItem>

<asp:ListItem Value="hi-IN">Hindi (India)</asp:ListItem>

<asp:ListItem Value="gu-IN">Gujarati (India)</asp:ListItem>

<asp:ListItem Value="ja-JP">Japanese </asp:ListItem>

</asp:DropDownList>

</div>

</form>

</body>

</html>

Code

public static DateTime dt = new DateTime();

protected void Page_Load(object sender, EventArgs e)

{

}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

{

dt = (DateTime)Calendar1.SelectedDate;

System.Threading; Thread.CurrentThread.CurrentCulture = new CultureInfo(DropDownList1.SelectedItem.Value.ToString());

}

For English (en-US) Culture setting

For Marathi-India (mr-IN) Culture setting

Download

Download Code Here…

Read More »

Tuesday, September 27, 2011

What is Page Output Caching with Example?

0 comments Posted by Rahul Kharde at 3:36 AM
In this article, we will take a look at the Page Output Cache directive, which is by far the easiest way of caching content with ASP.NET

What is Cache?
A cache is a type of dynamic and high speed memory that is used to supplement the function of the central processing unit and the physical disk storage. The cache acts as a buffer when the CPU tries to access data from the disk so the data traveling from the CPU and physical disks can have synchronized speed. Disk reading and writing process is generally slower than CPU function.
ASP.NET introduces another key/value pair object Cache. In addition to simply storing key/value pairs, Cache adds additional functionality specifically designed to store transient data:
  • Dependencies: A key added to the Cache can set up dependency relationships that can force that key to be removed from the Cache. The supported dependencies include key, file, and time.
  • Automatic Expiration: Underused items added to the Cache that have no dependencies will automatically expire if they are underused.
  • Support for Callback: Code paths can be added that will be called when an item is removed from the Cache, giving us an opportunity to update the Cache or not remove the item.
Type of Cache
ASP.NET supports three types of caching for Web-based applications:
  • Page Output Cache
  • Page Fragment Cache
  • Data Caching
Page Output Caching

Output Caching is particularly useful when you have very static pages. It is nothing but the HTML output of dynamic requests to ASP.NET Web pages. Each time an incoming ASP.NET page request comes in this engine checks to see if the page being requested has a cached output entry. If it does, this cached HTML is sent as a response; otherwise, the page is dynamically rendered, it's output is stored in the Output Cache engine.

Configure Output Cache for a Single Page in Declarative Way you can control the output cache’s behavior in a declarative way by using the @Output Cache page directive. The only required parameter for the output cache is the Duration all the other parameters can be used if you want to. Let’s describe some of the parameters

Duration: Required. Time, in seconds, the page should be cached. Must be a positive intege

Location: Specifies where the output should be cached. If specified, must be one of: Any, Client, Downstream, None, Server or Server and Client.

VaryByParam: Required. The names of the variables in the Request, which should result in, separate cache entries. "none" can be used to specify no variation. "*" can be used to create new cache entries for every different set of variables. Separate variables with ";".

VaryByHeader: Varies cache entries based on variations in a specified header.

VaryByCustom: Allows custom variations to be specified in the global.asax (for example, "Browser").

Syntax
Here is a very simple example of a page which will show us the difference between a cached page and a non-cached page. Try creating a new project, and change the OutputCache.aspx page to contain the following code
<%@ OutputCache Duration="100" VaryByParam="None" %>
HTML
<%@ OutputCache Duration="5" VaryByParam="None" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Output Cache</title>
</head>
<body>
<form id="form1" runat="server">
    <div>
        <asp:Label ID="Label5" runat="server" />
    </div>
</form>
</body>
</html>

Code Behind
protected void Page_Load(object sender, EventArgs e)
{
    Label1.Text = DateTime.Now.ToString();
}
This is all standard stuff, except the line with the DateTime. It simply outputs the current date and time to the page. Try running the project and reload the page a couple of times. As you will see, the time is only refreshed every 5 seconds.



Download
Read More »

Friday, September 23, 2011

Distributed Transaction in ASP.NET with SQL-Server

0 comments Posted by Rahul Kharde at 4:15 AM

A distributed transaction is a transaction that updates data on two or more networked computer systems. Distributed transactions extend the benefits of transactions to applications that must update distributed data. Implementing robust distributed applications is difficult because these applications are subject to multiple failures, including failure of the client, the server, and the network connection between the client and server. In the absence of distributed transactions, the application program itself must detect and recover from these failures.

  • A Distributed Transaction spans multiple data sources.
  • Distributed Transactions enable you to incorporate several distinct operations, which occur on different systems, into an atomic action that either succeeds or fails completely.
  • Distributed Transactions are managed by DTC, i.e., Distributed Transaction Coordinator.

On the server where the trigger resides, you need to turn the MSDTC service on.

You can this by clicking

1. START -> CONTROL PANEL -> ADMINISTRATIVE TOOLS -> SERVICES.

2. Find the service called 'Distributed Transaction Coordinator' and RIGHT CLICK (on it and select) > Start.

We will use, of course, two SQL Server 2000/2005 databases. The first one, named Bank1, will be on the SQL Server "DB_Bank1", and it will contain an Accounts table; this table holds Bank1's accounts. The second database will be Bank2 (on the server " DB_Bank2") and it will contain another Accounts table

For Fist Server (DB_Bank1) create table Bank1

CREATE TABLE [dbo].[Bank1](

[AccID] [int] NOT NULL,

[Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,

[Amt] [money] NULL,

CONSTRAINT [PK_Bank1] PRIMARY KEY CLUSTERED

(

[AccID] ASC

)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]

) ON [PRIMARY]



INSERT INTO Bank1(AccID,Name,Amt)VALUES(1,'UserName1','8000')

INSERT INTO Bank1(AccID,Name,Amt)VALUES(2,'UserName2','7500')

INSERT INTO Bank1(AccID,Name,Amt)VALUES(3,'UserName3','10000')

INSERT INTO Bank1(AccID,Name,Amt)VALUES(4,'UserName4','15700')


AccID

Name

Amt

1

UserName1

8000.00

2

UserName2

7500.00

3

UserName3

10000.00

4

UserName4

15700.00

In Second Server(DB_Bank2)create Table Bank2

CREATE TABLE [dbo].[Bank2](

[AccID] [int] NOT NULL,

[Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,

[Amt] [money] NULL,

CONSTRAINT [PK_Bank2] PRIMARY KEY CLUSTERED

(

[AccID] ASC

)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]

) ON [PRIMARY]


INSERT INTO Bank2(AccID,Name,Amt)VALUES(1,'UserName1','4580')

INSERT INTO Bank2(AccID,Name,Amt)VALUES(2,'UserName2','9650')

INSERT INTO Bank2(AccID,Name,Amt)VALUES(3,'UserName3','14750')


AccID

Name

Amt

1

UserName1

4580.00

2

UserName2

9650.00

3

UserName3

14750.00


HTML

Here we take two dropdown list box which contain the Account No. source account id hold the Fist Server account Holder ID and destination account no hold the second server account holder Id. Grid shows the record of respective banks

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>Untitled Pagetitle>

</head>

<body>

<form id="form1" runat="server">

<div>

<table style="width: 60%; font-family: Verdana; font-size: 10px;">

<tr>

<td style="text-align: right;">

Source Account ID

</td>

<td>

<asp:DropDownList ID="DropDownList1" runat="server" Width="79px">

<asp:ListItem>1asp:ListItem>

<asp:ListItem>2asp:ListItem>

<asp:ListItem>3asp:ListItem>

<asp:ListItem>4asp:ListItem>

</asp:DropDownList>

</td>

<td style="text-align: right;">

Destination Account ID

</td>

<td>

<asp:DropDownList ID="DropDownList2" runat="server" Width="79px">

<asp:ListItem>1asp:ListItem>

<asp:ListItem>2asp:ListItem>

<asp:ListItem>3asp:ListItem>

</asp:DropDownList>

</td>

</tr>

<tr>

<td>

</td>

<td colspan="2" style="text-align: center;">

Amount:

<asp:TextBox ID="TextBox3" runat="server">asp:TextBox>

</td>

<td>

</td>

</tr>

<tr>

<td style="text-align: center;" colspan="4">

<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />

</td>

</tr>

<tr>

<td colspan="2" style="text-align: center;">

<b>Bank 1b>

</td>

<td colspan="2" style="text-align: center;">

<b>Bank 2b>

</td>

</tr>

<tr>

<td colspan="2" style="text-align: center; vertical-align: top;">

<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#DEDFDE"

BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical">

<FooterStyle BackColor="#CCCC99" />

<RowStyle BackColor="#F7F7DE" />

<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />

<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />

<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />

<AlternatingRowStyle BackColor="White" />

</tasp:GridView>

</td>

<td colspan="2" style="text-align: center; vertical-align: top;">

<asp:GridView ID="GridView2" runat="server" BackColor="White" BorderColor="#DEDFDE"

BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical">

<FooterStyle BackColor="#CCCC99" />

<RowStyle BackColor="#F7F7DE" />

<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />

<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />

<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />

<AlternatingRowStyle BackColor="White" />

</asp:GridView>

</td>

</tr>

</table>

</div>

</form>

</body>

</html>



Code

Add the “System.Transactions” reference from “Add reference”

using System.Transactions;

using System.Data.SqlClient;

Declare global Connection string

private static string strConn1;

private static string strConn2;

page load

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

strConn1 = @"Data Source = ServerAddress1;User id = username;pwd = Password;Initial Catalog = DB_Bank1";

strConn2 = @"Data Source = ServerAddress2;User id = username;pwd = Password;Initial Catalog = DB_Bank2";

GridView1.DataSource = (DataSet)this.LoadGrid(strConn1, "Select * from Bank1");

GridView1.DataBind();

GridView2.DataSource = (DataSet)this.LoadGrid(strConn2, "Select * from Bank2");

GridView2.DataBind();

}

}

public DataSet LoadGrid(String StrConn, String SQL)

{

DataSet ds = new DataSet();

try

{

using (SqlConnection Con = new SqlConnection(StrConn))

{

SqlCommand Cmd = new SqlCommand(SQL, Con);

SqlDataAdapter da = new SqlDataAdapter();

da.SelectCommand = Cmd;

da.Fill(ds);

return ds;

}

}

catch (Exception ex)

{

}

return ds;

}

Button click event

protected void Button1_Click(object sender, EventArgs e)

{

try

{

using (TransactionScope scope = new TransactionScope())

{

SqlConnection cnn1 = new SqlConnection(strConn1);

SqlConnection cnn2 = new SqlConnection(strConn2);

cnn1.Open();

cnn2.Open();

SqlCommand cmd1 = new SqlCommand("update Bank1 set amt=amt-" + Convert.ToDouble(TextBox3.Text) + " where AccID='" + Convert.ToInt32(DropDownList1.SelectedItem.Value) + "'", cnn1);

SqlCommand cmd2 = new SqlCommand("update Bank2 set amt=amt+" + Convert.ToDouble(TextBox3.Text) + " where AccID='" + Convert.ToInt32(DropDownList2.SelectedItem.Value) + "'", cnn2);

cmd1.ExecuteNonQuery();

cmd2.ExecuteNonQuery();

cnn1.Close();

cnn2.Close();


GridView1.DataSource = (DataSet)this.LoadGrid(strConn1, "Select * from Bank1");

GridView1.DataBind();

GridView2.DataSource = (DataSet)this.LoadGrid(strConn2, "Select * from Bank2");

GridView2.DataBind();

scope.Complete();

}

}

catch(Exception ex)

{

}

}



Download

Download Code Here..

Read More »
 

Popular Posts

Recent Comments

© 2011. All Rights Reserved | Help to understand .Net | Template by Blogger Widgets

Home | About | Top