Archive for December, 2007

TSQL, Looping through a result set without using database cursor

A few years ago a project that I was working on required me to loop through a result set from a select statement.  Most examples that I saw used database cursors. However, I came across an old article that shows an alternative method for doing this.  Let’s say we have a table that looks like this:

Table Name : Contacts

ID FirstName LastName EmailAddress
1 Jack Bauer jbauer@fox24.com
2 Dan Marino dmarino@dolphins.com
3 George Bush gbush@whitehouse.gov
4 Michael Jordan mjordan@bulls.com
5 Bill Clinton bj@whitehouse.gov

 

Please don’t pay attention to the names. I’m sure you did so already. Anyway, let’s pretend that we had a scenario that we want to loop over the rows in this table and send an email out to these individuals.  We can do this without using a database cursor. Here is some code to ponder on:

 
   1:  declare @email varchar(20)
   2:  declare @id int
   3:  declare @rowNum int
   4:  declare @maxrows int
   5:   
   6:  select top 1 @id = ID, @email = EmailAddress from Contacts
   7:  select @maxRows = count(*) from Contacts
   8:   
   9:  set @rowNum = 0
  10:   
  11:  -- this will until the last row is reached
  12:  WHILE @rowNum < @maxRows
  13:   BEGIN
  14:      set @rowNum = @rowNum + 1
  15:      -- this is where you can now do something like sending an email
  16:      -- for now, we will just print the email to the output screen
  17:      print ('Sending email to: ' + @email)
  18:   
  19:      -- now we grab the next row making sure the ID of the next row
  20:      -- is greater than previous row
  21:      select top 1 @id = ID, @email = EmailAddress from Contacts where ID > @id
  22:   END
 

I have tried to include additional comments in this code snippet so that it doesn’t require much explanation. However, you can see the original article if you need additional details.. Until next time…

 

Happy Coding!

 

 

kick it on DotNetKicks.com

Appreciating a good, well-designed API

I have been using the .NET Framework heavily since 2002, and sometimes I have to sit back an marvel at a good, well-designed the API. I say this because just today I had to write some code to write to the file system, and it’s remarkable how simple it is to create a text file and write information to it.  You can all do it in just 2 lines of code using the System.IO.File class. Simply put, this class is a façade (or a wrapper) around the classes in the .NET Framework that work together to provide access to the file system on a Windows machine.  All the methods are static (shared for VB.NET) and it provides the most common functionality that you would normally need from the file system. Here is a little code to show you what I mean:

 
   1:  using System;
   2:  using System.IO;
   3:  using System.Text;
   4:   
   5:  namespace SimpleFileWriter {
   6:      class Program {
   7:          static void Main( string[ ] args ) {
   8:              using (StreamWriter writer = File.CreateText( @"c:\temp\test.txt" ))
   9:                  writer.WriteLine( "Hello World!" );
  10:   
  11:          }
  12:      }
  13:  }
 
 

I have posted the entire program just for your reference; but only lines 8 and 9 are needed to write data to the file system.  You can find more information on the other members of the File class here. I’m sure this may not be new to some of you; but the intent of this post was to emphasize how a well-designed API goes a long way. Until next time…

Happy Coding!

 

kick it on DotNetKicks.com

Flash CS3, UIScrollBar, scroll arrows not displaying on dynamic text

I am currently working on a Flash CS project and I recently came across an issue with using the UIScrollBar component..  The issue was quite intermittent; but after attaching the scroll bar component to dynamic the text box I was expecting to see bars and the arrows when the contents within the text box were larger than the constraints defined for the text box. After running the project both within the browser and in the SWF player, I noticed that on some occasions the scrolling was active and on some, inactive. 

After a few hours of research, I still couldn’t find a resolution to this issue. Then, I turned to the Flash CS3 documentation and after some careful reading of the UIScrollBar component documentation, I found the answer. The documentation mentioned that if you are dynamically loading text from an external source from ActionScript and then assigning that text to the dynamic text box, you must call the update() method on the scroll bar component to alert it that the contents have changed. So, if you have an instance of a dynamic text box on the stage called mytext_txt, and you attach a UIScrollBar component to the box by dragging the component on top of it.  You then must assign a instance name to the scroll bar component so that you can reference it within ActionScript.  Let’s say I name it mytext_scroll. Here is what the ActionScript 3 would look like:

 

   1:  import flash.display.*;
   2:  import flash.events.*;
   3:   
   4:  var textLoader:URLLoader = new URLLoader();
   5:  var simpleReq:URLRequest = new URLRequest("mytext.txt");
   6:   
   7:  textLoader.load(simpleReq);
   8:  textLoader.addEventListener(Event.COMPLETE, textComplete);
   9:   
  10:  function textComplete(event:Event):void {
  11:      mytext_txt.text = textLoader.data;
  12:      mytext_scroll.update();
  13:  }

 

Notice the update() on line 12. This seem to do the trick for me.  Hopefully, I was able to help someone figure this issue out. Until next time…

Happy Coding!