Tech NovoGeek

...Technology Simplified

Wednesday, June 20, 2012

Received MCTS Certification for WCF

No comments :

MCTS(rgb)_1374

image

Tuesday, June 19, 2012

Rijndael Algorithm for Encryption and Decryption

No comments :

I have implemented a sample code to implement encryption and decryption of string using Rijndael Algorithm

 protected void Page_Load(object sender, EventArgs e)
{
string clearData = "Hello, World!";
string key = "s@1tValue";
byte[] b = Encrypt(clearData, key);
Response.Write("Encrypted String: " + Convert.ToBase64String(b));
Response.Write("--------------------------------------------------------------");
Response.Write("Decrypted String: " + Encoding.UTF8.GetString(Decrypt(b, key)));
}

public static byte[] Encrypt(string iclearData, string key)
{
try
{
byte[] clearData = Encoding.ASCII.GetBytes(iclearData);
var salt = new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 };
var pdb = new PasswordDeriveBytes(key, salt);
MemoryStream ms = new MemoryStream();
var alg = Rijndael.Create();
alg.Key = pdb.GetBytes(32);
alg.IV = pdb.GetBytes(16);
CryptoStream cryptoStream = new CryptoStream(ms,alg.CreateEncryptor(),CryptoStreamMode.Write);
cryptoStream.Write(clearData, 0, clearData.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherTextBytes = ms.ToArray();
ms.Close();
cryptoStream.Close();
return cipherTextBytes;
}
catch (Exception)
{
throw new Exception("Encryption Failed");
}
}

public static byte[] Decrypt(byte[] cipherData, string key)
{
try
{
var salt = new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 };
var pdb = new PasswordDeriveBytes(key, salt);
var alg = Rijndael.Create();
alg.Key = pdb.GetBytes(32);
alg.IV = pdb.GetBytes(16);
MemoryStream ms = new MemoryStream(cipherData);
CryptoStream cryptoStream = new CryptoStream(ms,alg.CreateDecryptor(),CryptoStreamMode.Read);
byte[] plainTextBytes = new byte[cipherData.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
ms.Close();
cryptoStream.Close();
return plainTextBytes;
}
catch (Exception)
{
throw new Exception("Decryption Failed");
}
}

Monday, June 18, 2012

MCTS Certification

No comments :

Today i took my first Certification and it is on Exam 70-513: TS: Windows Communication Foundation Development with Microsoft .NET Framework 4
I got certified with score 861/1000

Friday, June 8, 2012

How to display rss feed in asp.net

No comments :
In this post I will show you how to display rss feed in asp.net using XmlDataSource and DataList control.
Create a web page name it as RSSFeed.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtUrl" runat="server" Width="349px"></asp:TextBox>
<asp:Button ID="btnLoad" runat="server" Text="Load" OnClick="btnLoad_Click" />
<asp:DataList ID="dlRss" runat="server">
<ItemTemplate>
<%#XPath("title") %>
<i>
<%#XPath("description") %></i> <i>
<%#XPath("pubdate") %></i> <b><a href="<%#XPath("link") %>">
<%#XPath("link") %></a></b>
</ItemTemplate>
</asp:DataList>
<asp:XmlDataSource ID="xml" runat="server" XPath="rss/channel/item"></asp:XmlDataSource>
</div>
</form>
</body>
</html>
protected void btnLoad_Click(object sender, EventArgs e)
{
if (txtUrl.Text != "")
{
xml.DataFile = txtUrl.Text;
dlRss.DataSourceID = xml.ClientID;
}
}

Wednesday, June 6, 2012

“click here to reload the designer” message when creating new SL application in Visual Studio 2010

No comments :

Generally Visual Studio 2010 doesn't come with Silverlight 4 . They have Silverlight 3 capability only.
If you want to develop Silverlight 4 app in any of the above two IDEs, you need to install additional add-on that can be installed from http://www.microsoft.com/downloads/en/details.aspx?FamilyID=b3deb194-ca86-4fb6-a716-b67c2604a139 (34 MB).
Once you have installed, you should start developing Silverlight 4 app.

But when you get the error message “click here to reload the designer” message with the Details pane referring to a null reference exception "object reference not set to an instance of an object"
Just uninstall all the Silver Light named programs from Control Panel.
And follow the process mentioned above .
Don't try to install SilverlightDeveloper.exe before installing SilverlightTools.exe because results in the above error.

Hope this helps!

Friday, June 1, 2012

Dynamic SQL Query

No comments :
SQL Server different ways of running a dynamic SQL Query statement:
1.Query with parameters
2.EXEC
3.Sp_Executesql

1. Query with parameters
It is as simple as the following example shows.
DECLARE @emp varchar(75)
SET @emp = 'Anu'
SELECT * FROM employee WHERE EmpName = @emp
2. EXEC

SQL statement is built on the fly
DECLARE @sqlCommand varchar(1000)
DECLARE @columnList varchar(75)
DECLARE @ emp varchar(75)
SET @columnList = 'EmpId, Salary, EmpName'
SET @emp = '''Anu'''
SET @sqlCommand = 'SELECT ' + @columnList + ' FROM employee WHERE EmpName = ' + @emp
EXEC (@sqlCommand)

3. sp_executesql
DECLARE @sqlCommand nvarchar(1000)
DECLARE @columnList varchar(75)
DECLARE @emp varchar(75)
SET @columnList = 'EmpId, Salary, EmpName'
SET @emp = '''Anu'''
SET @sqlCommand = 'SELECT ' + @columnList + ' FROM employee WHERE EmpName = ' + @emp
EXECUTE sp_executesql @sqlCommand, N'@emp nvarchar(75)', @emp = @emp

So here are three different ways of writing dynamic queries

SQL Server With Syntax in Dynamic Query

No comments :

Declare @sql nvarchar(max)
DEclare @tab nvarchar(50)
set @tab= 'Employeetable'
-- To get column list of table in database
select 'select * from '+@tab+' where ' + name + '=True' as query1 into Temptable
from sys.columns where object_id in(select object_id from sys.tables where name=''+@tab+'')

declare @colcount int
declare @counter int
DEclare @cmd nvarchar(max)
set @counter=0
select @colcount =COUNT(*) from Temptable
WHILE @counter < @colcount
BEGIN
SET @counter = @counter + 1
SET @cmd =';WITH MyCte AS ( select query1, RowNum = row_number() OVER ( order by query1 )
from Temptable ) SELECT query1 FROM MyCte WHERE RowNum = '+CONVERT(nvarchar,@counter)
EXECUTE sp_executesql @cmd
END

-- The above example is example for executing Dynamic Query Resultset
-- ;WITH MyCte AS ( select query1, RowNum = row_number() OVER ( order by query1 ) from Temptable )
--SELECT query1 FROM MyCte WHERE RowNum = 2

DECLARE @sqlCommand nvarchar(1000)
DECLARE @columnList varchar(75)
DECLARE @city varchar(75)
SET @columnList = 'EmpId, Salary, EmpName'
SET @city = 'Anu'
SET @sqlCommand = 'SELECT ' + @columnList + ',ROW_NUMBER() OVER( order by EmpId) as rownum
FROM Employee WHERE EmpName = @city'
EXECUTE sp_executesql @sqlCommand, N'@city nvarchar(75)', @city = @city
set @sqlCommand = 'SELECT *,ROW_NUMBER() OVER( order by query1) as rownum from Temptable'
EXECUTE sp_executesql @sqlCommand