Wednesday, December 15, 2010

Write log file while creating application



Write log while creating ur application. use below two class for write a log for ur appliaciton
//**************************************************************************//


Create one Application Class for Address where to store log………….

/******************/

using System;
using System.Collections.Generic;
using System.Text;

   
        class AppClass
        {
// change folder name if require by default mylog in c drive
            //public static string AppPath = @"c:\myLog\";
           // public static string AppPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            public static string AppPath = System.IO.Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);

            public AppClass()
            {
                Log4NET.Write(this.ToString() + " AppPath " + AppPath);
           
            }
        }


//**************************************************************************/


//  Create another Class for write log as per Datetime

using System;
using System.Collections.Generic;
using System.Text;


     public  class Log4NET
        {
            public static string logfile = @"\logs\LogEntery.log";
            public static string errlogfile = @"\logs\LogError.err";
            
            public static void Write(string message)
            {
                string DateTimeFile = "";
                DateTime dt = DateTime.Now;
                try{
                        if (!System.IO.Directory.Exists(AppClass.AppPath + @"\logs"))
                        {
                            System.IO.Directory.CreateDirectory(AppClass.AppPath + @"\logs");
                        }
                        DateTimeFile = dt.Year + "" + (dt.Month <= 9 ? "0" + dt.Month + "" : dt.Month + "") + "" + dt.Day + "";
                        logfile = @"\logs\DBConfig_" + DateTimeFile + ".log";
                        System.IO.FileStream fs = System.IO.File.Open(AppClass.AppPath + logfile, System.IO.FileMode.Append);
                        byte[] info = new System.Text.UTF8Encoding(true).GetBytes(System.DateTime.Now.ToString() + ":" + message + "\n");
                        fs.Write(info, 0, info.Length);
                        fs.Close();
                    }catch(Exception se){
                        if (!System.IO.Directory.Exists(AppClass.AppPath + @"\logs"))
                        {
                        System.IO.Directory.CreateDirectory(AppClass.AppPath + @"\logs");
                        }
                    System.IO.FileStream fs = System.IO.File.Open(AppClass.AppPath + @"\logs\dbLogCon.err", System.IO.FileMode.Append);
                        byte[] info = new System.Text.UTF8Encoding(true).GetBytes(System.DateTime.Now.ToString() + ":" + message + "\n");
                        fs.Write(info, 0, info.Length);
                        fs.Close();
                    }

            }
            public static void Error(string message)
            {
                string DateTimeFile = "";
                DateTime dt = DateTime.Now;
                try{
                    if (!System.IO.Directory.Exists(AppClass.AppPath + @"\logs"))
                    {
                        System.IO.Directory.CreateDirectory(AppClass.AppPath + @"\logs");
                    }
                    DateTimeFile = dt.Year + "" + (dt.Month <= 9 ? "0" + dt.Month + "" : dt.Month + "") + "" + dt.Day + "";
                    errlogfile = @"\logs\DBConfig_" + DateTimeFile + ".err";
                    System.IO.FileStream fs = System.IO.File.Open(AppClass.AppPath + errlogfile, System.IO.FileMode.Append);
                    byte[] info = new System.Text.UTF8Encoding(true).GetBytes(System.DateTime.Now.ToString() + ":" + message + "\n");
                    fs.Write(info, 0, info.Length);
                    fs.Close();
                    }catch(Exception se){
                        if (!System.IO.Directory.Exists(AppClass.AppPath + @"\logs"))
                        {
                            System.IO.Directory.CreateDirectory(AppClass.AppPath + @"\logs");
                        }
                        System.IO.FileStream fs = System.IO.File.Open(AppClass.AppPath + @"\logs\dbLogCon.err", System.IO.FileMode.Append);
                        byte[] info = new System.Text.UTF8Encoding(true).GetBytes(System.DateTime.Now.ToString() + ":" + message + "\n");
                        fs.Write(info, 0, info.Length);
                        fs.Close();                    
                    }

            }

        }


//**********************************************************************//



//***********************how to use in ur application**************************//

protected void btnLogin_Click(object sender, EventArgs e)
    {
       Try
       {
                Log4NET.Write("Login Clicked:");
 
      }
     Catch(Exception er)
       {
             Log4NET.Error("Login Failed:" +er.ToString());
        }
      
    }


//*******************//
//File Created on above path and log is written.......
logfile



Thank you bhavesh
 
ads

