KPBDOTNET
Friday, April 24, 2015
KPBDOTNET: Working on Data Encryption / Decryption
KPBDOTNET: Working on Data Encryption / Decryption: While coding we may have to handle sensitive data sometimes, like password fields. We may required this data not to be visible to the outs...
Working on Data Encryption / Decryption
While coding we may have to handle sensitive data sometimes, like password fields. We may required this data not to be visible to the outside world. In such cases we can implement the concept of Data Encryption/ Decryption.
Following code will show how to implement the same in C#.NET.
We have to create a simple C#.NET console application which will primarily focus on three different types of data Encryption Base64, SHA1, MD5.
So create three different methods to implement each type of encryption.
Also for Base64 data decryption create another method. Data decryption in remaining two types SHA1, MD5 is not possible, it is one way process.
Complete code listing is as follows -
static void Main(string[] args)
{
Console.Write("Enter word : ");
string myWord = Console.ReadLine();
//Base64 Encoding
Console.WriteLine("=====1. Base 64 Encoding=====");
string MyEncoded=Encode_Base64(myWord);
string MyDecoded=Decode_Base64(MyEncoded);
Console.WriteLine("Encoded Word : "+MyEncoded);
Console.WriteLine("Decoded Word : "+MyDecoded);
Console.WriteLine(string.Empty);
//Hashing
Console.WriteLine("=====2. Hash SHA1 one way , Decoding not possible=====");
MyEncoded=Encode_Hash(myWord);
Console.WriteLine("Encoded Word : "+MyEncoded);
Console.WriteLine(string.Empty);
//MD5
Console.WriteLine("=====3. MD5 =====");
MyEncoded = Encode_MD5(myWord);
Console.WriteLine("Encoded Word : " + MyEncoded);
Console.WriteLine(string.Empty);
Console.ReadLine();
}
public static string Encode_Base64(string word)
{
var encoded = System.Text.Encoding.UTF8.GetBytes(word);
return System.Convert.ToBase64String(encoded);
}
public static string Decode_Base64(string Eword)
{
var decoded = System.Convert.FromBase64String(Eword);
return System.Text.Encoding.UTF8.GetString(decoded);
}
public static string Encode_Hash(string word)
{
var myHash=System.Text.Encoding.ASCII.GetBytes(word);
var mySHA=System.Security.Cryptography.SHA1.Create();
var myBytes=mySHA.ComputeHash(myHash);
var sb=new StringBuilder();
for (int i = 0; i < myBytes.Length; i++)
{
sb.Append(myBytes[i].ToString("X2"));
}
return sb.ToString();
}
public static string Encode_MD5(string word)
{
var myHASH = System.Text.Encoding.ASCII.GetBytes(word);
var myMD5 = System.Security.Cryptography.MD5.Create();
var myBytes = myMD5.ComputeHash(myHASH);
var sb = new StringBuilder();
for (int i = 0; i < myBytes.Length; i++)
{
sb.Append(myBytes[i].ToString("X2"));
}
return sb.ToString();
}
Happy Coding :)
Friday, April 17, 2015
How to make entire DIV tag clickable
While working on various aspects of designing of the website, we may have to deal with a scenario where we have to make part of page , DIV tag, entirely clickable. Lets see how we can work on this.
The best way is to enclose entire div tag in an anchor tag as follows. This way we can mention the link, if any, where page should redirect after clicking div tag.
We can have any content in div tag like image, a long description, etc.
Sometimes, instead of redirect we may have to write some code. So replace anchor tag with LinkButton control as follows
Code Snippet for OnClick event is as follows -
So working on existing controls as you require, is the best way to achieve what you need.
- Happy Coding -
The best way is to enclose entire div tag in an anchor tag as follows. This way we can mention the link, if any, where page should redirect after clicking div tag.
<a runat="server" href="http://www.kpbdotnet.blogspot.com/">
<div class="jumbotron">
this is my DIV tag and we are making this complete div tag clickable
<p>
We are contents of div tag
</p>
</div>
</a>We can have any content in div tag like image, a long description, etc.
Sometimes, instead of redirect we may have to write some code. So replace anchor tag with LinkButton control as follows
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">
<div class="jumbotron">
test div tag
<asp:Label ID="lbl" runat="server" Text="click"></asp:Label>
</div>
</asp:LinkButton>Code Snippet for OnClick event is as follows -
protected void LinkButton1_Click(object sender, EventArgs e)
{
lbl.Text = "You click div tag.";
}So working on existing controls as you require, is the best way to achieve what you need.
- Happy Coding -
Tuesday, April 14, 2015
Opening two windows on Hyperlink click
Sometimes, we may require to open two windows on one Hyperlink click.
We can achieve this easily with button control by mentioning on link on client side scripting and the other on server side coding. But when we have to use Hyperlink, the simplest way to achieve this is as follows -
<a runat="server" href="http://www.google.com" onclick="window.open('http://www.yahoo.com');" >click Me</a>
We can execute above code and after clicking on the link two different windows will open one for http://www.google.com and the other for http://www.yahoo.com
Happy Coding !
We can achieve this easily with button control by mentioning on link on client side scripting and the other on server side coding. But when we have to use Hyperlink, the simplest way to achieve this is as follows -
<a runat="server" href="http://www.google.com" onclick="window.open('http://www.yahoo.com');" >click Me</a>
We can execute above code and after clicking on the link two different windows will open one for http://www.google.com and the other for http://www.yahoo.com
Happy Coding !
Wednesday, March 11, 2015
Adding unique key constraint
In this post we will try to understand about adding unique key constraint to table in SQL SERVER 2012
Assume you have created a table Users in your database with several columns as per your requirement. And column User_Name should be made unique, i.e. User_Name value should not be repeated.
Note : I am using SQL SERVER 2012 Management Studio.
Now we need to add unique key constraint to our this newly created Users table. In order to achieve this, follow steps below.
In this way we can add Unique key constraint to our table.
Assume you have created a table Users in your database with several columns as per your requirement. And column User_Name should be made unique, i.e. User_Name value should not be repeated.
Note : I am using SQL SERVER 2012 Management Studio.
Now we need to add unique key constraint to our this newly created Users table. In order to achieve this, follow steps below.
- In Object Explorer, find your database and there your table on which you want to add unique key constraint.
- Right Click table name to select
Script Table as → Create to → New Query Editor Window.
- Let us assume the we have to add Unique constraint on column User_Name of our Users table. So write following lines of code in Query Editor Window.
ALTER TABLE [dbo].[User]
ADD CONSTRAINT UNK UNIQUE(User_Name)
- Execute to view that unique key constraint is added successfully.
- Let's verify by adding same User_Name field twice you will get following error.
In this way we can add Unique key constraint to our table.
Setting Start up page in C#.NET windows application.
Like C#.NET Console application, windows form applications have function main() as starting point of code execution.
If you carefully note the solution explorer of your C#.NET windows form application, you will see Program.cs file.
In Program.cs file, you will see following default code -
Here, from the code snippet above, Main() method has a code statement as
Application.Run(new form1());
This part will determine the starting point of the application, So setting different windows form object will start application execution from this newly set windows form rather than form1.
So following change will make the application execution start from windows form-frmLogin
Application.Run(new frmLogin());
Save changes, Build and Run the application to view the result.
If you carefully note the solution explorer of your C#.NET windows form application, you will see Program.cs file.
In Program.cs file, you will see following default code -
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new form1());
}
}Here, from the code snippet above, Main() method has a code statement as
Application.Run(new form1());
This part will determine the starting point of the application, So setting different windows form object will start application execution from this newly set windows form rather than form1.
So following change will make the application execution start from windows form-frmLogin
Application.Run(new frmLogin());
Save changes, Build and Run the application to view the result.
Friday, March 6, 2015
Speech Recognition Windows Form Based .NET application
Speech Recognition Windows Form Based .NET application
Now in this post, we will work on windows based form application.
So create a simple Windows Form application which should look like this -
Add following namespaces.
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Globalization;
And create reference of SpeechSynthesizer class as follows.
SpeechSynthesizer Syn;
First we would work on Text-To-Speech.
On Listen Button Click event write following code which will Speak the entered text in TextBox. If you carefully note the code below we have used two different method as Speak() and SpeakAsync(). First method Speak() will speak the specified text synchronously, we cannot use pause(), resume(), etc here. While the other method SpeakAsync() will speak asynchronously. This will help us to work on various functionalities like Pause(), Resume().
private void btnListen_Click(object sender, EventArgs e)
{
Syn = new SpeechSynthesizer();
if (textBox1.Text.Trim().Equals(string.Empty))
{
Syn.Speak("Enter Text First");
}
else
{
Syn.SpeakAsync(textBox1.Text);
}
}
On Pause button Click, write following code
private void btnPause_Click(object sender, EventArgs e)
{
if (Syn.State == SynthesizerState.Speaking)
{
Syn.Pause();
}
}
And On Resume button Click, write following code
private void btnResume_Click(object sender, EventArgs e)
{
if (Syn.State == SynthesizerState.Paused)
{
Syn.Resume();
}
}
And Now we can Build and Run to check the functionality of Text-To-Speech.
Let us now work on Speech-to-Text
For More details you can visit the link https://msdn.microsoft.com/en-us/librarY/hh361683(v=office.14).aspx
And here is the code
private void Form1_Load(object sender, EventArgs e)
{
startRecognition();
}
private void startRecognition()
{
SpeechRecognizer recognizer = new SpeechRecognizer();
recognizer.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
}
void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
textBox1.Text = e.Result.Text;
}
When we Built and Run the application, we may get following window for the first time application execution
Click Next and follow the instructions. Once done you will get small listening window as follows.
Speak and then it will be converted to text which will appear in textbox. You can also work on several exception handling in this code.
--Happy Coding ---
Subscribe to:
Comments (Atom)
