Wednesday, November 17, 2010

Sending the Same Letter to Different People with their Details

Today a friend of mine needed some help from me on sending the same letter to different people while addressing the letters differently. This can be easily achieved by using Word Mail Merge feature.

Since I am having Word 2010 installed in my machine this post will list the steps required in Word 2010.

1. To start the process type in your letter and click on Mailings on the Word 2010 ribbon.

2. Click on Start Mail Merge and select Letters.

3. Then you need to select the people you need to send the letter to. To do that you have three options Type List, Use Existing List or Select from Outlook contacts. In this post I will use the first option.

4. When you click Type new List… you will get a window in which you can create a list of users with their details as shown in the following screenshot. For simplicity I will only create 2 users.

When you are done creating the list of users click ok so you will be prompted with a save window. Save it for later use since you may need the list for future use.

5. Then in your letter go to the appropriate places you need to place the differencing information, for example I need to put the address of the receiver after the text The Manager. To do so place the cursor on the required location and press on Address Block icon on the ribbon.

From the Insert Address Block window you can customize the way the address is printed on the letter. For example if you do not want to print the company name of the recipient then you can remove the checkmark on the Insert company name field. Preview pane will show you a preview of the actual data you have in your contact list. For the simplicity I will use the default options.

When you press ok you will see something similar to the following appearing in your letter, which will be the placeholder for the address block.

Following similar method but using the Insert Merge Field icon I did add the title and the last name of the recipient into the body section of the letter as you see below.

6. Preview your work by clicking Preview Results icon, by clicking the navigation buttons on the preview results pane you can navigate through your contacts to see the actual letters that will be created. For example below screenshot shows the letter for Mr. Withana with the changes for him.

7. When you are satisfied you can finish the merge with three options, in my case I will use the first option again.

As the final output of the merge I got one new document with the 2 letters addressed to the recipients i had in my contact list as shown in the following screenshot.

Saturday, November 13, 2010

Configuring SQL Server Instance for Azure Development Storage

If you have not configured Azure Development Storage to use your SQL Server, then while running your application targeted for Azure you will see an error similar to the following.

“Windows Azure Tools: Failed to initialize Development Storage service. Unable to start Development Storage. Failed to start Development Storage: the SQL Server instance ‘localhost\SQLExpress’ could not be found. Please configure the SQL Server instance for Development Storage using the ‘DSInit’ utility in the Windows Azure SDK.”

To overcome this you need to run the DSInit command using Windows Azure SDK Command Prompt.

Step 1 – Start the Windows Azure SDK Command Prompt.

Step 2 – Type DSInit /sqlinstance:. on the prompt and press enter.

The dot (.) at the end denotes the local SQL Server instance running on your machine. DSInit command details are as follows.

Syntax:

DSInit [/sqlinstance:<SQL server instance>] [/forceCreate] [/user:<Windows account name>]

Description:

Initialize the development store for usage by

Reserving http ports for the blob, table and queue services for the current user, and

Creating the database needed by the blob and queue services

Options:

sqlinstance : the name of the SQL Server instance on the local machine that should

be used for Development Storage. The default is SQLExpress.

Use "." for unnamed instance.

forceCreate : recreate the database even if it already exists.

user : user for whom ports are to be reserved. By default it is the current user.

.

After doing the configurations it will display a success screen similar to the following.

Then when you run your Azure ready application the Azure Development Storage and Azure Development Fabric will get started and notify you on your system tray as shown below.

After a while your application will also start without complaining if it is Azure ready.

Tuesday, November 02, 2010

Saving Table Changes in SQL Server 2008 R2

Have you noticed that with the default settings of SQL Server 2008 R2 you cannot use the design view to edit and save table changes which requires table to be recreated. But if you used earlier SQL Server installations you should remember that this functionality was possible. When you try to save such a table you will see an error similar to the following in SQL Server 2008 R2.

But the good news is that we can enable this functionality by going to SQL Server options.

Click Tools –> Options and expand Designers node and select ‘Table and Database Designers’.

Then uncheck the option ‘Prevent saving changes that require table re-creation’ and press OK.

Now if you try to save the table changes SQL will do the alteration without any complains.

Sorting SharePoint Folder Contents

Recently I needed to sort the contents of a SharePoint folder, in which I used IComparer to get my task done. Following is the code I used. Hope it is helpful.

As you see below I am using the TimeCreated property of SPFile object to do the comparison, other than this you can use any other property of the SPFile object. For a list of available properties refer MSDN.





  1. // Comparer Class.
  2. public class SPFilesComparer : IComparer
  3. {
  4.     #region IComparer Members
  5.     public int Compare(object object1, object object2)
  6.     {
  7.         return new CaseInsensitiveComparer().Compare(((SPFile)object1).TimeCreated, ((SPFile)object2).TimeCreated);
  8.     }
  9.     #endregion
  10. }








  1. ArrayList fileList = new ArrayList(attachFiles);
  2. IComparer compFiles = new SPFilesComparer();
  3. // Do the sorting based on the SPFilesComparer.
  4. fileList.Sort(compFiles);
  5. foreach (SPFile attachFile in fileList)
  6. {
  7.     // Use the sorted list.
  8. }




