Friday, September 28, 2007

The biggest piece of the new update is of course the wireless iTunes store. But even cooler in my opinion is that Safari is again, working with .NET 2.0 AJAX. Ok back to work on iPhone applications I could build.. hmmm.....

Update: People have been reporting the ability to listen to .wav files in email attachments. I just tried with my Vonage voice mail account, and it's not working for me. Bummer. I was really hoping that would be something that did work.

image

 | 
Friday, September 28, 2007 7:34:28 AM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Wednesday, August 08, 2007

Sounds really simple huh? I was creating a little tray application that did some screen scraping of HTML pages.

I ended up with a large string (scraped HTML data) that I needed to display on a Form.

This is what I came up with, use the WebBrowser control, but it does not take a string as input, I ddin't really want to save the string to a file on the disk.

   this.webBrowser1.Navigate("about:blank");
   HtmlDocument doc = this.webBrowser1.Document;
   doc.Write(strHtmlString);

Works like a charm. If this is a dumb way to do this, please comment and tell me a better way.

Wednesday, August 08, 2007 9:42:48 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [1]  | 
 Wednesday, September 20, 2006

The Setup
I’ve run into an interesting problem using the DataList control. I have a datasource, an XML file, that has a series of ‘choices’ in it. The choices could be anything, products, a free item a person could choose when making a purchase, or even a color for a car. What I’m trying to do is have a datasource, bound to a repeater control that renders a series of elements as a group, while only allowing the user to choose one of them.

My data source calls them choices 1–9. There is some ancillary information that gets carried along with it, a small image, large image, text to descibe the element, and a code to identify it elsewhere. Microsoft is so good at getting us to adopt their controls in .NET, and for the most part, they’re decent.  But when you run into a limitation like I have, it’s frustrating.

The Problem
The data list control contains an item template and inside the item template resides an image, a radio button, and some text. It all renders fine, but the groupname property for the radio button does not work. I’ve even reverted back to an ‘input’ tag using HTML instead of .NET, and .NET is still mucking with the name. The problem becomes instead of being able to select one radio button, the user can select all nine!

Here is the HTML that is generated, you’ll quickly see why the radio buttons are not working:

<tr>

    <td>

        <img id="dtlListChoiceSelection__ctl0_imgSmallChoice" src="choice1Small.jpg"

        alt="Choice 1" border="0" /><br/>

        <input value="CHOICE1" name="dtlListChoiceSelection:_ctl0:CardChoice"

        id="dtlListChoiceSelection__ctl0_CardChoice" type="radio" />Choice 1

    </td>

    <td>

        <img id="dtlListChoiceSelection__ctl1_imgSmallChoice" src="choice2Small.jpg"

        alt="Choice 2" border="0" /><br/>

        <input value="CHOICE2" name="dtlListChoiceSelection:_ctl1:CardChoice"

        id="dtlListChoiceSelection__ctl1_CardChoice" type="radio" />Choice 2

    </td>

    <td>

        <img id="dtlListChoiceSelection__ctl2_imgSmallChoice" src="choice3Small.jpg"

        alt="Choice 3" border="0" /><br/>

        <input value="CHOICE3" name="dtlListChoiceSelection:_ctl2:CardChoice"

        id="dtlListChoiceSelection__ctl2_CardChoice" type="radio" />Choice 3

    </td>

</tr>

Here is a screenshot the bad radio buttons! Don't worry about the broken images, they are just there for fluff.

CropperCapture[14].Jpg

The repeater control is appending it’s container name, and incrementing it each time, so we’re not getting a single name for the radio button. I’ve tried the ASP.NET radio button as well, with the same result.

