Gaurav Khanna's Blog

Tuesday, June 28, 2016

Often we have an autoincrement column in a table where next insert into the table will be one more than the previous one. In SQL Server we can have an identity property on a column which can help in generating value based on current seed & increment. But in many cases we need to have auto increment depending on another column. Say for example in below image we have books for different languages and we have sequence number based on Book Type.

Now if we delete a row from the table, the sequence number of subsequent rows will not change or get updated which will create a gap between two sequence number. This is one of the most common issues faced by developers and one of the most common question in various forums.

Solution 1

When we delete a row from the table, we can update the sequence number of subsequent rows by decrementing them by 1.

DELETEFROMBookDetail WHEREBookID=5

UPDATEBookDetail SETSequenceNo = SequenceNo - 1 WHEREBookID > 5

But decrementing by 1 will not work when we have deleted multiple row. For example

DELETEFROMBookDetail WHERESequenceNo <= 3

Solution 2

When we have update multiple rows in SQL with auto increment sequence, we can use Local Variable in SQL Server which will get incremented on each update.

DECLARE@SequenceNumber INT= 0

WHEREBookType = '.NET'

In this example, we have declared a SequenceNumber variable which will get incremented for each row, and we set that number in SequenceNo. Based on where condition in query, sequence number will get updated, and we will have sequence numbers without any gap.

Friday, August 15, 2014

Windows Phone 8.1 has a new feature called Toast Notification where we can show notifications to user and also show it in action center. We can also schedule the toast notification to appear at specific time. We can have scheduled toast notification in both Windows 8.1 Runtime app and Windows 8.1 Silverlight app.

Unlike Windows 8, Windows Phone only has one template for toast notification i.e. “toastText02”. It has application icon on left and accepts two text string with first with bold text and another with normal text. It is advisable to have very short strings to avoid truncation. We can use XML functions to set above two text.

When user clicks on toast notification, the app gets launched. We can set launch attribute of toast element containing parameters which we can use to decide navigation page when the app is launched. We can write following code to set launch parameter.

DimtoastNode = toastXml.SelectSingleNode("/toast")

CType(toastNode, XmlElement).SetAttribute("launch", "?FileID=1")

So if our launch page is MainPage.xaml then uri would be “MainPage.xaml?FileID=1”. We can read the QueryString value in MainPage.xaml and do operation as per project requirement.

ProtectedOverridesSubOnNavigatedTo(e AsNavigationEventArgs)

MyBase.OnNavigatedTo(e)

IfNavigationContext.QueryString.ContainsKey("FileID") = TrueThen

DimfileID AsString= NavigationContext.QueryString("FileID")

'Perform operation using fileID

EndIf

EndSub

Now we can create a ScheduledToastNotification where we can pass XmlDocument object containing information about toast and scheduled time when the toast notification should be displayed. We can optional also set “ID” property for ScheduledToastNotification so that we can cancel it in future if required.

Dimnotification AsNewScheduledToastNotification(toastXml, dueTime)

notification.Id = notificationID

We can also set MaximumSnoozeCount parameter to set maximum number of times notification should be displayed and SnoozeInterval parameter to set time interval between notifications. SnoozeInterval should be between 1 to 60 minutes and MaximumSnoozeCount should be between 1 to 5.

For ToastNotification to work correctly we have to set Notification Service as “WNS” in WMAppManifest.xml file. This is one of the most important steps as your app could be reject in Certification process if “WNS” is not selected in Notification Service.

Wednesday, April 2, 2014

Recently one of my Windows Phone app had a crash and it was available for download in crash count on WP Dev Center. Crash count had an excel file containing links for .CAB file which can be downloaded and we can extract the .CAB file to get the dump file containing stack traces.

We have to use WinDBG which is a free debugging tool from Microsoft. Following are the steps which need to be performed to get the exception details and stack traces.

Download Cab File and extract mdmp file

Open command prompt and set _NT_Symbol_Path and _NT_ALT_Symbol_Path environment variable as below.

Use .reload command to delete all symbol information for the specified module and reload these symbols as needed. Using “/f” parameter with .reload command will force the debugger to load the symbols immediately.

Windows Phone has a class called CollectionView which allows grouping, sorting, filtering and navigation in a data collection. To create a collection view for a collection that implements IEnumerable, we can create a CollectionViewSource
object, and add collection to the Source property. To filter the data based on TextBox text, we have to add event handler for Filter event.

In the above code, we are using "StartsWith" function to filter the Collection, and show the items which starts with text typed in TextBox. We can also use "Contains" function instead of "StartsWith" to show all the items which contains
text typed in TextBox.

Friday, July 26, 2013

Today I am going to share a code snippet to create an Editable Button in WPF, where we can update the text of a Button at runtime. In this control the user would right-click a button and then click 'Edit' to make to control Editable, and later right-click it to save/cancel the change.

First step to make the Button editable is to change the Style of Button and add a TextBox inside it, which we will make visible when the user clicks on 'Edit'.

As you can see in the above code, I have added a "txtContent" textbox which is hidden. Also to make editing look like a part of the Button, the Background and BorderBrush properties of textbox are transparent

In the above code we have an "IsEditMode" property which the user can use to know whether the control is in Edit Mode or not. Also, we have two different Context Menus, one to Edit the Control, and the another to Save/Cancel the changes. Based on the menuitems that are clicked, we are going to set the visibility of TextBox and ContentPresenter, defined in XAML.