Friday, October 22, 2010

Piano + Guitar = Harpejji

Are you interested in musical instruments? If so have you seen an instrument which combines the Piano and the Guitar? It is manufactured by a company named Marcodi and is named as Harpejji. The sounds from it is truly fascinating, experience the sounds for you self by visiting their site. For people who would like to buy it the pricing and ordering details are also there in the site.

Click this link to see the event of presenting Harpejji first time to the great musician A. R. Rahman.

Saturday, October 02, 2010

MaxLength property not Working in TextBox

If you are involved in coding using .Net sooner or later you will notice that when the TextMode of a TextBox is changed to MultiLine the MaxLength property will stop working. What this means is in the textbox users will be able to type as many character as they want.

When searching the web for a fix for this I found several ideas to solve this. Following is the code which I modified for my requirement. Hope this helps.

ASPX





  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head runat="server">
  3. <script type="text/javascript">
  4.     function RestrictLength(textBox) {
  5.         /* Get the Max Length attribute's value. */
  6.         var allowedLength = textBox.getAttribute('MaximumLength');
  7.         /* Check for the number of characters typed in by the user. */
  8.         if (textBox.value.length > allowedLength) {
  9.             /* If it is more than the allowed limit remove the additional text. */
  10.             textBox.value = textBox.value.substring(0, allowedLength);
  11.             /*  Show a message to user with the tooltip used for the textbox. */
  12.             alert("Maximum characters allowed for '" + textBox.title +"' is " + allowedLength + ".");
  13.         }
  14.     }
  15. </script>
  16.     <title></title>
  17. </head>
  18. <body>
  19.     <form id="form1" runat="server" style="vertical-align:top;">
  20.     <!-- I am binding my java script to the onkeyup event of the textbox. So each time a key
  21.     is pressed it will fire my javascript. -->
  22.     Address <asp:TextBox ID="TextBox1" runat="server" MaxLength="10"
  23.         onkeyup="RestrictLength(this);" Height="58px" TextMode="MultiLine"></asp:TextBox>
  24.     </form>
  25.     </body>
  26. </html>




 

ASPX.CS





  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3.     // Adding the attribute to the TextBox1.
  4.     // Note that this is different to the MaxLength property.
  5.     // Also this is not case sensitive.
  6.     TextBox1.Attributes.Add("maximumlength", "10");
  7.     // Adding a tooltip which is used in the message.
  8.     TextBox1.ToolTip = "User Address";
  9. }




The output will me something similar to the following.

Saturday, September 18, 2010

Repairing a Corrupted Database

When we are regularly using databases, sometimes those will get corrupted. This happened to one of my applications, it was a Windows Mobile application with SQL Server CE database. Since it is running in a mobile it is bit difficult for us to fix the database using the desktop machine. I used the following .Net Compact framework code to fix the corrupted database while the database is in the mobile.





  1. private void btnRepair_Click(object sender, EventArgs e)
  2. {
  3.     Cursor.Current = Cursors.WaitCursor;
  4.     Cursor.Show();
  5.     // txtDBPath will contain the path to the database.
  6.     engine = new SqlCeEngine(@"Data Source=" + txtDBPath.Text + ";Password=DBPassword");
  7.     try
  8.     {
  9.         if (!engine.Verify())
  10.         {
  11.             Cursor.Current = Cursors.Default;
  12.             Cursor.Hide();
  13.             DialogResult result = MessageBox.Show("Database is corrupted. Do you want to repair?", "My App", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
  14.             if (result == DialogResult.Yes)
  15.             {
  16.                 Cursor.Current = Cursors.WaitCursor;
  17.                 Cursor.Show();
  18.                 // You can also use RepairOption.DeleteCorruptedRows.
  19.                 engine.Repair(null, RepairOption.RecoverCorruptedRows);
  20.                 Cursor.Current = Cursors.Default;
  21.                 Cursor.Hide();
  22.                 MessageBox.Show("Database repaired successfully.", "My App");
  23.             }
  24.         }
  25.         else
  26.         {
  27.             Cursor.Current = Cursors.Default;
  28.             Cursor.Hide();
  29.             MessageBox.Show("Database is not corrupted.", "My App");
  30.         }
  31.     }
  32.     catch (Exception ex)
  33.     {
  34.         Cursor.Current = Cursors.Default;
  35.         Cursor.Hide();
  36.         MessageBox.Show(ex.Message, "Error!");
  37.     }
  38. }




Next time you get a corrupted SQL CE database try it out.