The Data
Here is all of the pieces, if you want to go away and test this on your own. I’ve tried several things, all of which I’ll explain at the end of the post. I have another way to solve the issue, but it just seems like I shouldn’t have to hassle like I have.

  • ASPX Page

    <asp:DataList RepeatDirection="Horizontal" RepeatColumns="3"
    id="dtlListChoiceSelection" runat="server">

        <ItemTemplate>

            <asp:Image id="imgSmallChoice" runat="server"
    AlternateText='<%# DataBinder.Eval(Container.DataItem, "ChoiceName")%>'
    ImageURL='<%# DataBinder.Eval(Container.DataItem, "SmallImage")%>' /><br/>

            <input type="radio" runat="server" name="CardChoice"
    value='<%# DataBinder.Eval(Container.DataItem, "ChoiceCode")%>'
    id="CardChoice"><%# DataBinder.Eval(Container.DataItem, "ChoiceName")%>

        </ItemTemplate>

    </asp:DataList>


  • ASPX CodeBehind

    public class testHarness : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.DataList dtlListChoiceSelection;
        private void Page_Load(object sender, System.EventArgs e)
        {
           
    BindChoiceSelectionList();
         }

               private void BindChoiceSelectionList()
               {
                     //find the datalist control
                    
DataList dtlListChoiceSelection = (DataList)Page.FindControl("dtlListChoiceSelection");

                     if (dtlListChoiceSelection != null)
                     {
                         //read the data from the xml file
                        
System.Data.DataSet ds = new System.Data.DataSet();
                         ds.ReadXml(Server.MapPath("ChoiceList.xml"));

                        //apply the dataset to the datalist control in the UI
                       
dtlListChoiceSelection.DataSource = ds.Tables[0].DefaultView;
                        dtlListChoiceSelection.DataBind();
                       }
                 }

  • XML Data File

    <?
    xml version="1.0" encoding="utf-8" ?>
    <ChoiceDefiniton>
      <Choice>
        <SmallImage>choice1Small.jpg</SmallImage>
       
    <LargeImage>choice1Large.jpg</LargeImage>
       
    <ChoiceName>Choice 1</ChoiceName>
       
    <ChoiceCode>CHOICE1</ChoiceCode>
      
    </Choice>
    <Choice>
        <SmallImage>choice2Small.jpg</SmallImage>
       
    <LargeImage>choice2Large.jpg</LargeImage>
       
    <ChoiceName>Choice 2</ChoiceName>
       
    <ChoiceCode>CHOICE2</ChoiceCode>
      
    </Choice>
      <Choice>
        <SmallImage>choice3Small.jpg</SmallImage>
       
    <LargeImage>choice3Large.jpg</LargeImage>
       
    <ChoiceName>Choice 3</ChoiceName>
       
    <ChoiceCode>CHOICE3</ChoiceCode>
      
    </Choice>
      <Choice>
        <SmallImage>choice4Small.jpg</SmallImage>
       
    <LargeImage>choice4Large.jpg</LargeImage>
       
    <ChoiceName>Choice 4</ChoiceName>
       
    <ChoiceCode>CHOICE4</ChoiceCode>
      
    </Choice>
      <Choice>
        <SmallImage>choice5Small.jpg</SmallImage>
       
    <LargeImage>choice5Large.jpg</LargeImage>
       
    <ChoiceName>Choice 5</ChoiceName>
       
    <ChoiceCode>CHOICE5</ChoiceCode>
      
    </Choice>
      <Choice>
        <SmallImage>choice6Small.jpg</SmallImage>
       
    <LargeImage>choice6Large.jpg</LargeImage>
       
    <ChoiceName>Choice 6</ChoiceName>
       
    <ChoiceCode>CHOICE6</ChoiceCode>
      
    </Choice>
      <Choice>
        <SmallImage>choice7Small.jpg</SmallImage>
       
    <LargeImage>choice7Large.jpg</LargeImage>
       
    <ChoiceName>Choice 7</ChoiceName>
       
    <ChoiceCode>CHOICE7</ChoiceCode>
      
    </Choice>
      <Choice>
        <SmallImage>choice8Small.jpg</SmallImage>
       
    <LargeImage>choice8Large.jpg</LargeImage>
       
    <ChoiceName>Choice 8</ChoiceName>
       
    <ChoiceCode>CHOICE8</ChoiceCode>
      
    </Choice>
      <Choice>
        <SmallImage>choice9Small.jpg</SmallImage>
       
    <LargeImage>choice9Large.jpg</LargeImage>
       
    <ChoiceName>Choice 9</ChoiceName>
       
    <ChoiceCode>CHOICE9</ChoiceCode>
      
    </Choice>


    What I’ve Tried
    I’ve tried using the asp:radio button inside my template, I’ve tried the HTML input type radio. I’ve tried to find the controls inside the page at PreRender, and rename them, that does not work either. I’ve tried finding the controls at PreRender, REMOVING them, and adding a STRING that is written inside a placeholder, and I get the same behaviour.

    Microsoft admits this to be a ‘known’ issue here. As I understand it, this is still a bug in .NET 2.0.

    There is a 3rd party control by MetaBuilders, but it’s not really an option for me, I can’t use an outside vendor’s controls for the work I’m doing. I’ve googled it, and searched all over and this seems to be the only solution.

    In Part II of this post, I’ll post how I solve this issue. So far, I’m not really satisfied with what I’ve come up with.
