Tuesday 24 September 2013

WPF - Difference between Page and Window

What is the difference between a Window and Page item in a WPF project?

Windows are independent and normal WPF application windows like Forms in WinForm Application.
A window can contain WPF Pages using Frame Container.

Pages on the other hand are intended for use in Navigation applications, where we can navigate between pages from within a Window. Examples of Pages are in Wizard kind of applications where each screen in a wizard can be a page, which are hosted within the main wizard window. Pages are hosted in NavigationWindows or Frames.

WPF - How to Read each line from a TextBox

To read each line from a TextBox control in WPF, we can use the below code snippet.
StringCollection type represents a collection of strings. The type is part of the namespace - System.Collections.Specialized. import the namespace in your code file:
using System.Collections.Specialized;
private StringCollection GetTextLines()
var lines = new StringCollection();
int lineCount = txtPlainTxt.LineCount;
for (int line = 0; line < lineCount; line++)
return lines;

The usage is straightforward as below:
Please note that each item of the StringCollection is a string holding each line of the TextBox.
// Usage
var lines = GetTextLines();
foreach (var line in lines)
// line is string with each line.

WPF - How to set Start Window

To set the Start window / page in a WPF app, we can configure it in App.xaml file.
In the "Application" element of App.xaml file in your project, set the "StartupUri" attribute to the xaml page you want to load first in the WPF Application.
In the below sample code "QuizTxt2XmlPage.xaml" is the start up page.

<Application x:Class="Quiz.WpfApp.App"

A reference screen shot is provided below:

Saturday 21 September 2013

SQL Server:Login failed for user 'sa'. (Microsoft SQL Server, Error: 18456)

Sometimes you can connect to MS SQL Server Management Studio with Windows Authentication but is unable to connect with SQL Server Authentication. You might get the below error message:

Login failed for user 'sa'. (Microsoft SQL Server, Error: 18456)
The most probable reason for this could be that when you first installed SQL Server and configured, you might have set the Server Authentication mode to Windows Authentication Mode only. Follow the below steps to solve this issue.

  1. Login to the MS SQL Server Management Studio with Windows Authentication. 
  2. In SQL Server Management Studio Object Explorer, right-click the server, and then click Properties. 
  3. In the Server Properties window, select "Security" under "Select a page" section. 
  4. Select the Server authentication as "SQL Server and Windows Authentication mode" and click Ok. 
  5. Restart the SQL Services and then try to login with 'sa' details.
Set Server Authentication mode to SQL Server and Windows

Restart MSSQL servies before trying to login again.

Thursday 5 September 2013

Windows Phone - How to use Resource Files

Resource Files in Windows phone has the similar concepts as it was in Web Forms. It can be used for localization as well as store string values to be used across your apps. Resource files are a nice way to reduce hardcoding strings inside your application code.
The default resource file added to your solution when the project is created is "AppResources.resx" which is in the "Resources" folder at the root of your project folder hierarchy. This default file is called the "neutral language resource" because it is the fallback resource file even in localization scenarios.

Note: As in web forms and win forms, each culture has its own resource file, with the culture suffixed to the filename (AppResources.en-US.resx). We will discuss more on localization scenarios in another post.

Below is the code to dynamically bind your resource to XAML page element's properties.
"{Binding Path=LocalizedResources.ApplicationHeader, Source={StaticResource LocalizedStrings}}"
For example if we want to bind a string resource to the Text property of TextBlock Control:
<TextBlock Text="{Binding Path=LocalizedResources.ApplicationHeader, Source={StaticResource LocalizedStrings}}" />

Wednesday 4 September 2013

Windows Phone - How to include ampersand (&) in the content of XAML elements like Combobox and TextBlock

/* XAML */
<ComboBox><ComboBoxItem> Awake & Alive</ComboBoxItem></ComboBox>
The above code throws exception :
Entity references or sequences beginning with an ampersand '&' must be terminated with a semicolon ';'.

The solution is to encode the special characters:
Use &amp; to encode the ampersand.
/* XAML */
<ComboBox><ComboBoxItem> Awake &amp; Alive</ComboBoxItem></ComboBox>
The same is applicable for other elements like TextBlock

/* XAML */
<ComboBox><ComboBoxItem><![CDATA[Awake & Alive]]></ComboBoxItem></ComboBox>
XAML being xml based, the xml parser when parsing an xml element, it also parses the text between XML tags, because XML elements can contain child elements.
CDATA indicated the text that should not be parsed by the XML parser. So any text that includes characters like '<' and '&' are to be included in CDATA, because these characters have special meaning in XML.
CDATA section starts with "<![CDATA[" and ends with "]]>"