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;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System.Xml;
using System.Web.Configuration;
using System.Collections;
/// <summary>
/// Summary description for @base
/// </summary>
///
namespace tally
{
public class @base
{
SqlConnection objCon = new SqlConnection(ConfigurationSettings.AppSettings["strLocalCon"]);
SqlDataAdapter objDA;
SqlCommand objCom;
SqlDataReader objDr;
private string _conString = "";
public string conString
{
set
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
AppSettingsSection appSection = (AppSettingsSection)config.GetSection("appSettings");
if (appSection != null)
_conString = appSection.Settings["strLocalCon"].Value;
}
get { return _conString; }
}
public @base()
{
conString = _conString;
}
// Function to access data set using SP;
public DataSet GetDataSet(SqlCommand objCmd)
{
DataSet objDS = new DataSet();
try
{
//SqlConnection objCon = new SqlConnection(ConfigurationSettings.AppSettings["strLocalCon"]);
SqlDataAdapter objDA = new SqlDataAdapter(objCmd);
objDA.Fill(objDS);
}
catch { }
return objDS;
}
// Function to get value according to querry
public DataSet GetDataTable(string query)
{
DataSet objDS = new DataSet();
try
{
//SqlConnection objCon = new SqlConnection(ConfigurationSettings.AppSettings["strLocalCon"]);
SqlDataAdapter objDA = new SqlDataAdapter(query, conString);
objDA.Fill(objDS);
}
catch { }
return objDS;
}
// Function to execute a querry;
public Boolean ExecuteQuery(string query)
{
Boolean IsSuccess = false;
SqlConnection objCon = new SqlConnection(ConfigurationSettings.AppSettings["strLocalCon"]);
objCom = new SqlCommand();
objCom.CommandText = query;
objCom.Connection = objCon;
try
{
objCon.Open();
objCom.ExecuteNonQuery();
IsSuccess = true;
}
catch { }
finally
{
objCon.Close();
objCon.Dispose();
}
return IsSuccess;
}
// Function to fetch a record from data table..
public SqlDataReader GetDataReader(String query)
{
objCon = new SqlConnection(ConfigurationSettings.AppSettings["strLocalCon"]);
objCom = new SqlCommand();
objCom.CommandText = query;
objCom.Connection = objCon;
objCon.Open();
objDr = objCom.ExecuteReader(CommandBehavior.CloseConnection);
return objDr;
}
// Function to checkRecord exists or not...
public Boolean IsExist(String query)
{
bool exist = false;
objCon = new SqlConnection(ConfigurationSettings.AppSettings["strLocalCon"]);
objCom = new SqlCommand();
objCom.CommandText = query;
objCom.Connection = objCon;
try
{
objCon.Open();
objDr = objCom.ExecuteReader();
objDr.Read();
if (objDr.HasRows)
exist = true;
else
exist = false;
}
catch { exist = false; }
finally
{
objCon.Close();
objCon.Dispose();
}
return exist;
}
// Function to return a single value from database...
//...returns a single String Value;
public String GetValue(String query)
{
string value = string.Empty;
objCon = new SqlConnection(ConfigurationSettings.AppSettings["strLocalCon"]);
objCom = new SqlCommand();
objCom.CommandText = query;
objCom.Connection = objCon;
try
{
objCon.Open();
value = Convert.ToString(objCom.ExecuteScalar());
}
catch { }
finally
{
objCon.Close();
objCon.Dispose();
}
return value;
}
//Function to get System date & time..
// Returns the date in DD/MM/YYYY Hr:Min:Sec AM/PM formate..
public DateTime SysDateTime()
{
DateTime dt = new DateTime();
objCon = new SqlConnection(ConfigurationSettings.AppSettings["strLocalCon"]);
objCom = new SqlCommand();
objCom.CommandText = "Select getdate()";
objCom.Connection = objCon;
try
{
objCon.Open();
dt = (DateTime)objCom.ExecuteScalar();
objCon.Close();
objCon.Dispose();
}
catch { }
finally
{
objCon.Close();
objCon.Dispose();
}
return dt;
}
//Function to get System date only..
// Returns the date in DD/MM/YYYY formate..
public String SysDate()
{
String strdt = String.Empty;
DateTime dt = new DateTime();
objCon = new SqlConnection(ConfigurationSettings.AppSettings["strLocalCon"]);
objCom = new SqlCommand();
objCom.CommandText = "Select getdate()";
objCom.Connection = objCon;
try
{
objCon.Open();
dt = (DateTime)objCom.ExecuteScalar();
}
catch { }
finally
{
objCon.Close();
objCon.Dispose();
}
strdt = dt.ToShortDateString();
return strdt;
}
// Function that fill dropdown without any heading such select
public void FillDropdownListNoSelect(String query, DropDownList objDDL, String colfield, String valField)
{
try
{
DataSet ds = new DataSet();
objDDL.Items.Clear();
//objDDL.Items.Add("Select");
objCon = new SqlConnection(ConfigurationSettings.AppSettings["strLocalCon"]);
objDA = new SqlDataAdapter(query, objCon);
objDA.Fill(ds);
objDDL.DataSource = ds.Tables[0];
objDDL.DataTextField = colfield;
objDDL.DataValueField = valField;
objDDL.DataBind();
}
catch { }
}
public void FillDaTaListNoSelect(String query, ListBox objDDL, String colfield, String valField)
{
try
{
DataSet ds = new DataSet();
objDDL.Items.Clear();
//objDDL.Items.Add("Select");
objCon = new SqlConnection(ConfigurationSettings.AppSettings["strLocalCon"]);
objDA = new SqlDataAdapter(query, objCon);
objDA.Fill(ds);
objDDL.DataSource = ds.Tables[0];
objDDL.DataTextField = colfield;
objDDL.DataValueField = valField;
objDDL.DataBind();
}
catch { }
}
public void fillRadioButtonList(String query, RadioButtonList objDDL, String colfield, String valField)
{
try
{
DataSet ds = new DataSet();
objDDL.Items.Clear();
//objDDL.Items.Add("Select");
objCon = new SqlConnection(ConfigurationSettings.AppSettings["strLocalCon"]);
objDA = new SqlDataAdapter(query, objCon);
objDA.Fill(ds);
objDDL.DataSource = ds.Tables[0];
objDDL.DataTextField = colfield;
objDDL.DataValueField = valField;
objDDL.DataBind();
}
catch { }
}
public void FillDropdownListwithvalue(String query, DropDownList objDDL, String colfield, String valField)
{
try
{
DataSet ds = new DataSet();
objDDL.Items.Clear();
//objDDL.Items.Add("Select");
objCon = new SqlConnection(ConfigurationSettings.AppSettings["strLocalCon"]);
objDA = new SqlDataAdapter(query, objCon);
objDA.Fill(ds);
DataTable newtbl = new DataTable();
DataRow NewRow = ds.Tables[0].NewRow();
NewRow[colfield] = "-Please Select-";
newtbl = ds.Tables[0];
newtbl.Rows.InsertAt(NewRow, 0);
newtbl.AcceptChanges();
newtbl = newtbl.Copy();
objDDL.DataSource = ds.Tables[0];
objDDL.DataTextField = colfield;
objDDL.DataValueField = valField;
objDDL.DataBind();
}
catch { }
}
// Function to bind drop down list box with text field only..
public void FillDropdownList(String query, DropDownList objDDL, String colfield)
{
try
{
DataSet ds = new DataSet();
objDDL.Items.Clear();
//objDDL.Items.Add("Select");
objCon = new SqlConnection(ConfigurationSettings.AppSettings["strLocalCon"]);
objDA = new SqlDataAdapter(query, objCon);
objDA.Fill(ds);
DataTable newtbl = new DataTable();
DataRow NewRow = ds.Tables[0].NewRow();
NewRow[colfield] = "-Please Select-";
newtbl = ds.Tables[0];
newtbl.Rows.InsertAt(NewRow, 0);
newtbl.AcceptChanges();
newtbl = newtbl.Copy();
objDDL.DataSource = ds.Tables[0];
objDDL.DataTextField = colfield;
objDDL.DataBind();
}
catch { }
}
// Function to bind value field and text field both..
public void FillDropdownListValue(String query, DropDownList objDDLval, String columnfield, string Dlvalue)
{
DataSet ds = new DataSet();
objDDLval.Items.Clear();
//objDDL.Items.Add("Select");
objCon = new SqlConnection(ConfigurationSettings.AppSettings["strLocalCon"]);
objDA = new SqlDataAdapter(query, objCon);
objDA.Fill(ds);
DataTable newtbl = new DataTable();
DataRow NewRow = ds.Tables[0].NewRow();
NewRow[columnfield] = "Select";
newtbl = ds.Tables[0];
newtbl.Rows.InsertAt(NewRow, 0);
newtbl.AcceptChanges();
newtbl = newtbl.Copy();
objDDLval.DataSource = ds.Tables[0].DefaultView;
objDDLval.DataTextField = columnfield;
objDDLval.DataValueField = Dlvalue;
objDDLval.DataBind();
}
public void FilllistboxtValue(String query, ListBox objDDLval, String columnfield, string Dlvalue)
{
try
{
DataSet ds = new DataSet();
//objDDL.Items.Add("Select");
objCon = new SqlConnection(ConfigurationSettings.AppSettings["strLocalCon"]);
objDA = new SqlDataAdapter(query, objCon);
objDA.Fill(ds);
objDDLval.DataSource = ds.Tables[0];
objDDLval.DataTextField = columnfield;
objDDLval.DataValueField = Dlvalue;
objDDLval.DataBind();
}
catch { }
}
/// Thsi method retrieve the string to encrypt from the Presentation Layer
/// And return the Encrypted String
///
public string encryptPassword(string strText)
{
return Encrypt(strText, "&%#@?,:*");
}
/// This method retrieve the encrypted string to decrypt from the Presentation Layer
/// And return the decrypted string
public string decryptPassword(string str)
{
return Decrypt(str, "&%#@?,:*");
}
// This method has been used to get the Encrypetd string for thepassed string
private string Encrypt(string strText, string strEncrypt)
{
byte[] byKey = new byte[20];
byte[] dv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(strEncrypt.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputArray = System.Text.Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, dv), CryptoStreamMode.Write);
cs.Write(inputArray, 0, inputArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch (Exception ex)
{
throw ex;
}
}
/// This method has been used to Decrypt the Encrypted String
private string Decrypt(string strText, string strEncrypt)
{
byte[] bKey = new byte[20];
byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
try
{
bKey = System.Text.Encoding.UTF8.GetBytes(strEncrypt.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] inputByteArray = inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(bKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch (Exception ex)
{
throw ex;
}
}
// To Import data from XML.....
public bool ExecuteTransaction(ArrayList arrSQL)
{
objCon = new SqlConnection(conString);
objCon.Open();
SqlTransaction _trans = objCon.BeginTransaction(IsolationLevel.ReadCommitted);
objCom = objCon.CreateCommand();
try
{
objCom.Transaction = _trans;
foreach (object objQry in arrSQL)
{
if (objQry != null)
{
objCom.CommandText = objQry.ToString();
objCom.ExecuteNonQuery();
}
}
_trans.Commit();
return true;
}
catch (SqlException Ex)
{
_trans.Rollback();
string errorMessage = "Error Code:" + Ex.ErrorCode + ", Message:" + Ex.Message;
return false;
}
finally
{
objCon.Close();
objCon.Dispose();
}
}
//Execute SP..
}
}
No comments :
Post a Comment