Wednesday, September 20, 2006 8:49:10 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [2]  | 
 Tuesday, June 27, 2006

Bad bad bad

 

Tuesday, June 27, 2006 10:19:41 AM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Tuesday, April 18, 2006

On larger projects, I've had this happen quite frequently. You open a lot of files, set break points, and sometime forget to clear them? Well apprently Visual Studio does not like this. I was debugging today and it was taking like 5 seconds to step through the code, line to line.

I had a hunch that maybe breakpoints were my issue, the file I was debugging, only had one breakpoint, but I went to the debug window, and clicked "Clear All Breakponints" the IDE instantly responded, and it was back to lightining quick again.

File under "stupid" or "obscure".

Tuesday, April 18, 2006 3:01:19 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 

Just saw this little tidbit, and I plan to download, and add it to my VS2005 instance at home. A spellchecker for Visual Studio 2005! How freaking cool is that? It will check things like alt tags, html text, captions, etc; it even provides suggestions just like Word. You need to have Office and Visual Studio 2005.

Check it out here.

CropperCapture[5].Jpg

Tuesday, April 18, 2006 1:15:11 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Thursday, April 13, 2006

I'm digging .NET 2.0! I can say for sure, that it's going to seem like forever waitng to switch over to it at work, but that's ok. So one of the things in .NET 1.1 that was decent, but could use improvement was client uploaded files via the browser.

Enter .NET 2.0 FileUpload Control. Now this may seem geeky, well, I guess if you are reading this you're either a friend, or a geek, so.... The coolest thing about the new 2.0 control is that you don't have to mess around withe MultiPart encded forms. Yep, this baby sits inside a 'normal' <form> tag. Wooooo!

And since it's now a <asp:> control type, you can run validators against it. No more making the user upload a file to determine if it's the correct type, or having to write javascript to check the input box. The really cool thing is that it just works.

Just add the following .aspx code

<asp:FileUpLoad id="FileUpLoad1" runat="server" />
<asp:Button id="UploadBtn" Text="Upload File" OnClick="UploadBtn_Click" runat="server" Width="105px" />

Like I said you can even add Regex, Required Field, etc. validators and set the ControlToValidate argument to the name of your upload control.

Originally, in the codebehind I was doing this:

protected void UploadBtn_Click(object sender, EventArgs e)
{
   if
(FileUpLoad1.HasFile)
   {
      FileUpLoad1.SaveAs(FilePath
+ FileUpLoad1.FileName);
      //Then I was doing a bunch of GDI stuff to work on the image, sized it, and created a new image and saved it.
      //
The problem came when I wanted to delete the original as uploaded above.
      //snip
      
   }
}

Even after getting a handle to the file as an image, from the disk, AND calling the dispose method on the object when I was done with it, I'd still get an exception stating that the file was in use by another process when I was trying to delete the original.

So I talked w/ a guy at work here named Stuart (see blogroll on the right) and he mentioned that it would be good if I could work with it as a stream, instead of having to write it to the disk. So after some playing, I came up with this!

protected void UploadBtn_Click(object sender, EventArgs e)
{
   if
(FileUpLoad1.HasFile)
   {
      //Instead of saving the image, I just assign my variable to the Upload Control's instance of the file as a STREAM.
      System.Drawing.Image imgPhotoResize = System.Drawing.Image.FromStream(FileUploadControl.FileContent);
      
   }
}

Nice! Now there's no file to cleanup when I'm done, all I have to do is save the file from my Image object, and call Dispose() on my instance of that object:

   imgPhotoResize.Dispose();

Thursday, April 13, 2006 8:48:08 AM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Thursday, April 06, 2006

