Wednesday, September 30, 2009

How to: Set a conditional breakpoint in VS.NET

While this topic is known by many as a beginners step to debugging, I have seen cases (including myself a while back) where unnecessary extra debugging has occurred in attempt to monitor a variable for a specific value.

Below I will highlight a quick and simple way to set up a conditional breakpoint in VS.NET. This breakpoint will only be hit once the condition we set is = 'True'.

This method of setting up a conditional breakpoint can be quite useful when looping through a large list of values, and the developer wants to only debug the code once a certain value has arisen.

In the code below, I created a simple ArrayList and added some characters to it (A-G). Now let's say that I really only want to debug once the variable 'alItem = "D"'. Well I could place a breakpoint on the line that reads 'MyValue = alItem' and keep pressing 'F5' on the keyboard until the value of 'alitem = "D"', or I can set up a conditional breakpoint, so execution only stops once the value I want is set.

To begin, let's take a look at the code for this example below:


Dim al As New ArrayList
al.Add("A")
al.Add("B")
al.Add("C")
al.Add("D")
al.Add("E")
al.Add("F")
al.Add("G")

Dim MyValue As String = String.Empty
For Each alItem As String In al
MyValue = alItem
Next


1st, place a breakpoint on the line in which we want to set up a conditional breakpoint as pictured below:




2nd, open the 'Breakpoints' window by going to 'Debug -> Windows -> Breakpoints' as shown below:



3rd, right click the 'Condition' column description for the breakpoint set and select 'Condition' as shown below. This is where we dictate what condition we want to break execution:



4th, enter in the condition we want to break on debugging; in our case I entered 'alItem = "D"' and made sure the 'Is True' radio button is selected:



Lastly, run the code. You will notice that the code will only break execution when the condition we set up is 'True' as pictured below.



That's it! Hopefully this will save you a little time debugging in the future.

Saturday, September 26, 2009

How To: Make an asynchronous call from an AJAX Modal Popup

I have been asked a few times and seen some confusion occasionally around making an asyncronous postback call from an AJAX Modal Popup. The purpose of doing this is to be able to make a server side call from the Modal Popup without having it dissapear or 'Hide', which then reverts the view back to the main page.

There is really no low level magic to making this happen, but rather it just involves making sure the proper AJAX UpdatePanels, and associated AsyncPostBackTriggers are placed in the correct locations.

I think the confusion sometime lies around where the UpdatePanel is to be located. I have seen times where the developer feels it should wrap the entire control referenced by the modalpopupextender, but actually it only needs to be placed around the individual control(s) causing the postback within the modal popup.

For this example, I made a simple page with a 'Show AJAX Modal Popup' button that displays the popup. Then a button within the modal popup calls its server side click event to change the label's 'Text' property. This is all done without the modal popup ever closing.

First, take a look at the source for the .aspx page:



<%@ Register Assembly="AjaxControlToolkit, Version=3.0.30512.20315, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"
Namespace="AjaxControlToolkit" TagPrefix="AjaxControlToolkit" %>












PopupControlID="pnlTestPostback" BackgroundCssClass="modalBackground" DropShadow="true">







Second, look at the code behind:


Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub btnShowModalPopup_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnShowModalPopup.Click
If Page.IsValid Then
Me.ModalPopupExtender1.Show()
End If
End Sub
Private Sub btnTestPostback_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTestPostback.Click
Me.lblMessage.Text = "We made an asynchronous call server side from a modal popup."
End Sub
Private Sub btnCloseModalPopup_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCloseModalPopup.Click
'Reset the text back to its default message:
Me.lblMessage.Text = "Press to test postback without full postback."
Me.ModalPopupExtender1.Hide()
End Sub
End Class


As you can see the trick was the placement of the UpdatePanel and having an AsyncPostBackTrigger in the UpdatePanel containing the label, to the '
btnTestPostback' click event. You may ask why both controls (label and button) didn't just go in the same UpdatePanel; well I was trying to show an example that could be more scalable to a larger ModalPopup with a greater number of control that could not be placed in the same UpdatePanel.


If you ever have any problems getting this implemented on a larger project, I recommend removing the UpdatePanels and associated triggers, and adding them back in carefully one at a time to get the behavior you are trying to accomplish.

Tuesday, September 22, 2009

How To: Add an .asmx Web Service Reference to a WCF Project in VS.NET

Recently I was developing a WCF service in VS.NET that needed to consume and make a call to a .asmx web service. Typically consuming a web reference from Visual Studio is as straight forward as right clicking the project in Solution Explorer and selecting 'Add Web Reference...'.

In my VS.NET 2008 WCF project there is only the option to 'Add Service Reference...'. So how do I add a .asmx web service? It is still easy to do to a WCF Service in VS.NET 2008, but it is a bit more hidden in the menus, so I will show you how to do it.

1st, right click the project and select 'Add Service Reference...' as displayed below:



2nd, click the 'Advanced' button in the bottom left-hand corner as displayed below:



3rd and lastly, click the 'Add Web Reference...' button in the bottom left-hand corner as displayed below:



At this point the more familiar 'Add Web Reference' window will be displayed where you can enter your .asmx URL and proceed to consuming the web reference to your WCF project.