We are updating our database daily keep watching.
ra one - mausam- Force - Rascals- Mere Brother Ki Dulhan -Aarakshan- Bodyguard

Friday, December 3, 2010

Learn Oledb connection

Hello,
Here is a complete code detail of oledb connection.

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Drawing;
using System.Data.OleDb;

public class clsGeneral
{
DataTable dt = new DataTable();
public static string storeId = "";
public static string store = "";
#region :: Variable Declaration ::

public static string cnString;
#endregion


public clsGeneral()
{
try
{
//******* change connection path of window application***********/
string AppPath1 = System.IO.Directory.GetCurrentDirectory();
string AppPath = AppPath1.Substring(0,AppPath1.Length-10) + "\\RMC30.MDB";
cnString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + AppPath + "";
// cnString = ConfigurationSettings.AppSettings.Get("name");
}
catch (Exception)
{
}
}
/*********** Execute non qeury***** for insert delete, update*********/
public bool execute_NonQuery(string sql)
{
try
{
OleDbConnection con = new OleDbConnection();
con.ConnectionString = cnString;
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
con.Open();
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
con.Dispose();
cmd = null;
con = null;
sql = null;

}
catch (Exception er)
{
}
return true;
}
//******** select query*****************//
public object execute_Scalar(string sql)
{
string NewID = "";
try
{

OleDbConnection con = new OleDbConnection();
con.ConnectionString = cnString;
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
con.Open();
NewID = Convert.ToString(cmd.ExecuteScalar());

cmd.Dispose();
con.Close();
con.Dispose();
cmd = null;
con = null;
sql = null;

}
catch (Exception)
{

}
return NewID;
}
//********* get Record in Datatable*******/
public DataTable get_Records(string sql)
{
DataTable ds = new DataTable();
try
{
OleDbConnection con = new OleDbConnection();
con.ConnectionString = cnString;
con.Open();

OleDbDataAdapter adp = new OleDbDataAdapter(sql, con);
adp.Fill(ds);
adp.Dispose();
con.Close();
con.Dispose();
sql = "";

}
catch (Exception)
{

}
return ds;
}
//********* fill in Datagrid***********//
public bool fill_Grid(string sql, ref System.Windows.Forms.DataGrid dg)
{
try
{
OleDbConnection con = new OleDbConnection();
con.ConnectionString = cnString;
con.Open();
DataSet ds = new DataSet();
OleDbDataAdapter adp = new OleDbDataAdapter(sql, con);
adp.Fill(ds);
dg.DataSource = ds.Tables[0];

ds = null;
adp.Dispose();
con.Close();
con.Dispose();

}
catch (Exception)
{

}
return true;
}


public bool fill_Checkbox(string sql, ref System.Windows.Forms.CheckedListBox cb)
{
try
{
OleDbConnection con = new OleDbConnection();
con.ConnectionString = cnString;

con.Open();
DataSet ds = new DataSet();
OleDbDataAdapter adp = new OleDbDataAdapter(sql, con);
adp.Fill(ds);
cb.DataSource = ds.Tables[0];
ds = null;
adp.Dispose();
con.Close();
con.Dispose();

}
catch (Exception)
{

}
return true;
}

//************* fill datatable using dataAdapter********/
public bool DataAdapter(string sql, DataTable dt)
{
try
{
OleDbConnection con = new OleDbConnection();
con.ConnectionString = cnString;

con.Open();
OleDbDataAdapter adp = new OleDbDataAdapter(sql, con);
adp.Fill(dt);
adp.Dispose();
con.Close();
con.Dispose();

}
catch(Exception )
{

}
return true;
}

public bool fill_combo(string sql, ref System.Windows.Forms.ComboBox cmb, string cmbName, string cmbVal)
{
try
{
OleDbConnection con = new OleDbConnection();
con.ConnectionString = cnString;
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
con.Open();
System.Data.OleDb.OleDbDataReader dr;
// System.Data.SqlClient.SqlDataReader dr;
//dr = SqlHelper.ExecuteReader(cnString, CommandType.Text, sql);
dr = cmd.ExecuteReader();
cmb.DataSource = dr;
cmb.ValueMember = cmbVal;
cmb.DisplayMember = cmbName;
dr = null;
cmd.Dispose();
con.Close();
con.Dispose();
cmd = null;
con = null;
sql = null;

}
catch (Exception)
{

}
return true;
}
}


Thanks to Bhavesh for his editing.
Enjoy.
Submit your site
Submit your site to listtheweb.com
link