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!

VS 2008 – "Visual Studio encountered an unexpected error"

Now that VS 2008 has been released to MSDN Subscribers, I went ahead like everyone else to rush to download and install it.  Not to my surprise, the install went perfectly since I didn’t install any pre-release version on my machine. I leaned my lesson last time when VS 2005 was released and all the headaches I had then. However, once installed, I was unable to use VS 2008 because it kept crashing and providing me with an error of "Visual Studio encountered an unexpected error"  when trying to interact with any part of the user interface. I couldn’t even exit from the program. I had to keep killing the process.  I’m like great, your not the only one who this error caught unexpectedly.  So, I figured a reboot would take care of it since I didn’t bother to reboot after the install. Nope, didn’t work.  I did a repair. Didn’t work. So, I did a complete uninstall, then reinstall and still didn’t work.  So, at this point I am frustrated since about 2 or 3 hrs have already passed.

I then decided to do some debugging…yes debugging…and I decided to start Visual Studio in Safe mode. You can do that by choosing:

Start, Run and enter this command     devenv /SafeMode

OR

Execute the same command from the command prompt directly.

After doing this, Visual Studio worked fine. I could use the menus, bring up dialog boxes, etc.  So, I believe that running is SafeMode doesn’t load any add-ins or custom VSIP packages, and I suspected one of these were probably causing the issue.  I was right, I use VisualSVN for accessing my Subversion source control repositories through Visual Studio. Its a great tool and even thought it $50, I find it very useful and much better than anything I could get for free.

So, I uninstall VisualSVN, version 1.2.4 (I think), and started VS 2008 and everything worked perfectly.  I then proceed to the VisualSVN web site to download the latest version, installed, and the new version and VS 2008 worked nicely.

I hope this helped somebody even if you don’t use VisualSVN, there may be some other add-in that may cause similar issues with VS 2008. Until next time…

Happy Coding!

 

 

kick it on DotNetKicks.com

.NET and the Provider Model

If you have been using the new features of the ASP.NET 2.0 web framework like Membership, Roles, SiteMap, Profiles and others, you have probably heard about the Provider Model. If not, have no fear, you have been using it all along.  The Provider Model is a set of API’s that were build into the .NET 2.0 Framework that gives both windows and web developers the ability to build applications that allow certain features/functionality to be replaced, at runtime, via the application’s configuration file.  The only requirement is that the new feature must implement all of the interfaces of the feature being replaced.  This is all done through a set of abstract classes.

Without going through much detail, Miguel Castro was a guest on DnrTV and he really goes into detail on a real word example ( Credit Card Processing ) on how you would use the Provider Model for this senario.  This was a really great show and it’s the real reason why I was encouraged to blog about this.

Happy Coding!

 

 

kick it on DotNetKicks.com

Hello Developers!

This is my first post and I thought I should use it for testing purposes. My plans on starting this blog is to use this as a means of documenting — yes, I said it — my journey as a full-time developer.  The older I get, the more I forget things that I may have done years or even months ago. So, whatever I come across and I feel it’s worth a blog post, I will certainally do that. Until next time.

Happy coding!

Tyrone