Wednesday, May 02, 2007

I just recently found out about a custom Split function for SQL Server 2000/2005 while I was reading this article: Designing Reports with SQL Server Reporting Services 2005. It mentioned a custom Split function made in T-SQL that could take in a delimited string with id's.

Today I had the need of such a functionality in my current project and luckely remembered where I saw it in the first place. Since I like it I thought I would create a small example for it. What it does is retrieve, from the Northwind database, some data of the Employees table and binds it to a GridView control. If you check several checkboxes and press the button you get, for those selected emloyees, to see everything that's in the database for them.

Grab the Split function from the article or get it here:

IF EXISTS (
  SELECT * 
    FROM INFORMATION_SCHEMA.ROUTINES 
   WHERE SPECIFIC_NAME = N'Split' 
)
   DROP FUNCTION Split
GO
CREATE FUNCTION dbo.Split
(
    @ItemList NVARCHAR(4000), 
    @delimiter CHAR(1)
)
RETURNS @IDTable TABLE (Item VARCHAR(50))  
AS      

BEGIN    
    DECLARE @tempItemList NVARCHAR(4000)
    SET @tempItemList = @ItemList

    DECLARE @i INT    
    DECLARE @Item NVARCHAR(4000)

    SET @tempItemList = REPLACE (@tempItemList, ' ', '')
    SET @i = CHARINDEX(@delimiter, @tempItemList)

    WHILE (LEN(@tempItemList) > 0)
    BEGIN
        IF @i = 0
            SET @Item = @tempItemList
        ELSE
            SET @Item = LEFT(@tempItemList, @i - 1)
        INSERT INTO @IDTable(Item) VALUES(@Item)
        IF @i = 0
            SET @tempItemList = ''
        ELSE
            SET @tempItemList = RIGHT(@tempItemList, LEN(@tempItemList) - @i)
        SET @i = CHARINDEX(@delimiter, @tempItemList)
    END 
    RETURN
END  
GO

All credits go the respective author of the formentioned article for making the function.

I created the following stored procedure that makes use of the Split function:

CREATE PROCEDURE USP_RetrieveInformationForSelectedEmployees
    @p_selectedEmployees    NVARCHAR(50)
AS
BEGIN
    SELECT * 
    FROM Employees
    WHERE EmployeeID in (SELECT Item FROM split(@p_selectedEmployees, ','))
END

And this is the ASP.NET webform:

 

   1:  <%@ Page Language="C#" %>
   2:  <%@ Import Namespace="System.Data.SqlClient" %>
   3:  <%@ Import Namespace="System.Web.Configuration" %>
   4:  <%@ Import Namespace="System.Data" %>
   5:   
   6:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
   7:      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   8:   
   9:  <script runat="server">
  10:   
  11:      protected void Button1_Click(object sender, EventArgs e)
  12:      {
  13:          RetrieveInformationForSelectedEmployees();
  14:      }
  15:   
  16:      private void RetrieveInformationForSelectedEmployees()
  17:      {
  18:          StringBuilder sb = new StringBuilder();
  19:   
  20:          // First loop through the GridView and see which
  21:          // employees were selected. I use the StringBuilder
  22:          // since the list could be a very long list.
  23:          foreach (GridViewRow row in GridView1.Rows)
  24:          {
  25:              if (((CheckBox)row.FindControl("CheckBox1")).Checked)
  26:              {
  27:                  sb.Append(GridView1.DataKeys[row.RowIndex].Value.ToString() + ',');
  28:              }
  29:          }
  30:   
  31:          using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings
  32:              ["NorthwindConnectionString"].ConnectionString))
  33:          {
  34:              using (SqlCommand cmd = new SqlCommand("USP_RetrieveInformationForSelectedEmployees", conn))
  35:              {
  36:                  cmd.CommandType = CommandType.StoredProcedure;
  37:                  cmd.Parameters.AddWithValue("@p_selectedEmployees", sb.ToString());
  38:   
  39:                  DataSet ds = new DataSet();
  40:                  SqlDataAdapter da = new SqlDataAdapter(cmd);
  41:                  da.Fill(ds);
  42:   
  43:                  GridViewResult.DataSource = ds;
  44:                  GridViewResult.DataBind();
  45:              }
  46:          }
  47:      }
  48:      
  49:  </script>
  50:   
  51:  <html xmlns="http://www.w3.org/1999/xhtml" >
  52:  <head runat="server">
  53:      <title>Untitled Page</title>
  54:  </head>
  55:  <body>
  56:      <form id="form1" runat="server">
  57:      <div>
  58:          <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
  59:              DataKeyNames="EmployeeID" DataSourceID="SqlDataSource1">
  60:              <Columns>
  61:                  <asp:TemplateField>
  62:                      <ItemTemplate>
  63:                          <asp:CheckBox ID="CheckBox1" runat="server" />
  64:                      </ItemTemplate>
  65:                  </asp:TemplateField>
  66:                  <asp:BoundField DataField="LastName" HeaderText="LastName" 
  67:                      SortExpression="LastName" />
  68:                  <asp:BoundField DataField="FirstName" HeaderText="FirstName" 
  69:                      SortExpression="FirstName" />
  70:                  <asp:BoundField DataField="Title" HeaderText="Title" 
  71:                      SortExpression="Title" />
  72:              </Columns>
  73:          </asp:GridView>
  74:          <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  75:              ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
  76:              SelectCommand="SELECT [EmployeeID], [LastName], [FirstName], [Title] 
  77:              FROM [Employees] ORDER BY [LastName], [FirstName]">
  78:          </asp:SqlDataSource>
  79:          
  80:          <asp:Button ID="Button1" runat="server" Text="Retrieve data" OnClick="Button1_Click" />
  81:          <p>
  82:              <asp:GridView runat="server" ID="GridViewResult" />
  83:          </p>
  84:      </div>
  85:      </form>
  86:  </body>
  87:  </html>