Wednesday, September 16, 2009

Using Reflection to Map Properties on Objects by their String Name

You might have an object that has the following properties:

MyObject.Value1
MyObject.Value2
MyObject.Value3
MyObject.Value4
MyObject.Value5
In addition, you can not refractor the code to use a list of objects or other method and are stuck with having several properties with a similar purpose on a single object. You may have coded logic in a ‘Case’ statement to determine which property to set on the object based on some other logic like the sample UI event and associated method below:

Public Sub MyUIEvent()

UpdateProperty(Me.txtValue1.ClientID, MyObject)
UpdateProperty(Me.txtValue2.ClientID, MyObject)
UpdateProperty(Me.txtValue3.ClientID, MyObject)
UpdateProperty(Me.txtValue4.ClientID, MyObject)
UpdateProperty(Me.txtValue5.ClientID, MyObject)

End Sub

Private Sub UpdateProperty(ByVal TextBoxID As String, ByVal MyObject As MyCustomObject)

Select Case TextBoxID

Case "txtValue1"
MyObject.Value1 = txtValue1.Text
Case "txtValue2"
MyObject.Value2 = txtValue2.Text
Case "txtValue3"
MyObject.Value3 = txtValue3.Text
Case "txtValue4"
MyObject.Value4 = txtValue4.Text
Case "txtValue5"
MyObject.Value5 = txtValue5.Text
End Select

End Sub

In this case you can change your ‘UpdateProperty’ method to use Reflection to map the property name on an object using its String name equivalent. You can infer the UI element to property name logic mapping inline in the call to your new method. This saves a lot of code, especially if you have more that 5 properties that are involved in the ‘Case’ statement.

Take a look to the modified method named ‘UpdatePropertyUsingReflection’ and the change to the parameter values passed in to reduce the code using Reflection:


Public Sub MyUIEventCallingReflectionMethod()

'Call the method to Update the object's property based on the name value of the property provided:
UpdatePropertyUsingReflection(Me.txtValue1.Text, MyObject, MyObject.Value1.ToString())
UpdatePropertyUsingReflection(Me.txtValue2.Text, MyObject, MyObject.Value2.ToString())
UpdatePropertyUsingReflection(Me.txtValue3.Text, MyObject, MyObject.Value3.ToString())
UpdatePropertyUsingReflection(Me.txtValue4.Text, MyObject, MyObject.Value4.ToString())
UpdatePropertyUsingReflection(Me.txtValue5.Text, MyObject, MyObject.Value5.ToString())

End Sub

Private Sub UpdatePropertyUsingReflection(ByVal TextBoxValue As String, ByVal MyObject As MyCustomObject, ByVal PropertyToUpdate As String)

'Use Reflection to get the property attributes for the Property Name Value passed into Sub():
Dim myProperty As System.Reflection.PropertyInfo = MyObject.GetType().GetProperty(PropertyToUpdate)
'Set the value on the object passed in using the setvalue method on the reflected property.
myProperty.SetValue(MyObject, TextBoxValue, Nothing)

End Sub

So there you have it; the (2) lines of code using Reflection replaced the larger ‘Case’ statement needed to determine which property to update.

Of course, the example used here was not the only use for this method and could be applied in many situations. The main goal was to show how to reflect properties using their String type name equivalent. There is some overhead associated with Reflection in .NET, but we can leave that for another day. Depending on its usage, this method can save a lot of code and be used not only with object properties, but in calling methods on objects by their String name as well.

Wednesday, September 9, 2009

Lets get things started; Initial thoughts

For this 1st blog post I wanted to speak a little to what I want to accomplish on this blog, and some of my thoughts on software development. Therefore there will be no code in this post, so IF you don't want to read this THEN proceed to the next post. ;-)

This industry more than most can humble one's opinion of how much they think they know, quickly by doing a quick search of the latest technology, architecture, design pattern, etc. and realizing you can't know it all. But this very fact is also one of the best parts of the industry as well; there is always more to know and another challenge around the corner. I guess that is why now more than ever I am so engaged in creating applications, writing code, and learning something new every day.

In this process of becoming a more seasoned developer I believe it is important to know that someone will always know more than I do (young and old), and it is OK to be wrong (hopefully not a lot, but hey it happens). This is one important way to become better in what we do; learn from our mistakes and grow from them.

This Blog will not concentrate around any one .NET or Microsoft technology, but rather bounce around topics and even escaping the bounds to other topics that help support building solid Microsoft based applications (i.e. Javascript, the software development life cycle, design patterns, etc.). The topics will also range from the introductory to the advanced in an attempt to spread knowledge to all tiers of people writing applications. I particularly enjoy helping those newer to the industry as it can be an uphill grind learning so many of the key concepts needed to build applications, before one really gets a head of steam and takes off. However, I also enjoy diving into the newest Microsoft Technologies and discussing more advanced development topics in an effort not to duplicate so many of the tutorials, blog posts, forum posts, books, and articles that have already covered the introductory subjects. When I do, I will try to explain them as I see them and how I needed to learn them in an effort to help others that find them useful.

With those thoughts laid out, I hope to contribute some nice posts here, and I encourage good discussion so please feel free to chime in or contact me with any questions. And away we go....