Thursday, November 26, 2009

script test

  



Sunday, November 15, 2009

Convert DataSet into ArrayList in C#

Converting the DataSet into ArrayList is not an big deal anymore.
If you are sure about the number of columns in the DataTable,create the same class members in the Class.

Let assume I have an DataSet contains of the table named "Product" which has three columns called ProductID and Stock.

First step to convert the DataSet into ArrayList is,creating the Class name ProductClass



Database db = DatabaseFactory.CreateDatabase("DatabaseConnectionString");
DbCommand cmd = db.GetSqlStringCommand("Select * from Products");
DataSet ds = db.ExecuteDataSet(cmd);
List proList = new List();

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
Products p = new Products();
p.ProductName = ds.Tables[0].Rows[i]["ProductName"].ToString();
p.ProductID = ds.Tables[0].Rows[i]["ProductID"].ToString();
proList.Add(p);

}


Product.cs

public class Products
{
private string _productName;

private string _productid;

public string ProductName
{
get { return _productName; }
set { _productName = value; }


}

public string ProductID
{
get { return _productid; }
set { _productid = value; }

}



}

GridView1.DataSource = proList.ToArray();
GridView1.DataBind();

Wednesday, November 11, 2009

LINQ to SQL Classes tutorial

Step 1:

Add Linq to Sql classes to your application,and make sure you selected the .NET 3.5 as target framework.



Step : 2

Add Server database to solution explorer.After adding Database diagram toggle to find the tables on selected database.



Step :3

Linq to SQL template name will becomes the parent class and retrieve the data from the table as below.



Snippet:

DataClasses2DataContext db2 = new DataClasses2DataContext();
var c = from sp in db.Peoples
select sp;
List li = c.ToList();
GridView1.DataSource = li.ToArray();
GridView1.DataBind();

Monday, November 02, 2009

Basic LINQ in C#

var c = new List left arrow bracket Customers right arrow bracket {
new Customers{Code=20,Name="Murugesan"},
new Customers{Code=21,Name="Pandian"},
new Customers{Code=22,Name="Senthilan"},
new Customers{Code=20,Name="Muthu"},
new Customers{Code=20,Name="Sachin"},
};
var result = from sp in c
select sp;
foreach (var Result in result)
{
Response.Write(Result.Name.ToString());
}


Class implementation

public class Customers
{
private int _code;
private string _name;
public int Code
{
get { return _code; }
set { _code = value; }
}

public string Name
{
get { return _name; }
set { _name = value; }
}
}

Sunday, November 01, 2009

Encrypting-Decrypting the password in C#

Encrypting the password in .NET become ease,DESCryptoServiceProvider enables the developer to encrypt the plain text in to encrypted form.

Code snippet as below :


public static byte[] Encrypt(string txtPassword, SymmetricAlgorithm key)
{
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, key.CreateEncryptor(), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cStream);
sw.WriteLine(txtPassword);
sw.Close();
cStream.Close();
byte[] buffer = mStream.ToArray();
mStream.Close();
return buffer;
}


Decrypting the encrypted bytes:

public static string Decrypt(byte[] CryptedBytes, SymmetricAlgorithm key)
{
MemoryStream mStream = new MemoryStream(CryptedBytes);
CryptoStream cStream = new CryptoStream(mStream, key.CreateDecryptor(), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cStream);
string PlainTxt = sr.ReadLine();
sr.Close();
cStream.Close();
mStream.Close();
return PlainTxt;
}

In Database,I used to save these bytes in Password column.While retrieving/Checking the Password,I used the Decrypt the function.So Password column will have encrypted text that easily human can't read or memorize.

For using this code you need to import System.Security.Cryptography class into your application.