So in diagnosing the problem below, it would have maybe been helpful to see the IIS logs, At the time I was using the built in web server (for development) that ships with VS 2005. It requires that you address it via localhost, on a port other than what IIS uses. I tried to have a firend hit it using my Dynamic DNS service, and he could not. He could hit my IIS instance on port 80.

Later, I did find you could do this: Go to your web application properties, start options, choose use custom server, and then put the path to your web application in. You'll have to go into IIS and setup a site, or virtual directory for your app.

 

Thursday, April 06, 2006 7:33:05 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 

So I'm into my second night of VS2005, and .NET 2.0. I'm working on a super secret project with a friend of mine. Hopefully we'll have something fun to show in a month or so. Anyways, I decided why not use .NET 2.0?

So I could just code it in 2.0, the way that I would in 1.1, but what would that accomplish? So I've got a really good book, and I'm teaching myself as I go.

One of the first things in .NET 2.0 that sounded really cool (actually it is pretty cool) is Master Pages. A Master Page is basically a file that you define that you can have other pages visually inherit from. What this means is the 10 pages of our site that all have the same layout inherit from one master page. If we want to move the ads, or switch the navigation, it's one file to edit.

Inside a Master Page, you can define areas that are for content. You then add Content Pages to VS 2005. So here comes the issue I ran into, and it took a while to figure out:

   Issue:
   My Master Page was defined as MasterPage.master. I added a bunch of HTML, added a content
   page called default.aspx that was linked to the Master Page. The default content page had the
   word test in it printed from the codebehind. Every time I debugged into this codebehind call,
   I noticed the Page_Load event was being called twice, and it was never a postback. I messed
   around with AutoEventWireUp in both the Master Page and the Content Page, to no avail.

   Solution:
   
I started removing HTML. When I went back to a blank page, w/ no additional HTML, the page
   events behaved correctly. I finally found this was causing the problem (my bad HTML).

      <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
      
</asp:contentplaceholder>

      <div style="BORDER-RIGHT: #000000 1px solid; BORDER-TOP: #000000 1px solid; BORDER-LEFT: #000000 1px solid; BORDER-BOTTOM: #000000 1px solid"><img alt="#" src="#"/></div>

   
Removing the div tags w/ the inline styles stopped the events from firiing twice. Sure maybe
   it's bad to have inline styles applied to a div, but man I wouldn't expect that type of behaviour.

   I'm going to tackle THEMES and SKINS next, so I'm sure I'll see why soon.

Thursday, April 06, 2006 7:20:17 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [1]  | 
 Thursday, March 02, 2006

So this is one of those errors that I occasionally run into, and have not been sure what to do. I usually do an iisreset, kill the aspnet_wp.exe process, shut down visual studio, then open visual studio again, and it goes away.

Not today. And of course at the most inconvenient time, my usual kludge to get this error to disapeer did not work. So with some further research I found a KB Article on Microsoft, explaining why this can happen.

The indexing service is known to index temporary asp.net files! I thought it was too good to be true, as a quick fix, I paused the indexing service, did a rebuild on my solution, no error. Sweet! I then followed the instructions in the KB article to tell the indexing service not to index my temporary ASP.NET files.

Also handy if you want to keep the indexing service out of certain other areas.

http://support.microsoft.com/default.aspx?scid=kb;en-us;329065

Thursday, March 02, 2006 5:24:09 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Thursday, January 19, 2006

 

When I snap a picture from my phone, with an extra click or two, I can send it to any email address I want.

I've seen camera blog sites, and thought it would be cool to do my own version. So I'm making this C# .net code available to you all, play with it, improve it, add it to your site. If you implement it, let me know I'd like to see!

Thanks to Scott Hanselman, who gave me the idea to use threads within .NET to accomplish some of the work. I was doing this before with a scheduled webservice that ran once every 15 minutes. While this worked fine, it was a pain to wait 15 minutes for a picture to be posted. It was also kind of lame because not all web hosts have the ability to schedule a web service. Using threading within .NET, when the web application starts, a thread is invoked to do the image checks to an email address you specify. The frequency that the thread starts and stops is also configurable.