In lines 23 upto 29 I loop over the rows in the GridView and see if the checkbox in the first column was checked. If it was, I append the DataKey value of it to the StringBuilder instance. On line 37 I pass the string of all the selected employee IDs. The stored procedure uses the Split function in the IN clause.

Have fun!

Grz, Kris.

kick it on DotNetKicks.com

Wednesday, May 02, 2007 6:06:40 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [4]  |  Trackback
 Tuesday, May 01, 2007
Tuesday, May 01, 2007 7:11:54 AM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [0]  |  Trackback

Well, another company is actually lying a bit. I got the same boss, the same collegues, ...

So what does change? Well for starters we've become an independent company while formerly we were a subdivision of quite a large company in Belgium.
We're still a Microsoft Gold Certified Partner and the quality we deliver hasn't changed a bit either.

Check out the site and the new logo:

Grz, Kris.

Tuesday, May 01, 2007 5:37:11 AM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Monday, April 30, 2007

If you're an ASP.NET developer like me you're going to love this one: Dynamic Datacontrols.

Take a look at the video and be amazed. I can't help to think that it resembles BLINQ a lot although still quite different because that was generated by a tool. This time it depends on the name of the folder and page that you give so that the dynamic controls are able to put together whole pages by themselves. I can't really explain it as well as I would love to but just check out the video. It's cool.

Grz, Kris.

Monday, April 30, 2007 10:10:24 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [2]  |  Trackback

The Silverlight forums, where I'll also be playing a moderation role, just went live. Go check them out! Of course there's not only the forums but also a getting started part, a learn section where already several videos are available. Whitepapers and quickstarts are also available.

Grz, Kris.

kick it on DotNetKicks.com

Monday, April 30, 2007 5:02:18 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Saturday, April 28, 2007

As a moderator on the ASP.NET I often see a request for a certain keyboard shortcut to do a specific thing. Since I like shortcuts myself and use them very often I wanted to provide a little how to here:

First you go to Tools | Customize and click the button Keyboard...

The following screen appears:

If you want to check out which shortcut corresponds to which command you can point the cursor to the textbox at 1. There you can type in the keyboard shortcut like ctrl + K, ctrl + D. In the dropdownlist at the bottom you can then see where the shortcut is used at that moment like in the following figure:

You can clearly see that the command in the Text editor, that's where you type your code, is the Edit.FormatDocument which outlines your code properly.

The other way around can also be done. If you type into 2 the command you're after, in my example Edit.FormatDocument you get to see which shortcut combination in the dropdownlist right underneath it (Shortcuts for selected command). If you're not satisfied with it you can then, in textbox 1, type in your own shortcut combination and click the Assign button.

If you want to fine grain where a certain keyboard combination is used you can select where to use it in the dropdownlist "Use new shortcut in".

Grz, Kris.

kick it on DotNetKicks.com

Saturday, April 28, 2007 11:44:52 AM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [1]  |  Trackback
 Friday, April 27, 2007

