Friday, November 12, 2010

DBConnector:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;


namespace ShareLibrary.DAL
{
   public class DBConnector
    {
        private string connectionString = null;
        private SqlConnection sqlConn = null;

        public DBConnector()
        {
            connectionString = ConfigurationManager.ConnectionStrings["ShareDBConnectionString"].ToString();

            sqlConn = new SqlConnection(connectionString);
        }

        ///
        /// Return sqlConnection
        ///

        public SqlConnection GetConnection
        {
            get
            {
                return sqlConn;
            }

        }
    }
}


Insert Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace ShareLibrary.DAL.Gateways
{
   public class DataInsertion
    {
        #region Member variables

        private DBConnector dbConnectorObject;
        private SqlConnection sqlConnectionObject;

        #endregion

        #region Execute query and insert data

        ///
        /// Insert data to database
        ///

        ///
Query striing
        ///
        public bool InsertData(string sqlString)
        {

            dbConnectorObject = new DBConnector();
            sqlConnectionObject = new SqlConnection();
            sqlConnectionObject = dbConnectorObject.GetConnection;

            try
            {
                sqlConnectionObject.Open();
                SqlCommand sqlCommandObject = new SqlCommand(sqlString, sqlConnectionObject);

                int i = sqlCommandObject.ExecuteNonQuery();
                if (i > 0)
                    return true;
                else
                    return false;
            }

            catch (SqlException ex)
            {
                throw new Exception(" Database problem.\n" + ex.Message);
            }

            finally
            {
                if (sqlConnectionObject.State == ConnectionState.Open)
                {
                    sqlConnectionObject.Close();
                }
            }
        }


        #endregion

        #region insert method with parameter
        ///
        /// this method insert data into database
        ///

        /// Query string
        /// Sql parameter
        ///
        public bool InsertData(string sqlString, List parametersList)
        {
            dbConnectorObject = new DBConnector();
            sqlConnectionObject = new SqlConnection();
            sqlConnectionObject = dbConnectorObject.GetConnection;

            try
            {
                sqlConnectionObject.Open();
                SqlCommand sqlCommandObject = new SqlCommand(sqlString, sqlConnectionObject);

                foreach (SqlParameter pr in parametersList)
                {
                    sqlCommandObject.Parameters.Add(pr);
                }

                int i = sqlCommandObject.ExecuteNonQuery();
                if (i > 0)
                    return true;
                else
                    return false;
            }

            catch (SqlException ex)
            {
                throw new Exception(" Database problem.\n" + ex.Message);
            }
            finally
            {
                if (sqlConnectionObject.State == System.Data.ConnectionState.Open)
                {
                    sqlConnectionObject.Close();
                }
            }
        }


        #endregion
    }
}

Selection:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace ShareLibrary.DAL.Gateways
{
   public class DataSelection
    {
        #region Member variables

        private DBConnector dbConnectorObject;
        private DataTable dataTableObject;
        private SqlConnection sqlConnectionObject;
        private SqlDataAdapter sqlDataAdepterObject;
        private DataSet dataSetObject;
      
        #endregion

       ///
       /// Return selected data
       ///

       ///
selection sql query
       ///
       public DataTable GetQueryResult(string selectingString)
       {
           dbConnectorObject = new DBConnector();
           dataTableObject = new DataTable();
           sqlConnectionObject = new SqlConnection();
           dataSetObject = new DataSet();
           try
           {
               sqlConnectionObject = dbConnectorObject.GetConnection;
               sqlDataAdepterObject = new SqlDataAdapter(selectingString, sqlConnectionObject);

               sqlDataAdepterObject.Fill(dataSetObject);

               dataTableObject = dataSetObject.Tables[0];

               if (dataSetObject.Tables[0].Rows.Count == 0)
               {
                   return null;
               }

               else
               {
                   return dataTableObject;
               }
           }

           catch (Exception ex)
           {
               throw new Exception("DataBase Problem." + ex.Message);
           }

           finally
           {
               if (sqlConnectionObject.State == ConnectionState.Open)
               {
                   sqlConnectionObject.Close();
               }
           }

       }
    }
}




///
        /// serialize the object to xml string and return it
        ///

        /// type of object to serialize
        ///
