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 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.

 

Unique Key Constraint


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 -

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


To understand basic Speech Recognition concept, please visit my earlier post here.


Now in this post, we will work on windows based form application.
So create a simple Windows Form application which should look like this -


As mentioned in my earlier post here add reference to System.Speech Namespace.
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


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 ---




Speech Recognition Console Application .NET

 


Speech-to-Text and Text-to-Speech conversion using .NET


With System.Speech reference we can work on Speech-to-Text and Text-to-Speech conversion in .NET. So first step while working on this simple application is to add reference to this namespace as follows


Note: For recognizing voice, i.e. Speech-to-Text  add System.Speech.Recognition Namespace and for Speak functionality i.e. Text-to-Speech add System.Speech.Synthesis Namespace


Let’s first work on Speech recognition be creating a simple console application.
Here we have to add System.Speech.Recognition namespace in our console application.
After that write following code in main()-


using (SpeechRecognitionEngine rec = new SpeechRecognitionEngine(new CultureInfo("en-US")))
{
rec.LoadGrammar(new DictationGrammar());
rec.SpeechRecognized += rec_SpeechRecognized;
rec.SetInputToDefaultAudioDevice();
rec.RecognizeAsync(RecognizeMode.Multiple);
while (true)
{
    Console.ReadLine();
}
}


Event Handling
static void rec_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine(e.Result.Text);
}




Similarly create another console application for Text-to-Speech functionality and here we have to add System.Speech.Synthesis namespace. After that add following code in Main()


SpeechSynthesizer syn = new SpeechSynthesizer();
syn.Speak("this is test speaking application");
if (syn != null)
{
syn.Dispose();
}
Console.ReadLine();

We can work on several variation in these both the application to understand more in details about System.Speech reference.