Tuesday, September 7, 2010

Use the tool aspnet_regiis.exe to encrypt and decrypt connection string in web.config

We place connection string in web.config like:

< ?xml version="1.0"?>
< configuration xmlns="http..... ">
< appSettings/>
< connectionStrings>

< connectionStrings/>
< system.web>
...
< /system.web>
< /configration>

We can use Visual Studio Command prompt to run the following command to encrypt and decrypt connection string in web.config:

aspnet_regiis -pef "connectionStrings" "C:\....\EncryptWebSite"

aspnet_regiis -pdf "connectionStrings" "C:\....\EncryptWebSite"

ADO.net Connection Pool (2)

Something more about Connection Pool:

1. Connection Polling is the process of reusing existing active connections instead of creating new connections when a request is made to the database.

2. When the connection manager recieves a request for a new connection, it checks its pool for available connections. If a connection is available, it returns, if no connections available, and the maximum pool size has not been reached, a new connection is created and returned.

3. If the maximal connection pool size has been reached, the connection requested is added to the queue.

4. Minimal pool size default is 0. When your application requires consistence, sometimes we set this value to be 5 even the application is inactive for long period of time.

5. Maximal pool size default 100.

6. To implement connection pooling. The connection string must be exactly the same, case sensitive and even space in the string should be the same.

7. The user ID must be the same for every user or service in the pool.

8. The process ID must be the same, it is impossible to share connection across processes.

9. Where is pool located? Connection pool is a client side technology, which means the database doesn't know anything about the connection pools involved in your application.

10. When is pool created? When the first connection is instantiated, a connection pool group is created, but the first connection pool is not created until the first connection is opened.

Thursday, September 2, 2010

StreamReader read txt file from a http link

using System;
using System.IO;
using System.Net;

WebRequest request = WebRequest.Create(url);
request.Credentials = new NetworkCredential("username", "password");
//if there is credential.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader sr
= new StreamReader(response.GetResponseStream()))
{
string tstr = sr.ReadToEnd();
......
}

Sort Multiple Columns of DataTable using DataView object

DataTable's View has Sort Property.
During my work, I want to build a gridview which has Datatable sorted by first column, and then the second column, and then the third column.

I write the code like following:

Drage a GridView Control to the desinger with name GridView1.

GridView1.AutoGenerateColumns = false;

BoundField bf1 = new BoundField();
bf1.HeaderText = "Company Name";
bf1.DataField = "Company Name";
GridView1.Columns.Add(bf1);


BoundField bf2 = new BoundField();
bf2.HeaderText = "First Name";
bf2.DataField = "First_Name";
GridView1.Columns.Add(bf2);

BoundField bf3 = new BoundField();
bf3.HeaderText = "Last Name";
bf3.DataField = "Last_Name";
GridView1.Columns.Add(bf3);

BoundField bf4 = new BoundField();
bf4.HeaderText = "Salary";
bf4.DataField = "Salary";
GridView1.Columns.Add(bf4);

//The above code to build a GridView can also written in page sources

DataTable dt = new DataTable();

dt.Columns.Add("Company Name");
dt.Columns.Add("First_Name");
dt.Columns.Add("Last_Name");
dt.Columns.Add("Salary");
dt.Columns["Salary"].DataType = typeof(int);


DataRow dr1 = dt.NewRow();
dr1["Company Name"] = "Microsoft";
dr1["First_Name"] = "John";
dr1["Last_Name"] = "Smith";
dr1["Salary"] = 700000;
dt.Rows.Add(dr1);

DataRow dr2 = dt.NewRow();
dr2["Company Name"] = "Google";
dr2["First_Name"] = "Jay";
dr2["Last_Name"] = "Wan";
dr2["Salary"] = 1000000;
dt.Rows.Add(dr2);

DataRow dr3 = dt.NewRow();
dr3["Company Name"] = "Microsoft";
dr3["First_Name"] = "Eric";
dr3["Last_Name"] = "White";
dr3["Salary"] = 750000;
dt.Rows.Add(dr3);

DataRow dr4 = dt.NewRow();
dr4["Company Name"] = "Apple";
dr4["First_Name"] = "Jessica";
dr4["Last_Name"] = "Black";
dr4["Salary"] = 800000;
dt.Rows.Add(dr4);

