Mr Pike's Blog

 Diary of an <i>ASP.NET</i> bodger

  • Stuck

    I've been going through Jason Bere's excellent book - Teach Yourself Visual Studio.NET 2003 in 21 Days as a way of re-acquainting myself with both developing and the .NET framework etc. I've been faithfully doing a day at a time, but now... I'm stuck, and apparently its because I'm a smart-arse with SQL2005 instead of SQLExpress.


  • Day 5: Writing ASP.NET applications

    Friday 27th May 2005 – Chapter 5 – Writing ASP.NET applications


    Once you’ve got it into your head that writing ASP.NET is

    a)       No different than writing windows forms (With a few exceptions)

    b)      Totally different than ASP

    c)       Tons easier than classic ASP once you get over the learning hump


    You’ll find yourself whizzing along. Jason leads you through writing very quick form page and then points out the files that are created for you, in particular the web.config, Global.asax, Styles.css, AssemblyInfo and vdisco. So what’s changed in VS.NET 2005?

    First thing I noticed is that it asks you if you would like debugging turned on in the web.config. I also noticed that my simple page, a textbox, button and label, didn’t bloody work. Part of my problem is that I have SQL2005 installed, as opposed to SQL Express but I can’t seem to find where or how I can add in my local server instance as opposed to the expected SQL Express instance…


    So I’m no longer whizzing along, and seeing as I'm off to France in the morning, I have no time to figure out what or how I can get round this. Clean install of XP Pro and VS.NET/SQL2005 so there shouldn't be any lingering issues with old beta's etc. Sob.

    Anyway. Off to France (YAY!!)




    Ok -  resumed. The thing I was missing was... ASPNET_REGSQL.EXE which lives in your Microsoft.NET\Framework\v2.0.50215\ directory (Or whatever version).


    So now I'm whipping along again. Awesome...



  • Day 4: Deploying

    Thursday 26th May 2005 – Chapter 4 – Deplying windows form apps


    In the Police, deployment means sending a Police Officer to a job. In computers, it used to mean a lot of messing about, dll hell, versioning... etc. Let's see what's changed.


    Ok, I’m actually a day behind (already!) but I got other stuff going on, so lets not lol!

    This chapter allows me to play catchup though cos really, what’s there to know in VS.NET 2005 about deploying?? You go to Solution Explorer, right click, Publish… Done.


    It took all my default settings, installed it to Pike Productions folder, created the start men, made a web-page with the install button on, created the MSI file, everything. I’m obviously an unknown publisher but even so, how easy was that! I can see now why I got out of programming. My grandma could do this.


    Ok, ok, there’s more to it than that, especially if we want a custom installation, but when I need that knowledge I’ll get it. Time for a pint instead…


    Day 5 - writing ASP.NET applications


  • Day 3: Windows Forms revisited

    Wednesday 25th May 2005 – Chapter 3 – Writing Windows Forms applications


    Cool – let’s get into it. Basically, make a windows form that does Hello World! On a button click event. Reckon we can skip most of this – it’s too damn easy in VS.NET! Let’s time it – GO!

    36 seconds on a 1.8ghz machine lol.

    Public Class Form1


        Private Sub Form1_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Me.TextBox1.Text = "Clickety click"

        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Me.TextBox1.Text = "Hello WOrld!!"

        End Sub

    End Class


    I notice though that a lot of the old Form Designer generated code is missing. I do recall digging into that at some point, so I’m sure there’ll be a way to hack into it these days. Perhaps its to protect developer from themselves.


    Messing about with the properties is about the same, though adding a splash-screen is incredibly easy. First one I did was on screen for about a micro-second, so more of a flash-screen really, and adding the splash led me to the tabbed project Properties screen which is pretty awesome.


    Jason Beres talks about everything in .NET being a class file and demonstrates this by passing things between 2 forms, activating and deactivating objects and then goes on to demonstrate creating controls dynamically at runtime. This was a bit of revision for me, and I (in a sad way) enjoyed it <GEEK>.


    Public Class Form1


        Private Sub Form1_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Me.TextBox1.Text = "Clickety click"

            Dim rd1 As RadioButton = New RadioButton

            rd1.Location = New Drawing.Point(50, 50)

            rd1.Text = "Blue"


            AddHandler rd1.Click, AddressOf RadioHandler


        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Me.TextBox1.Text = "Hello WOrld!!"

        End Sub


        Public Sub RadioHandler(ByVal sender As Object, ByVal e As EventArgs)

            Select Case sender.text

                Case "Blue"

                    Me.BackColor = Color.Blue

                    sender.Text = "Red"

                Case "Red"

                    Me.BackColor = Color.Red

                    sender.Text = "Blue"

            End Select

        End Sub

    End Class


    Adding in a menu was so childishly simple I thought I’d done it wrong.

        Private Sub HoweasyisthisToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HoweasyisthisToolStripMenuItem.Click

            Me.TextBox1.Text = "Menu Item"

        End Sub


        Private Sub QuitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuitToolStripMenuItem.Click


        End Sub


    The next bit of the day I had to read over again. Inheritance in VS.NET is a biggie, something I need to get my head round and I’m not sure if master pages is inheritance or something else. Thinking about it, it probably is. So, making an inherited form in VS.NET 2005 – easy?

    Add New item, inherited form, form1… pooft! Yup. S’easy.

    Then I found a bug – “Cannot access a disposed object – Object Name: “Designer Frame”

    Hmm. All I did was amend the menu item to open Form2 and did a on click event.

    Visual Studio threw the error box bottom right of my screen and would not go away no matter how many times I clicked OK. Must be a beta2 bug.


     The remainder of the chapter is about prettying things up which I don’t really care too much about, as I’m only revising here. My impression so far of 2005 is that the term Rapid Application Development tool is about an apt a name as could have been thought. The stupid little app I wrote above would have taken ages in VB6. Now it’s down to about 10mins.



    Click here for day 4 - Deploying.


  • Day 2: Easy day, looking at the IDE.

    Tuesday 24th May 2005 – Chapter 2, Introduction to Visual Studio.NET

    Thankfully, todays chapter (based on Jason Beres book – Learn Visual Studio in 21 Days) is quite short. Its not in content, and again, I’m using Vis Studio 2005, not VS.NET 2003 as the book was intended for, but lets be frank here – who actually reads the Introduction to …. chapters? Not me, hence I will always remain an amateur.

    I say thankfully because my day started at 7am (Yes, there’s two 7 o’clocks in a day Ryan) and although I was hoping to get home for 5pm, a burglar (who had the rottenest teeth I have *ever* seen) spoiled all that so by 9pm I was skipping through Chapter 2 – Introduction to Visual Studio.NET.


    Some things have changed. It seem sleeker to me. Nicer. I’m a VB.NET guy, not a _real_ programmer, and so I like the loss of all that frippery. There’s a News bit which is ok, but I think it should reflect the content of WWW.ASP.NET rather than what *they* think we should get. There’s a couple of things missing - Get a Web Hoster… If you can’t find yourself a web-host in the year 2005, then you may as well quit now. 2003 used to direct people to iNNERHOST and I’m assuming they are still ok. I always like Brad Kingsleys Orcsweb though, just because he was so damn passionate about it all.


    Downloads and updates have moved/gone also, which is a good thing IMO. I never ever got update to work on my work or home machine. Ever. And downloads – Google for them. It’s Today.Year.ToString for Christs’ sake.


    Dynamic Help – Well, there is a “How do I” link under help, but what intrigued me more was the community links. I recall the buzz about it at the MVP conference last year but I figured MS was going to retain the old newsgroups (I can’t say it enough – IT’S 2005 – LET IT GO!!!) but I was waaay wrong. They’ve done an excellent job on it. Relevant online help, which is good for me, so that when I search for “Make a random number” I get a bunch of links under the codezone community. I was a tad upset that my old mates at didn’t appear on the lists but I did see some old favourites in there (,,


    The IDE is roughly the same. Its nowhere near as daunting as the original Visual Studio, and far more intuitive – nothing to be scared of here.


    Skipping tons of things, straight to the debug menu – vastly overhauled. I believe there are some bugs within debugging that need ironing out (see Peter Brombergs article – what he said) but it still remains massively superior to what it was.


    Overall, considering it’s late, I’ve worked a 14 hour day trying to outwit a halfwit (a guy with no brains and no teeth), I’m just going to skip this chapter. Some excellent advice in it though. I'm sure his updated book will be just as good.


    Day 3...


  • Finding my feet...

    Jeez, seriously guys. It feels like I've forgotten how to tie my laces, or ride a bike.
    I opened up Visual Studio, almost 12mths to the day after I last did, and wow. Talk about noob.

    Where do you start? I reckon I could do some basic stuff but how far could I go before I need to yell for help on ?

    I recall now once an ex-boss saying ".NET, how hard can it be? We can train this guy up in 6weeks no?". I nearly resigned that day. There's things that make it easier than what it was like 5yrs ago, but there's so much new stuff now that you think, "Where do I start?????"


    I always found the best way to learn something is to have a goal in mind. There's no point just keep looking at snippets and MSDN movies if they are just random bits of knowledge - you have to have some purpose in mind in order that your head-guy can put it in the right pigeon hole. So. Here's my mini requirements.


    I recently passed the Police sergeants exam (Yay!). We all received our feedback the other day and basically, the exam was split into 4 sections (Crime, traffic etc). Each one required a 55% pass rate


       And before you all go 55%??!!! - the exam is rock hard. Multiple choice and the choices are sometimes

          a) Believe

          b) Suspect

          c) Know

          d) Reasonably suspect


    However. Some people managed to get like 4% for some sections and I was (like you) thinking - how??? Its multiple choice. What are the chances of getting less than say 20% in an exam with 140 questions? And there's my first learning project.


    Write a system that

    ·                     Creates 140 random questions, in one of 4 categories, with a random right answer for each (Q1 (CAT1), Answer a. Q2(CAT2) Answer b, Q3(CAT1) Answer b etc). Those categories had a set number of questions in

    ·                     Dump them in a database.

    ·                     Run a random sequence of answers for 7783 candidates (eg - how many sat the English Sergeants exam in March 2005)

    ·                     Dump them in the database

    ·                     Compare the averages with those actually achieved in the exams (ie - highest score, lowest score, averages per category, etc)

    ·                     Make some bar charts?

    ·                     Look at frequencies??

    ·                     Did one category do any better than any of the others?

    ·                     And so on.


    So. To begin. Make the database - dead simple schema, and as my SQL is ok (I'm hoping..!), I don't need to brush up on it too much so I won't go overboard.

    USE [Exam]


    /****** Object:  Table [dbo].[question]    Script Date: 05/21/2005 13:39:41 ******/


    CREATE TABLE [dbo].[question](

           [question_q_id] [int] IDENTITY(1,1) NOT NULL,

           [question_category] [int] NOT NULL,

           [question_answer] [int] NOT NULL

    ) ON [PRIMARY]




    As you can see, I'm not putting the actual questions or answers in, this is just for sake of art.


    I now need a process thats going to create my questions, based on the weightings
    CAT1 - 25 Q's

    CAT2 - 34 Q's

    CAT3 - 43 Q's

    CAT4 - 38 Q's


    There are a number of ways of doing this, I considered a view to make this dynamic for the future, but I reckon easiest would be to just hardcode it in the insertion routine


    So (Cracking knuckles), I finally get to open Visual Studio 2005 with a purpose in mind. Lets make a form app. My first immediate thing is that once again the design form is locked at the left and the Toolbox opens on top of it. I *hate* that. Hate hate hate. I like the new shadowed anchors though that tell you were to drop the window in order for it to add itself to the right hand bar.


    I want to click a button, see whats going into the database, then see the state of the database after that insertion (ie, how many categories have been used up, how many questions generated etc.). Buttons are the same, labels too but what's DataGridView??? I know what a datagrid is but this looks sexy. How good is the wizard? Lets see..

    Choose data source, none available so it tells me to make one.
    Add Project data source

    New Connection... all good so far

    Test, choose data objects to go in the datagrid, Done.

    <Insert_amazed_expletive_of_choice> here... If that works first time I'll be well impressed.

    I can't tell you how long that would have taken in notepad!



    Awesome... :D


    Aaaaaaaand - it works!!!!

    Sexy shit indeed.


    Damnit – daughter has rebooted my machine. Grrr…it’s not saved. Never mind


    Next step is to insert the random questions, which means I need to generate them first, which means I need a question object first?? Where do I build a question object? I recall vaguely it went in Main() .. gimme a sec.
    Solution Explorer, Add New Item..

    (Splash screen?!)
    (Just seen module, I think my function needs to go in there..)


    Holy crap, this is getting far too complex now. I need help. Scrap the objects for now, revert back to simple code (eg, hide head in sand).


    Make a random number between 1 and 4 – how easy could that be? Snippet?

            Dim generator As New Random

            Dim randomValue As Integer

            ' Generates numbers between 1 and 5, inclusive.

            randomValue = generator.Next(1, 6)

    Hmm, not really random if memory serves me correctly (it doesn’t) but it’ll do. We used to do it using the current time as the seed. Ctype( something something)


        Public Function make_random_number(ByVal iCat As Integer) As Integer


            Dim generator As New Random(CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))

            ' Generates numbers between 1 and 5, inclusive.

            Return generator.Next(1, 5)


        End Function


    Seems to work J - HOWEVER – its not as good as just using New Random() on its own???!!!

    So now I need to know how to insert into the database. I’ll definitely have to look this one up.

    Looking at the snippets it seems the best way is to make a datatable from my random numbers and then update the database with that. I just want to get this working though now, getting tired. Simple insert?

    There's a snippet for an update which should work, if I add the sqlConnection object and change the update to an insert.


            Dim oConnection As New SqlClient.SqlConnection("Data Source=yoda;Initial Catalog=Exam;Integrated Security=True")

            Dim UpdateStatement As String = " "INSERT INTO [Exam].[dbo].[question]  ([question_category],[question_answer]) VALUES (" & iCat & "  ," & iRan & ")""

            Dim UpdateCommand As New SqlClient.SqlCommand(UpdateStatement, oConnection)



                Dim rowCount As Integer

                rowCount = UpdateCommand.ExecuteNonQuery()

            Catch ex As Exception

                Throw ex



            End Try


    I **LOVE** the way the snippet thing iterates down and changes the object names as you type them in. Well thought out for noobs and pro’s alike.


    After a few runs of my amateur routine its quite clear that the questions aren’t being randomly generated. The time element of the generator is too fast, so I added in a threading.thread.sleep(randomNumber) to make it livelier. Then I discovered that the random generator actually produced MORE random numbers than doing it the old way with system.time etc. The whole thing now looks like this:


    Imports System.Data

    Public Class Form1


        Private Sub btn_Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Start.Click


            ' Cat1 = 25, 2=34, 3=43, 4=38


            lbl_Progress.Text = "Working.."


            Dim iCtr As Integer = 25

            For iCtr = 1 To 25

                If iCtr = 1 Then

                    insert_question_to_database(1, make_random_number(5), True)


                    insert_question_to_database(1, make_random_number(5), False)

                End If




            For iCtr = 1 To 34

                insert_question_to_database(2, make_random_number(5), False)



            For iCtr = 1 To 43

                insert_question_to_database(3, make_random_number(5), False)



            For iCtr = 1 To 38

                insert_question_to_database(4, make_random_number(5), False)



            lbl_Progress.Text = "Done. Reload the view"



        End Sub


        Public Function make_random_number(ByVal max_Num As Integer) As Integer


            Dim generator As New Random()

            ' Generates numbers between 1 and 5, inclusive.

            Return generator.Next(1, max_Num)


        End Function


        Public Function insert_question_to_database(ByVal iCat As Integer, ByVal iRan As Integer, ByVal bFirstRun As Boolean) As Boolean


            Dim oConnection As New SqlClient.SqlConnection("Data Source=yoda;Initial Catalog=Exam;Integrated Security=True")

            Dim UpdateStatement As String

    ‘ if this is the first run, delete the contents of Exam.

            If bFirstRun = True Then

                UpdateStatement = "Truncate table question; " & "INSERT INTO [Exam].[dbo].[question]  ([question_category],[question_answer]) VALUES (" & iCat & "  ," & iRan & ")"


                UpdateStatement = "INSERT INTO [Exam].[dbo].[question]  ([question_category],[question_answer]) VALUES (" & iCat & "  ," & iRan & ")"

            End If

            Dim UpdateCommand As New SqlClient.SqlCommand(UpdateStatement, oConnection)



                Dim rowCount As Integer

                rowCount = UpdateCommand.ExecuteNonQuery()

            Catch ex As Exception

                Throw ex



            End Try

            Return True


        End Function

    End Class


    Ok, a bit simplistic but it’s done what I want without too much fuss. Heavy on the SQL, too much traffic over the wire, too many inserts as opposed to one dataset update but I’m happy with it, considering it’s my second day on the job. I WILL HOWEVER BE GRATEFUL FOR POINTERS ON THE NEW WAY OF DOING THINGS (Ta).


    Now, keeping the same logic, I’ll add in the 7783 random entries and see if the averages reflect those of the actual exam and whether virtual monkeys could have achieved better results…


            For iCtr = 1 To 7783

                For iQ = 1 To 140

                    insert_answer_to_database(iCtr, iQ, make_random_number(5), False)




    ?? Pffft Lol. After about 2mins of grinding along I realised just how_bad this method was.

    What did I miss?? 7783*140 = 1,089,620 inserts.

    Did I destroy the connection object? NO (Slap).

    Makes no difference though.

    I think I’m going to have to dig into objects, create a dataset, hold it in memory and then update the table using the dataset.


    OR, I could go shopping instead.

    ‘goes shopping….One hour later its almost finished…. OKAY! I’ll do a bloody collection…


    This took me an afternoon to blunder through. Its quite clear to me (now) that I really need to brush up on my collections, OOP and datasets/views/etc before I can make something halfway decent. I suppose my next ramblings will be about collections. As a sneak preview, the highest random score was 56%, which means there’s some really thick coppers in England lol.


  • First findings

    First thing is the fact that after year of not using it, and more than 2yrs of not having upgraded it, my home machine is now woefully inadequate for Visual Studio.NET 2005. I have a 1.8g Athlon, 1g RAM and other bits. It's ok, I had to do a clean install of XP Pro (Server just bluescreened so I think I have RAM problems) but my desire to install outweighed my cash-ability to buy a new PC.