Monday 25 August 2014

Swap Value of Column in SQL Server/ write a single update statement to swap the value in the column/replace male to female and female to male in SQL Server.



There are multiple option to this simple problem in SQL Server.

Method 1: Using CASE Statement


UPDATE SimpleTable
SET Gender = CASE Gender WHEN 'male' THEN 'female' ELSE 'male' END
GO
SELECT *
FROM SimpleTable
GO





Method 2: Using REPLACE  Function


UPDATE SimpleTable
SET Gender = REPLACE(('fe'+Gender),'fefe','')
GO
SELECT *
FROM SimpleTable
GO





 Method 3: Using IIF (Only SQL Server 2012 or above)


UPDATE SimpleTable
SET Gender = IIF(Gender = 'male', 'female', 'male')
GO
SELECT *
FROM SimpleTable
GO









How To Disable ASP Session State in ASP.NET?



Disable Session State at the Application Level
The following steps to disable session state at the Application level:

a)       Start Microsoft Visual Studio .NET, and create a new ASP.NET Web Application.
b)       In Solution Explorer, double-click Web.config to view the contents of this file.
c)       Locate the <sessionState> section, and set the mode value to off.
d)       Save the file and/or the project to disable session state throughout all pages in the application.
Disable Session State at the Page Level
The following steps to disable session state at the Page level:

a)       Start Microsoft Visual Studio .NET, and create a new ASP.NET Web Application.
b)       In Solution Explorer, double-click the Web Form for which you want to disable session state.
c)       Click the HTML tab.
d)       At the top of the page, add EnableSessionState="false" in the @ Page directive. The modified attribute should appear similar to the following:
e)       <%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
f)        AutoEventWireup="false" Inherits="WebApplication1.WebForm1"
g)       EnableSessionState="false" %>

h)       Save the file and/or project to disable session state throughout all pages in the application.

Tuesday 19 August 2014

Get text file and its contents in c#.


string s = Path.GetPathRoot(Environment.SystemDirectory);
// Read the file as one string.
System.IO.StreamReader myFile = new System.IO.StreamReader(s + "Test.txt");
string myString = myFile.ReadToEnd();
string[] newString = myString.Split('\n');
myFile.Close();
MessageBox.Show(myString);