DataRow dr5 = dt.NewRow();
dr5["Company Name"] = "Apple";
dr5["First_Name"] = "Jessica";
dr5["Last_Name"] = "White";
dr5["Salary"] = 500000;
dt.Rows.Add(dr5);

DataRow dr6 = dt.NewRow();
dr6["Company Name"] = "Apple";
dr6["First_Name"] = "Jessica";
dr6["Last_Name"] = "White";
dr6["Salary"] = 1500000;
dt.Rows.Add(dr6);

dt.DefaultView.Sort = "Company Name ASC,First_Name ASC,Last_Name DESC,Salary DESC";

GridView1.DataSource = dt;

GridView1.DataBind();

The results is like:



Notice, the DataView has the RowFilter, RowStateFilter Properties.

We can add:

dt.DefaultView.RowFilter="First_Name like 'J%' and Salary>600000" to display the data which has the first name like 'J%' and Salary>600000.(It is T sql where clause).

Monday, August 30, 2010

Use Button CommandName, CommandArgument Property to dynamically Call Button Command Events

Button Server Control has two types: submit and command. By default a button is a submit button which doesn't have commandname, commandArguments, and it has button.Click events as the default event. We can create command button using the following steps.

Create a Web Application, in the designer drag 4 buttons:

Set the CommandName Property to be: btn1Command, btn2Command, btn3Command, btn4Command. And CommandArgument to be: Asc, Desc, Upper, Lower.

The Source is like:

< body>
< id="form1" runat="server" method="post" action="Default.aspx">
  < id="Button1" runat="server" commandname="btn1Command" text="NAsc" commandargument="Asc">
   < id="Button2" runat="server" commandname="btn2Command" text="NDesc" commandargument="Desc">
   < id="Button3" runat="server" commandname="btn3Command" text="LUpper" commandargument="Upper">
   < id="Button4" runat="server" commandname="btn4Command" text="LLower" commandargument="Lower">
  < /form>
< /body>

In the Events window, set these 4 buttons Command to be: Button_Command, double click, and write in the Button_Command Event the following code:

protected void Button_Command(object sender, CommandEventArgs e)
{
 switch (e.CommandName)
 {
   case "btn1Command":
   ShowNumbers(e.CommandArgument);
   break;
   case "btn2Command":
   ShowNumbers(e.CommandArgument);
   break;
   case "btn3Command":
   ShowLetters(e.CommandArgument);
   break;
   case "btn4Command":
   ShowLetters(e.CommandArgument);
   break;
  }
}

And Write the ShowNumbers, ShowLetters Methods:

private void ShowNumbers(object commandArgument)
{
  if (commandArgument.ToString() == "Asc")
  {
   Response.Write("123");
  }
  else if (commandArgument.ToString() == "Desc")
  {
   Response.Write("321");
  }
}

private void ShowLetters(object commandArgument)
{
  if (commandArgument.ToString() == "Upper")
  {
   Response.Write("AAA");
  }
  else if (commandArgument.ToString() == "Lower")
  {
   Response.Write("aaa");
  }
}

Build the web application to test the button commands. The sample code is at:

http://groups.google.com/group/jiezhu0815myfiles/web/ButtonCommandSample.rar

Wednesday, August 25, 2010

Array.Find(), Array.FindLast() and Array.FindAll()

static void Main(string[] args)
{
string[] arr = new string[5] { "tests", "Google", "good","yes","grey" };

Console.WriteLine(Array.Find(arr, StartwithG));
Console.WriteLine(Array.FindLast(arr, StartwithG));

string[] arrstartwithg = Array.FindAll(arr, StartwithG);
foreach (string str in arrstartwithg)
{
Console.WriteLine(str);
}

}

static bool StartwithG(string str)
{
if (str.ToLower().StartsWith("g"))
return true;
else return false;
}

Compare Array has equalent element

string[] arr1=new string[3]{"a","b","c"}
string[] arr2=new string[3]{"a","b","c"}

arr1==arr2 //false
arr1.Equals(arr2) //false
arr1.SequenceEqual(arr2) //true