TSQL: Nested Select with multiple results to one (comma separated) string

by Pieter Brinkman 25. August 2009 03:22

For this example I will use the default asp.net Membership tables.

Let say: I need to return all users with their roles (comma separated) in one query. After a long time Googling I found the following solution.

[code:tsql]

select
  UserName,
  (select roles.RoleName + ', '
    FROM aspnet_Roles roles
    join aspnet_UsersInRoles usersInRole on roles.RoleId = usersInRole.RoleId
    WHERE usersInRole.UserId = aspUser.UserId
    for xml path('')) as roles
from
    aspnet_Users aspUser

[/code]

Don't know if this is the best way, but it works.

Categories: TSQL

TSQL: Use common table expression

by Pieter Brinkman 23. August 2009 03:04

With common table expressions you can save the results to a temporary result set and use this results set for other queries.

[code:tsql]

WITH temporaryNamedResultSet
AS
(
  select UserName from aspnet_Users
)
select * from temporaryNamedResultSet

[/code]

 

Categories: TSQL

Asp.Net: invoke WCF method with WCF Test Client

by Pieter Brinkman 20. August 2009 02:53

When deploying a Silverlight application we ran into problems a WCF web-service, to find out what the problem was I wanted to invoke the method.

Microsoft shipped an application for invoking methods from your Windows PC (WCFtestclient.exe). The following steps explain how to use WCFtestclient.exe.

First startup Visual Studio 2008 Command Prompt. In the command prompt type wcftestclient, the application will startup.

Now we need to add the Service. Click File and Add Service.

 Add Service to wcftestclient


Add the URL to your service in the pop-up and pres ok. The service will now add all methods from your service. You can add multiple service URLs.

Double click the method you want to invoke from the tree on the left pane. Enter the values that are required for the Invoke and press Invoke. The method will be invoked.

Invoke WCF method


The right bottom pane will show the response. In my case this shows the Stack-trace because of the error. Normally this will show the web-service response (XML).

With the stack-trace I could located the error and fix it.

Hope this helps,

Pieter

 

XSL: for-each with max items

by Pieter Brinkman 5. August 2009 09:21

While working with Umbraco and Sitecore I learned some Xsl tricks.

The following example shows how to show the first 10 items in a HTML list.

[code:xml]
<ul>
  <xsl:for-each select="*">
    <xsl:if test="position()&lt;='9'">
      <li>
 <a href="{@link}">
   <xsl:value-of select="@name"/>
  </a>
      </li>
    </xsl:if>
  </xsl:for-each>
</ul>
[/code]

 

Categories: XSL