Dotnet, DotnetCore, Azure, C#,VB.net, Sql Server, WCF, MVC ,Linq, Javascript and Jquery
26 June 2012
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;
}
}
}
}
//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
Path Details in Asp.net - Absolute Path and Relative Path in asp.net
Path Details in Asp.net - Absolute Path and Relative Path in asp.net
string applicationPath = string.Empty;
string executionFilePath=string.Empty;
string filePath = string.Empty;
string path = string.Empty;
string physicalApplicationPath = string.Empty;
string physicalPath = string.Empty;
string rawUrl = string.Empty;
String rootPath = string.Empty;
string mapPath = string.Empty;
//Relative path
//Only a Portion of full path
//Absolute path
//Contains Root directory and all Sub directory
// Gets the ASP.NET application's virtual application root path on the server.
applicationPath = Request.ApplicationPath + "/images/Nature.gif";
//Output : /ApplicationPathInWeb/images/Nature.gif
//Gets the virtual path of the current request.
executionFilePath = Request.CurrentExecutionFilePath;
//Output : /ApplicationPathInWeb/Default.aspx
//Gets the virtual path of the current request.for the URL http://www.contoso.com/virdir/page.html/tail, //the FilePath value is /virdir/page.html.
filePath = Request.FilePath;
//Output : /ApplicationPathInWeb/Default.aspx
//Gets the virtual path of the current request.
path = Request.Path;
//Output : /ApplicationPathInWeb/Default.aspx
physicalApplicationPath = Request.PhysicalApplicationPath;
//Output:D:\Arun\SupportTools\ApplicationPathInWeb\
physicalPath = Request.PhysicalPath;
//Output:D:\Arun\SupportTools\ApplicationPathInWeb\Default.aspx
rawUrl = Request.RawUrl;
//Output:/ApplicationPathInWeb/Default.aspx
rootPath = Server.MapPath("~");
//Output:D:\Arun\SupportTools\ApplicationPathInWeb
mapPath = Request.MapPath("~\\nature.jpg");
//Output:D:\Arun\SupportTools\ApplicationPathInWeb\nature.jpg
Select statement and Case Statement in Sql Server
Select statement and Case Statement in Sql Server
SELECT ProductNumber, Name, "Price Range" =
CASE
WHEN ListPrice = 0 THEN 'Mfg item - not for resale'
WHEN ListPrice < 50 THEN 'Under $50'
WHEN ListPrice >= 50 and ListPrice < 250 THEN 'Under $250'
WHEN ListPrice >= 250 and ListPrice < 1000 THEN 'Under $1000'
ELSE 'Over $1000'
END
FROM Production.Product
ORDER BY ProductNumber ;
Subscribe to:
Posts (Atom)
Implementing OAuth validation in a Web API
I mplementing OAuth validation in a Web API Implementing OAuth validation in a Web API using C# typically involves several key steps to sec...
-
ViewBag, ViewData, TempData and View State in MVC ASP.NET MVC offers us three options ViewData, ViewBag and TempData for passing data from...
-
// Export Datatable to Excel in C# Windows application using System; using System.Data; using System.IO; using System.Windows.Forms; ...