Once the initial version of this was created (some time ago) I realized a few things. Initially I had set it up with essentially no security. All someone had to know was the address that I sent images to, and they could post an image to my site. I then added a check to make sure it was coming from my phone's address at least. So someone that wanted to post an image would have to know several things, a.>the address to post messages to, b.>your phone's address, and c.>that you even did this on the web site to begin with. After more consideration I added the ability to turn on one more level of security, a pin code. So if configured in the web.config your sms message must contain a pin that matches a value in the web.config.

Keep in mind that this was developed rather quickly, and there is definite room for improvement. It is currently working with Cingular phone service. I'm guessing that different providers handle messages differently, if there is enough demand, I'm willing to build processing models around the different providers. For instance, cingular's text is added to the message as an attachment, which I thought was odd.

The file included in this post is a C#, .NET 1.1 project, configured to work on Cingular (maybe others) celluar SMS service. The zip file contains the .NET Solution, respective dlls, and two sample images. I have the latest image control on the front page of my site, and it points to the camera blog page.

So here's how it goes:

  • You snap a picture w/ your camera phone.
  • You send the picture via SMS message to an email addrees that's on an POP3 compliant mail server
  • Your web application is always running and checking this address
    • The thread starts the check
    • It logs into the mail account
    • It finds a message from your cellular address
    • It finds an image attachment
    • If pin check is on, it determines if the pin in the message matches your pin setting
  • It posts the picture to your site
  • It deletes the mail message
  • The thread sleeps until time passes and it starts all over again.

On the web site:

 I have included one page of this project called default.aspx that includes:

  • A 'latest' image image control - this user control picks the latest image from directory where they're being stored.
  • The image blogger control that pages the images in a dataset, and allows users to page through them.


web.config settings are documented pretty well. contact me if you have questions.

Feature enhancements:

  • add ability to add text captions and display them w/ the picture, stripping out pin of course.
  • Ideas?

View the latest image control and the image blogger control in action.

Some basic installation instructions and files for download can be found here.

Thursday, January 19, 2006 7:51:15 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Monday, January 16, 2006

So a long time ago, I had written some code to automatically display images from folders. So on my personal site via login, family members can see pictures, and all I have to do is just upload a folder of images. Thumbnails and paging, etc. etc. is created automatically. It's nice because I can just dump pictures into a folder and forget about it. No coding, no hassle, no fuss.

The big problem I ran into was that there is no way using the FileSystemInfo object to sort by date. I think that is crazy. So I had to use the IComparer interface to do the comparison my self.

System.Collections.IComparer dateComparer = new DateComparer();

System.IO.DirectoryInfo mDir = new System.IO.DirectoryInfo(System.Configuration.ConfigurationSettings.AppSettings["KatherineImages"].ToString());
            
System.IO.FileSystemInfo[] mFiles = mDir.GetFiles("*.jpg");
Array.Sort(mFiles, dateComparer);

private sealed class DateComparer : System.Collections.IComparer
{
    public int Compare(object info1, object info2)
    {
       System.IO.FileInfo fileInfo1 = info1 as System.IO.FileInfo;
       System.IO.FileInfo fileInfo2 = info2 as System.IO.FileInfo;

       DateTime Date1 = fileInfo1 == null ? System.Convert.ToDateTime(System.Data.SqlTypes.SqlDateTime.Null) : fileInfo1.LastWriteTime;
       DateTime Date2 = fileInfo2 == null ? System.Convert.ToDateTime(System.Data.SqlTypes.SqlDateTime.Null) : fileInfo2.LastWriteTime;

       if (Date1 > Date2) return 1;
       if (Date1 < Date2) return - 1;
       return 0;
    }
}

This allows me to sort the files by date, so when uploading images from a digital camera session, people are not seeing the last images first. They are seeing the images in the date/time order they were taken.

 

Monday, January 16, 2006 5:33:29 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Thursday, October 27, 2005

This seems like a good 'dev box' sql replacement.

VistaDB 2.1 database for .NET has been released
This 2.1 update includes over 60 improvements, including new support for .NET 2.0 and Visual Studio .NET 2005. VistaDB is a small-footprint, embedded SQL database alternative to Jet/Access, MSDE and SQL Server Express 2005 that enables developers to build .NET 1.1 and .NET 2.0 applications. Features SQL-92 support, small 500KB embedded footprint, free 2-User VistaDB Server for remote TCP/IP data access, royalty free distribution for both embedded and server, Copy 'n Go! deployment, managed ADO.NET Provider, data management and data migration tools. Free trial is available for download.
- Learn more about VistaDB
- Repost this to your blog and receive a FREE copy of VistaDB 2.1!

