Silverlight 3 combobox selectedvalue

by Andrei Hetel 25. July 2011 05:22

I was very unpleasant surprised to find out that there is no selectedvalue property for a combobox in Silverlight 3. Most of the time you want to perform an action based on a combo box selection change. So, I come up with a simple solution which will be detailed below.

 

XAML code:

<ComboBox x:Name="MyColors">

 

We need two simple classes to bind the combobox:

 

Public Class CComboItem

     Private _ID As Int64
     Private _Description As String

     Public Sub New(ByVal pid As Int64, ByVal pDesc As String)
         _ID = pid
         _Description = pDesc
     End Sub

     Public Property ID() As Int64
         Get
             Return _ID
         End Get
         Set(ByVal value As Int64)
             _ID = value
         End Set
     End Property

     Public Property Description() As String
         Get
             Return _Description
         End Get
         Set(ByVal value As String)
             _Description = value
         End Set
     End Property

End Class

 

Second class, more exactly a collection:

 

Imports System.Collections.ObjectModel

Public Class CComboList
     Inherits ObservableCollection(Of CComboItem)

     Public Sub addItem(ByVal itm As CComboItem)
         Add(itm)
     End Sub
End Class

 

Load the combo as follows:

 

Dim colorList As New CComboList
colorList.Add(New CComboItem(1, "red"))
colorList.Add(New CComboItem(2, "yellow"))
colorList.Add(New CComboItem(3, "blue"))
MyColors.ItemsSource = colorList
MyColors.DisplayMemberPath = "Description"
MyColors.SelectedIndex = 0

 

Finally, because the post is already too long, get selected value from the combo box:

 

Private Sub MyColors_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles MyColors.SelectionChanged
     Dim id As Int64
     If MyColors.SelectedItem Is Nothing Then Exit Sub
     ' here is the selected value...
     id = DirectCast(MyColors.SelectedItem, CComboItem).ID
End Sub

 

Pretty simple, but at least is doing the trick.

 

Facebook - how to run a FQL query

by Andrei Hetel 12. December 2009 16:17

Better think twice before starting a Facebook application using AP.NET - it's PHP world there. That could change in the future because I could see some C# example in Facebook wiki, which is a big step forward I suppose. More than this, in the glorious day when I finally manage to integrate some Facebook functionality into Ysabel, Facebook Developer Toolkit released version 3.0 (all my code is based on 2.0 - and people are complaining about backward compatibility) - that really made my day!

According with the pages that I read about ASP.NET and Facebook it's best to choose an IFRAME application type and use master pages. Not 100% if it's the best strategy, but this is what I've done.

Anyway, it took me a couple of hours to figure out how to run a FQL query, and after that to do something with the result, but it's finally working. I was surprised that only couple of lines of code are needed.

A working piece of code looks like this:

 

Dim fbAPI As new facebook.API
Dim query As String
Dim xmlDR As String
Dim ds As DataSet

fbAPI.ApplicationKey = "your application key"
fbAPI.Secret = "your secret key"

query = "select uid, name from user where uid in (select uid2 from friend where uid1=" & your_Facebook ID & ")"

xmlDR = fbAPI.fql.query(query)
ds = New DataSet()
ds.ReadXml(New StringReader(xmlDR), XmlReadMode.InferSchema)

Dataset returned contains 2 datatables, in the second datatable is the data you have requested (complete list of your friends according to the above query).

Simple isn't it? Happy coding.

 

Web services - returning custom objects

by Andrei Hetel 13. November 2008 14:12

This article is focused on web services, more exactly about the way you can return a custom object into your client application (in my case a windows mobile application). The easiest way is to use a simple example. On web service project let's create a simple class with only 2 properties CastleId, CastleName:

 

<Serializable()> _
Public Class CCastle

  Private _CastleId As Int64
  Private _CastleName As String

  Public Sub New()
  End Sub
  Public Sub New(ByVal newCastleId As Int64, ByVal newCastleName As String)
    _CastleId = newCastleId
    _CastleName = newCastleName
  End Sub

  Public Property CastleId() As Int64
    Get
      Return _CastleId
    End Get
    Set(ByVal value As Int64)
      _CastleId = value
    End Set
  End Property
  Public Property CastleName() As String
    Get
      Return _CastleName.Trim
    End Get
    Set(ByVal value As String)
      _CastleName = value
    End Set
  End Property