the object to serialize
        ///
        public static String ObjectToXml(ref T xmlObject)
        {
            using (StringWriter s = new StringWriter())
            {
                XmlSerializer xs = new XmlSerializer(typeof(T));

                xs.Serialize(s, xmlObject);
                return s.ToString();
            }
        }

        ///
        /// method to deserialize the xml data to the object type
        ///

        ///
        ///
        ///
        public static T XmlToObject(String xml)
        {
            string fixedString = xml;
            if (typeof(T) == typeof(DataTable))
            {
                string rp = @"(?\d{4}-\d{2}-\d{2})(?

Saturday, April 24, 2010

Screen scraping in C# using HtmlAgilityPack.

In my project, i have used  HtmlAgilityPack library to capture the page and parse it.

1. First you create a asp.net project and add HtmlAgilityPack reference in your project.

2. Create a 'HtmlWeb' object

3. Load the html page using 'HtmlDocument'

4. now you get html page in 'HtmlDocument' object.

5. You can display this 'HtmlDocument' object in asp 'Literal' control.

the code this below


HtmlWeb hwObject = new HtmlWeb();
            HtmlDocument htmldocObject = hwObject.Load("http://www.c-sharpcorner.com");
          
           
lLatestPrice.Text = htmldocObject.DocumentNode.InnerHtml;


You can find all link from this page using a loop like this

            foreach (HtmlNode link in htmldocObject.DocumentNode.SelectNodes("//a[@href]"))
            {
                string s = link.InnerText;
               
            }

thats it.....

Saturday, March 13, 2010

Desktop icon disable

1. http://binaryworld.net/Main/CodeDetail.aspx?CodeId=3730

http://forums.techarena.in/software-development/1099472.htm


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace ShowHideDesktop
{
    public partial class Form1 : Form
    {
       
        //[DllImport("user32.dll", EntryPoint = "FindWindowA")]
        //private static extern long FindWindow(string lpClassName, string lpWindowName);

        //[DllImport("user32.dll")]
        //private static extern long GetWindow(long hwnd, long wCmd);

        //[DllImport("user32.dll")]
        //private static extern long ShowWindow(long hwnd, long nCmdShow);

        ////[DllImport("user32.dll")]
        ////private static extern long EnableWindow(long hwnd, long fEnable);

        //[DllImport("user32", EntryPoint = "FindWindowExA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]

        //public static extern long FindWindowEx(long hWnd1, long hWnd2, string lpsz1, string lpsz2);

        //[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]

        //public static extern long SetWindowPos(long hWnd, long hWndInsertAfter, long X, long Y, long cx, long cy, long wFlags);

        //[DllImport("User32.dll")]
        //public static extern int MessageBox(int h, string m, string c, int type);

        [DllImport("User32.dll")]
        public static extern int FindWindow(string lpClassName, string lpWindowName);

        [DllImport("User32.dll")]
        public static extern Int32 SendMessage(int hWnd, int Msg,  int wParam, int lParam);

        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
       
        void ShowDesktopIcons(bool show)
        {
            IntPtr hWnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Progman", null);
            if (show)
                ShowWindow(hWnd, 5);
            else
                ShowWindow(hWnd, 0);
        }



        public Form1()
        {
            InitializeComponent();
        }

        private void showIconcheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (showIconcheckBox.Checked)
            {
                //  ShowHideDesktopIcons(true);
                ShowDesktopIcons(true);

            }
            else
                ShowDesktopIcons(false);

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Process p = new Process();

            p.StartInfo.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.System);

            p.StartInfo.FileName = "taskmgr.exe";

            p.StartInfo.CreateNoWindow = true;

            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

            p.Start();

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {

            const int WM_CLOSE = 0x0010;

            int taskManager = FindWindow("#32770", "Windows Task Manager");

            SendMessage(taskManager, WM_CLOSE, 0, 0);

        }


        //void ShowDesktopIcons(bool bVisible)
        //{
        //    ShowWindow(GetWindow(FindWindow("Progman", "Program Manager"), GW_CHILD),
        //    (bVisible ? SW_SHOW : SW_HIDE));
        //}

        /////
        ///// method for handling the showing/hiding of the desktop icons
        /////

        /////
condition: show or hide
        ///// true/false
        //public bool ShowHideDesktopIcons(bool show)
        //{
        //    try
        //    {
        //        //get a handle to the desktop ("Progman")
        //        long winHandle = FindWindowEx(0, 0, "Progman", null);

        //        //determine if we're showing or hiding
        //        switch (show)
        //        {
        //            case true:
        //                ShowWindow(winHandle, 0);
        //                break;
        //            case false:
        //                ShowWindow(winHandle, 5);
        //                break;
        //        }
        //        return true;
        //    }
        //    catch (Win32Exception ex)
        //    {
        //      //  MessageBox.Show(ex.ToString());
        //        return false;
        //    }

        //}

    }
}

Disable network...
1. http://www.eggheadcafe.com/community/aspnet/2/10068229/how-to-disconnect-the-int.aspx
2. http://lamahashim.blogspot.com/2010/03/disabling-network-using-c.html
3. http://www.codeguru.com/forum/showthread.php?t=328228&highlight=internethangup
4. http://www.debugging.com/bug/4715