Components using ASP.NET,C#
Just Create a class file for Components.cs in ASP.NET Application
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//Import Namespaces
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System.Collections.Generic;
public class Components
{
#region Member variables for Sql Connections
SqlConnection con;
SqlCommand cmd;
SqlDataReader dr;
DataSet ds = new DataSet();
DataTable dt = new DataTable();
SqlDataAdapter da;
static int m, n, p;
string Day, Month;
string[,] temporary;
string[] t;
#endregion
#region Constructor
public Components()
{
this.OpenConnection();
}
#endregion
#region Destructor
~Components()
{
this.CloseConnection();
}
#endregion
#region Open the Connection
public void OpenConnection()
{
try
{
string s = ConfigurationManager.AppSettings.Get(“SqlConnection”);
this.con = new SqlConnection(s);
this.con.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString() + “Exception Caught While Open the Connection”);
}
}
#endregion
#region Close the Connection
public void CloseConnection()
{
try
{
if (this.con.State != ConnectionState.Open)
this.con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString() + “Exception Caught While Closing the Connection”);
}
}
#endregion
#region InsertUpdateDelete
public Array InsertUpdateDelete(string[,] MyInParam, string MySpName)
{
try
{
cmd = this.con.CreateCommand();
cmd.CommandText = MySpName.ToString();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
for (int i = 0; i < MyInParam.Length / 2; i++)
{
cmd.Parameters.AddWithValue(MyInParam[i, 0], MyInParam[i, 1]);
}
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
n = dt.Rows.Count;
m = dt.Columns.Count;
p = n * m;
int k = 0;
t = new string[p];
int j = 0;
if (p == 0)
{
return null;
}
else
{
for (int i = 0; (i <= n – 1) && (k < p); i++)
{
t[k] = dt.Rows[i][j].ToString();
if ((i == n – 1) && (k <= p))
{
j++;
i = -1;
}
k++;
}
return t;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString() + “Exception Caught while InsertUpdateDelete”);
return (t);
}
}
#endregion
#region DataSetSelect
public DataSet DataSetSelect(string[,] Myinparam, string Myspname)
{
try
{
cmd = this.con.CreateCommand();
cmd.CommandText = Myspname.ToString();
cmd.CommandType = CommandType.StoredProcedure;
for (int i = 0; i < Myinparam.Length / 2; i++)
{
cmd.Parameters.AddWithValue(Myinparam[i, 0], Myinparam[i, 1]);
}
da = new SqlDataAdapter(cmd);
da.Fill(ds);
return (ds);
}
catch (Exception ex)
{
string sss = ex.Message.ToString();
MessageBox.Show(ex.Message.ToString() + “Exception Caught while DataSetSelect”);
return (ds);
}
}
#endregion
#region DataTableSelect
public DataTable DataTableSelect(string[,] Myinparam, string Myspname)
{
try
{
cmd = this.con.CreateCommand();
cmd.CommandText = Myspname.ToString();
cmd.CommandType = CommandType.StoredProcedure;
for (int i = 0; i < Myinparam.Length / 2; i++)
{
cmd.Parameters.AddWithValue(Myinparam[i, 0], Myinparam[i, 1]);
}
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
return (dt);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString() + “Exception Caught while DataTableSelect”);
return (dt);
}
}
#endregion
#region DataReaderSelect
public SqlDataReader DataReaderSelect(string[,] Myinparam, string Myspname)
{
try
{
cmd = this.con.CreateCommand();
cmd.CommandText = Myspname.ToString();
cmd.CommandType = CommandType.StoredProcedure;
for (int i = 0; i < Myinparam.Length / 2; i++)
{
cmd.Parameters.AddWithValue(Myinparam[i, 0], Myinparam[i, 1]);
}
dr = cmd.ExecuteReader();
dr.Read();
return (dr);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString() + ” Page => Components => DataReaderSelect”);
return (dr);
}
}
#endregion
#region SelectInOutParam
public Array SelectInOutParam(string[,] MyInParam, string[,] MyOutParam, string MySpName)
{
try
{
string result;
cmd = this.con.CreateCommand();
cmd.CommandText = MySpName.ToString();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
for (int i = 0; i < MyInParam.Length / 2; i++)
{
cmd.Parameters.AddWithValue(MyInParam[i, 0], MyInParam[i, 1]);
}
for (int i = 0; i < MyOutParam.Length / 2; i++)
{
if (MyOutParam[i, 1] == “SqlDbType.VarChar,150″)
{
cmd.Parameters.Add(MyOutParam[i, 0], System.Data.SqlDbType.VarChar, 150);
}
else
{
cmd.Parameters.Add(MyOutParam[i, 0], System.Data.SqlDbType.Int, 4);
}
cmd.Parameters[MyOutParam[i, 0]].Direction = System.Data.ParameterDirection.Output;
}
cmd.ExecuteNonQuery();
temporary = new string[MyOutParam.Length / 2, 2];
for (int i = 0; i < MyOutParam.Length / 2; i++)
{
result = Convert.ToString(cmd.Parameters[MyOutParam[i, 0]].Value);
temporary[i, 0] = MyOutParam[i, 0];
temporary[i, 1] = result;
}
return temporary;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString() + “Exception Caught while SelectInOutParam”);
return temporary;
}
}
#endregion
#region Encrypt(PlainText)
public string Encrypt(string strPlainText)
{
string plainText = strPlainText; // original plaintext
string passPhrase = “Pas5pr@se”; // can be any string
string saltValue = “s@1tValue”; // can be any string
string hashAlgorithm = “SHA1″; // can be “MD5″
int passwordIterations = 2; // can be any number
string initVector = “@1B2c3D4e5F6g7H8″; // must be 16 bytes
int keySize = 128; // can be 256 or 192 or 128
return Encrypt(plainText, passPhrase, saltValue, hashAlgorithm, passwordIterations, initVector, keySize);
}
#endregion
#region Encrypt(Set Key for PlainText)
public string Encrypt(string plainText, string passPhrase, string saltValue, string hashAlgorithm, int passwordIterations, string initVector, int keySize)
{
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
PasswordDeriveBytes password = new PasswordDeriveBytes(
passPhrase,
saltValueBytes,
hashAlgorithm,
passwordIterations);
byte[] keyBytes = password.GetBytes(keySize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform encryptor = symmetricKey.CreateEncryptor(
keyBytes,
initVectorBytes);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,
encryptor,
CryptoStreamMode.Write);
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherTextBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
string cipherText = Convert.ToBase64String(cipherTextBytes);
return cipherText;
}
#endregion
#region Decrypt(PlainText)
public string Decrypt(string strCryptedText)
{
string cipherText = strCryptedText; // original plaintext
string passPhrase = “Pas5pr@se”; // can be any string
string saltValue = “s@1tValue”; // can be any string
string hashAlgorithm = “SHA1″; // can be “MD5″
int passwordIterations = 2; // can be any number
string initVector = “@1B2c3D4e5F6g7H8″; // must be 16 bytes
int keySize = 128; // can be 256 or 192 or 128
return Decrypt(cipherText, passPhrase, saltValue, hashAlgorithm, passwordIterations, initVector, keySize);
}
#endregion
#region Decrypt(Reset Key for CipherText)
public string Decrypt(string cipherText, string passPhrase, string saltValue, string hashAlgorithm, int passwordIterations, string initVector, int keySize)
{
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
PasswordDeriveBytes password = new PasswordDeriveBytes(
passPhrase,
saltValueBytes,
hashAlgorithm,
passwordIterations);
byte[] keyBytes = password.GetBytes(keySize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform decryptor = symmetricKey.CreateDecryptor(
keyBytes,
initVectorBytes);
MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
CryptoStream cryptoStream = new CryptoStream(memoryStream,
decryptor,
CryptoStreamMode.Read);
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes,
0,
plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
string plainText = Encoding.UTF8.GetString(plainTextBytes,
0,
decryptedByteCount);
return plainText;
}
#endregion
public string getDateTime()
{
switch (DateTime.Now.DayOfWeek.ToString())
{
case “Sunday”:
{
Day = “Sun”;
} break;
case “Monday”:
{
Day = “Mon”;
} break;
case “Tuesday”:
{
Day = “Tue”;
} break;
case “Wednesday”:
{
Day = “Wed”;
} break;
case “Thursday”:
{
Day = “Thu”;
} break;
case “Friday”:
{
Day = “Fri”;
} break;
case “Saturday”:
{
Day = “Sat”;
} break;
}
switch (Convert.ToInt32(DateTime.Now.Month.ToString()))
{
case 1:
{
Month = “Jan”;
} break;
case 2:
{
Month = “Mon”;
} break;
case 3:
{
Month = “Mar”;
} break;
case 4:
{
Month = “Apr”;
} break;
case 5:
{
Month = “May”;
} break;
case 6:
{
Month = “Jun”;
} break;
case 7:
{
Month = “Jul”;
} break;
case 8:
{
Month = “Aug”;
} break;
case 9:
{
Month = “Sep”;
} break;
case 10:
{
Month = “Oct”;
} break;
case 11:
{
Month = “Nov”;
} break;
case 12:
{
Month = “Dec”;
} break;
}
string GenDateTime = Day + ” ” + Month + ” ” + DateTime.Now.Day.ToString() + “, ” + DateTime.Now.Year.ToString() + ” ” + DateTime.Now.ToShortTimeString().ToString();
return (GenDateTime);
}
#region Simple Encoding and Decoding
public string Encode(string InputText)
{
int i;
char CurrentCharacter;
int CurrentCharacterCode;
string EncodedText = “”;
//Iterate through the length of the input parameter
for (i = 0; i < InputText.Length; i++)
{
//Convert the current character to a char
CurrentCharacter = System.Convert.ToChar(InputText.Substring(i, 1));
//Get the character code of the current character
CurrentCharacterCode = (int)CurrentCharacter;
//Modify the character code of the character, – this
//so that “a” becomes “n”, “z” becomes “m”, “N” becomes “Y” and so on
if (CurrentCharacterCode >= 97 && CurrentCharacterCode <= 109)
{
CurrentCharacterCode = CurrentCharacterCode + 13;
}
else
if (CurrentCharacterCode >= 110 && CurrentCharacterCode <= 122)
{
CurrentCharacterCode = CurrentCharacterCode – 13;
}
else
if (CurrentCharacterCode >= 65 && CurrentCharacterCode <= 77)
{
CurrentCharacterCode = CurrentCharacterCode + 13;
}
else
if (CurrentCharacterCode >= 78 && CurrentCharacterCode <= 90)
{
CurrentCharacterCode = CurrentCharacterCode – 13;
}
//Add the current character to the string to be returned
EncodedText = EncodedText + (char)CurrentCharacterCode;
}
return EncodedText;
}
#endregion
}
VN:F [1.7.4_987]
Rating: 0.0/5 (0 votes cast)
VN:F [1.7.4_987]