26 June 2012

To delete the column in sql server


To delete the column in sql server

ALTER TABLE TableName
DROP COLUMN ColumnName



The script for renaming any object (table, sp etc) in Sql Server :


The script for renaming any object (table, sp etc) in Sql Server :


sp_RENAME '[OldTableName]' , '[NewTableName]'




Script for renaming column in table in Sql Server


Script for renaming column in table in Sql Server :

sp_RENAME 'TableName.[OldColumnName]' , '[NewColumnName]', 'COLUMN'



8 June 2012

Check connection string is valid or not in C#


//Check connection string is valid or not in C#

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace SupportingToolsforCode
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string connectionstring = ConfigurationManager.AppSettings["ConnectionString"];

         ///

        /// check connection string is valid or not
        ///

        ///
        ///
        private void btnCheck_Click(object sender, EventArgs e)
        {
           
           
            if(!(String.IsNullOrEmpty(connectionstring)))
            {
                try
                {

                    //To check using SqlHelper
                    // SqlHelper objSqlHelper = new SqlHelper(connectionstring);

                    using (SqlConnection conn = new SqlConnection(connectionstring))
                    {
                        conn.Open();
                        MessageBox.Show("Connection String is valid");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(Convert.ToString(ex));
                }              
            }

        }
}



Event Viewer read, write and clear in C#

Event Viewer read, write and clear in C#




//Event Viewer read, write and clear in C#

using System;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;

namespace EventViewer
{
    public partial class EventViewer : Form
    {
        public EventViewer()
        {
            InitializeComponent();
        }

     
        ///

        /// Read the Logs from Event Viewer - Application
        ///

        public void ReadLogs()
        {
            //logType can be Application, Security, System or any other Custom Log.
            string logType = "Application";

            DataTable dtable = new DataTable("EventViewer");

            DataColumn column;
            DataRow row;
            int i=0;
            int LastLogToShow = 0;

            // Create new DataColumn, set DataType,
            // ColumnName and add to DataTable.  

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "InstanceID";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            dtable.Columns.Add(column);

            // Create new DataColumn, set DataType,
            // ColumnName and add to DataTable.  
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Source";
            column.ReadOnly = true;
         
            // Add the Column to the DataColumnCollection.
            dtable.Columns.Add(column);

            // Create new DataColumn, set DataType,
            // ColumnName and add to DataTable.  
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "EntryType";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            dtable.Columns.Add(column);

            // Create new DataColumn, set DataType,
            // ColumnName and add to DataTable.  
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.DateTime");
            column.ColumnName = "Date";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            dtable.Columns.Add(column);

         
            // Create new DataColumn, set DataType,
            // ColumnName and add to DataTable.  
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Message";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            dtable.Columns.Add(column);


            try
            {

                EventLog ev = new EventLog(logType, System.Environment.MachineName);
                LastLogToShow = ev.Entries.Count;

                if (LastLogToShow <= 0)
                {
                    dgvShow.DataSource = null;
                    MessageBox.Show("No log in the Event Viewer");
                    return;
                }

                             
                for (i = ev.Entries.Count - 1 ; i > 0; i--)
                {
                    EventLogEntry CurrentEntry = ev.Entries[i];

                    row = dtable.NewRow();

                    row["InstanceID"] = CurrentEntry.InstanceId;
                    row["EntryType"] = Convert.ToString(CurrentEntry.EntryType);
                    row["Date"] = Convert.ToDateTime(CurrentEntry.TimeGenerated);
                    row["Source"] = Convert.ToString(CurrentEntry.Source);
                    row["Message"] = Convert.ToString(CurrentEntry.Message);
                   
                    dtable.Rows.Add(row);        
         
           
                }
                ev.Close();


                if (dtable.Rows.Count > 0)
                {

                    dgvShow.DataSource = dtable;

                }
             

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        ///

        /// EventView Click Event - Used to bind the Log message in the grid
        ///

        ///
        ///
        private void btnEventViewer_Click(object sender, EventArgs e)
        {
            ReadLogs();
        }

        ///

        /// Write the log in the Eventviewer
        ///

        void WriteLog()
        {
                 
            try

            {
                //See if the source exists.
                if (!(EventLog.SourceExists("MySystemSource", System.Environment.MachineName)))
                    EventLog.CreateEventSource("MySystemSource", "System", System.Environment.MachineName);

                EventLog ev = new EventLog("System", System.Environment.MachineName, "MySystemSource");
                /* Writing to system log, in the similar way you can write to other
                 * logs that you have appropriate permissions to write to
                 */
                ev.WriteEntry("Warning is written to system Log", EventLogEntryType.Warning, 10001);
                MessageBox.Show("Warning is written to System Log");
                ev.Close();  
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void btnWrite_Click(object sender, EventArgs e)
        {
            WriteLog();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {

          DialogResult result = MessageBox.Show("Are You sure want to Delete","Important Question",MessageBoxButtons.YesNo);

          try
          {

              if (Convert.ToString(result) == "Yes")
              {


                  //Create an EventLog instance and pass log name and MachineName where the log resides.
                  EventLog ev = new EventLog("Security", System.Environment.MachineName);
                  ev.Clear();
                  ev.Close();
              }
          }
          catch (Exception ex)
          {
              throw ex;
          }
       
        }      
     
    }
}



5 June 2012

To restart IIS


To restart IIS

  • Click Start, click Run type IISReset, and then click OK.

    A Command Prompt window opens displaying the status of the IISReset command. You should read the status at the command prompt to make sure that IIS stops and restarts.



1 June 2012

Application Path in Windows application in C#


Application Path in Windows application in C#

string exeFilePath = System.Reflection.Assembly.GetEntryAssembly().Location;
//Output:D:\Arun\SupportTools\PathForWindowsApplication\PathForWindowsApplication\bin\Debug\PathForWindowsApplication.exe

string exeDirectory = System.IO.Path.GetDirectoryName(exeFilePath);
//Output:D:\Arun\SupportTools\PathForWindowsApplication\PathForWindowsApplication\bin\Debug




Consistency level in Azure cosmos db

 Consistency level in Azure cosmos db Azure Cosmos DB offers five well-defined consistency levels to provide developers with the flexibility...