Thursday, October 27, 2005 8:37:57 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 

Just read this on a Microsoft blog....

http://blogs.msdn.com/somasegar/archive/2005/10/27/485665.aspx

Looks like Visual Studio 2005 is shipping... :)

Thursday, October 27, 2005 12:51:59 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Thursday, October 20, 2005

So I was looking for a way to 'randomly' display a picture from a given directory. I was going to get all crazy, and do a bunch of file system operations, and then some randomization on that. After I had started, I stopped. I thought this does not need to be so complicated!

I talked to a few friends, who thought my approach seemed fine. But to be quite honest I was in a lazy mood and decided that I didn't feel like comming up with some complicated solution for something that seems so easy. Then it hit me. Isn't there some AdRotator thing in .NET? Yeah there is, and it's slick.

I simply added the following code to my .aspx page.

<asp:AdRotator id="ar1" AdvertisementFile="headers.xml" BorderWidth="0" runat=server />

Well I'm not really using Ads. I have a header graphic similar to a banner ad, but I wanted it to be random each time the page loaded. The cool thing about using this control is that there is no codebehind logic to worry about.

There is then an xml file in this case called headers.xml with the followng structure:

<?xml version="1.0" encoding="utf-8" ?>
<Advertisements>

<Ad>
<ImageUrl>images/header/header1_noglow.jpg</ImageUrl>
<AlternateText>Alt Text</AlternateText>
<Impressions>1</Impressions>
</Ad>

<Ad>
<ImageUrl>images/header/header_sky2.jpg</ImageUrl>
<AlternateText>Alt Text</AlternateText>
<Impressions>1</Impressions>
</Ad>

<Ad>
<ImageUrl>images/header/header_sky.jpg</ImageUrl>
<AlternateText>Alt Text</AlternateText>
<Impressions>1</Impressions>
</Ad>

<Ad>
<ImageUrl>images/header/header_window.jpg</ImageUrl>
<AlternateText>Alt Text</AlternateText>
<Impressions>1</Impressions>
</Ad>

</Advertisements>

Here are the arguments that you can configure in the XML file, and what they do on the front end.

  • ImageUrl - An absolute or relative url to image file
  • NavigateUrl - The location to navigate to when the image is clicked, if you leave this out, clicking the image does nothing
  • AlternateText - The 'alt' text to be displayed on the image
  • Keyword - Specifies a category for the ad that the page can filter on
  • Impressions - A number that indicated the 'weight' of the ad in the schedule of rotation relative to other ads in the file. The larger the number, the more often it will be displayed

So this is really basic stuff, but something I've always ignored in the past. So if you want a way to display a discrete set of images randomly, this might be the ticket.

Thursday, October 20, 2005 7:07:15 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 

So I thought it would be cool, to create a page for my wife's web site, that would let me know how many people are on her site at any given time. This will be useful to know when it's OK to upload new code that would bring the site down. Don't want to upset our users! Since her site is on a shared hosting environment I don't have physical access to the box. So I created an admin page on the site that prints out the number of users on the site using this code:

 

//using System.Diagnostics;

PerformanceCounter PC;
PC = new PerformanceCounter("Web Service", "Current Anonymous Users", true)
PC.InstanceName = "";
Response.Write("Current Anonymous Users: " + PC.NextValue().ToString());


The first argument in creating the new PerformanceCounter is the Category of the Performance Counter (SQL Server, Browser, Server, Memory, Processor, etc.) The next argument is the item for that category (Current connections, Active Sessions, Current FTP Connections, etc.) The two things to know when doing this are to: 1.> Know the instance of the item that you want to monitor. The instance name is the name of the site in IIS, or if you were looking at CPU utilization, the processor, etc. You can find this data by looking in Perfmon, or by asking your service provider. 2.> On your PerfomanceCounter object PC in this case, call the NextValue() method to get the current value of the counter.

This is a pretty simple example of accessing the performance counters intrinsic to the OS.

Thursday, October 20, 2005 6:38:59 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  |