I just read this news: Windows Live Gallery goes 2.0.

Taken from the original blog post:

Now you have one-stop-shopping for all your miniapps; Sidebar Gadgets, Toolbar buttons, Web Gadgets, SideShow Gadgets, as well as appearance properties like Display Pictures, Emoticons, and Messenger Winks.

While some of the areas, like Emoticons and Winks, are not populated yet, the other areas are fully staffed including
all 807 Sidebar Gadgets.

You can visit the gallery directly at http://gallery.live.com/

Grz, Kris.

Friday, April 27, 2007 6:22:26 AM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Saturday, April 21, 2007

Last week I needed a calendar control to input dates into a textbox, in Belgian notation, so I decided to give the Calendar extender control in the ASP.NET AJAX Toolkit a go.

I put this line in my web.config: <globalization culture="nl-BE" uiCulture="nl-BE" />

I added a TextBox control and a Calendar extender but it always gave me US notation. So I went out looking for a solution. Searching on the ASP.NET forums quickly answered my question. The ScriptManagers EnableGlobalization property seems not to be set by default. After setting it to true my problem was solved immediately.

This was my little test code:

    1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="culturewithoutsettingscriptmanager.aspx.cs" Inherits="culturewithoutsettingscriptmanager" %>

    2 <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>

    3 

    4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    5 

    6 <html xmlns="http://www.w3.org/1999/xhtml" >

    7 <head runat="server">

    8     <title>Untitled Page</title>

    9 </head>

   10 <body>

   11     <form id="form1" runat="server">

   12     <div>

   13         <asp:ScriptManager ID="ScriptManager1" runat="server" EnableScriptGlobalization="true">

   14         </asp:ScriptManager>

   15         <asp:TextBox runat="server" ID="TextBoxDate" Width="116px" />

   16         <asp:Button runat="server" ID="ButtonSave" OnClick="ButtonSave_Click" Text="Save" />

   17         <br />

   18         <asp:Label ID="LabelDate" runat="server"></asp:Label>

   19         <ajaxToolkit:CalendarExtender runat="server" ID="Calendar1"

   20         FirstDayOfWeek="Monday"

   21         TargetControlID="TextBoxDate" />

   22     </div>

   23     </form>

   24 </body>

   25 </html>

And the CodeFile:

    1 using System;

    2 using System.Data;

    3 using System.Configuration;

    4 using System.Collections;

    5 using System.Web;

    6 using System.Web.Security;

    7 using System.Web.UI;

    8 using System.Web.UI.WebControls;

    9 using System.Web.UI.WebControls.WebParts;

   10 using System.Web.UI.HtmlControls;

   11 

   12 public partial class culturewithoutsettingscriptmanager : System.Web.UI.Page

   13 {

   14     protected void Page_Load(object sender, EventArgs e)

   15     {

   16 

   17     }

   18     protected void ButtonSave_Click(object sender, EventArgs e)

   19     {

   20         DateTime dt;

   21         DateTime.TryParse(TextBoxDate.Text.Trim(), out dt);

   22 

   23         LabelDate.Text = dt.ToShortDateString();

   24     }

   25 }

Testing it with my birthdate, 13/06/1975, I got the expected result in LabelDate.

Grz, Kris. 

kick it on DotNetKicks.com

Saturday, April 21, 2007 3:29:30 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Tuesday, April 17, 2007

Since I shifted to another project/client I'm constantly using my laptop so I decided to "upgrade" my user experience while developing by making it easier to type on a normal keyboard and to have my screen in a decent position instead of constantly looking down. So I decided to buy a Logitech Alto.

I like the soft keyboard and the integrated USB hub to plug my mouse in (a Logitech MX 1000 laser btw). I only had the pleasure to work with it for a single day but I already like it a lot.

Grz, Kris.

Tuesday, April 17, 2007 6:12:48 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [2]  |  Trackback
 Monday, April 16, 2007

Somasegar blogged about it. Finally the new name of WPF/e is known and it's Silverlight. I still have to get used to the new name but think it's better this way because it makes it easier to understand for less technical people that it was little to do with WPF. Microsofts hope is of course that it'll be a Flash killer but they have quite some downloads to go. But on the other hand I hope more games will be coming out soon like these: WPF/e games. Of course it'll be Silverlight games then.

You can also see a small video about it on10.

Grz, Kris.

Technorati tags: , ,
Monday, April 16, 2007 6:07:27 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [0]  |  Trackback