End Class

 

Please note that the class is marked as serializable and the properties are public read/write, to be able to use it into the client application created later.

Now, let's create a collection class, also serializable:

 

<Serializable()> _
Public Class CCastleList
  Private _CastleList As List(Of CCastle)

  Public Property CastleList() As List(Of CCastle)
    Get
      Return _CastleList
    End Get
    Set(ByVal value As List(Of CCastle))
      _CastleList = value
    End Set
  End Property
End Class

 

Finally, web method:

 

<WebMethod()> _
Public Function CastleList() As CCastleList

  Dim cl As New CCastleList
  Dim myList As New List(Of CCastle)

  myList.Add(New CCastle(1, "Castle1"))
  myList.Add(New CCastle(2, "Castle2"))
  cl.CastleList = myList
  Return cl

End Function

 

That's all on the web service side. Now, let's create a client application, reference the web service and call the web method. Like this:

 

Public Sub LoadCastleList()

  Dim ws As localhost.YsaMain
  Dim cl As localhost.CCastleList
  Dim castle As localhost.CCastle

  Try
    ws = New localhost.YsaMain
    cl = ws.CastleList()

    For Each castle In cl.CastleList
      debug.writeline (castle.CastleId & " " & castle.CastleName)
    Next castle

  Catch ex As Exception
  End Try
End Sub

 

.NET Code Profiler

by Andrei Hetel 6. November 2008 04:28

Have you ever dream to see how well your .NET code is performing? Yes, I do. Today I find it here: .NET Profiler - looks pretty well and on top of this it's free. Check video demonstration available on their site and review your code. Good luck!

SQL Server 2005 CLR Integration

by Andrei Hetel 6. August 2008 10:25

There are a lot of articles on the net about how to achieve this, some of them good, some other hard to follow. My problem was pretty simple: scan a folder from new files, import and process them and at the end move them some place else.

 

There are 2 approaches:

  • First is to use FSO and OLE Automation
  • Second CLR integration

 

I decide to implement the second solution because:
  • is very hard to work with OLE automation
  • if the COM object executes in SQL Server address space, a bug in your code will crash SQL Server

 

VB.NET code
Imports System.IO
Imports System.Security.Permissions

<IODescriptionAttribute("SQLServerFileSystemWatcherDesc")> _
<PermissionSetAttribute(SecurityAction.InheritanceDemand, Name:="FullTrust")> _
<PermissionSetAttribute(SecurityAction.LinkDemand, Name:="FullTrust")> _

Public Class Import
<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
<Microsoft.SqlServer.Server.SqlProcedure()> _
Public Shared Sub Arhive(ByVal sourcePath As String, ByVal fname As String, ByVal destPath As String)
File.Move(finalSource & fname, finalDest & fname)
End Sub
End Class
Code is compiled like this from command prompt:
vbc /t:library CAMAImport.vb

 

SQL Server side

 

Steps:
1) Enable CLR integration:

 

EXEC sp_configure @configname = 'clr enabled', @configvalue = 1
RECONFIGURE WITH OVERRIDE
GO

 

2)Register the assembly (please note PERMISSION_SET = EXTERNAL_ACCESS will never work wih SAFE! - took me an hour to figure out this):

 

CREATE ASSEMBLY WDMImport FROM 'c:\SQLServer\WDMLog\Import.dll'
WITH PERMISSION_SET = EXTERNAL_ACCESS
GO

 

3) Tell database server where to find the object code:

 

CREATE PROC ImportMoveFile
(@sourcePath NVARCHAR(255), @fname NVARCHAR(255), @destPath NVARCHAR(255) )
AS EXTERNAL NAME WDMImport.Import.Arhive
GO

 

4) Run it:

 

EXEC ImportMoveFile 'c:\', 'test.txt', 'c:\Imported\'