Creating FK’s

Posted by Monty on February 4th, 2009

If you get the following error, when trying to export data from YAF:

sqlpubwiz

You might want to try running this script, to generate the create FK statements. Inconjuction with the drop FK statements, it will alow you to generate YAF scripts painlessly.


SELECT 'ALTER TABLE ' +  OBJECT_NAME(f.parent_object_id) + ' ADD CONSTRAINT '  + f.name + ' FOREIGN KEY (' + COL_NAME(fc.parent_object_id, fc.parent_column_id) + ') REFERENCES ' + OBJECT_NAME (f.referenced_object_id) + '(' + COL_NAME(fc.referenced_object_id, fc.referenced_column_id) + ')' FROM sys.foreign_keys AS f INNER JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id

It should give you an output like:


ALTER TABLE yaf_Choice ADD CONSTRAINT FK_yaf_Choice_yaf_Poll FOREIGN KEY (PollID) REFERENCES yaf_Poll(PollID)
ALTER TABLE yaf_Topic ADD CONSTRAINT FK_yaf_Topic_yaf_Poll FOREIGN KEY (PollID) REFERENCES yaf_Poll(PollID)
ALTER TABLE yaf_PollVote ADD CONSTRAINT FK_yaf_PollVote_yaf_Poll FOREIGN KEY (PollID) REFERENCES yaf_Poll(PollID)
ALTER TABLE yaf_AccessMask ADD CONSTRAINT FK_yaf_AccessMask_yaf_Board FOREIGN KEY (BoardID) REFERENCES yaf_Board(BoardID)
ALTER TABLE yaf_Active ADD CONSTRAINT FK_yaf_Active_yaf_Board FOREIGN KEY (BoardID) REFERENCES yaf_Board(BoardID)
ALTER TABLE yaf_BannedIP ADD CONSTRAINT FK_yaf_BannedIP_yaf_Board FOREIGN KEY (BoardID) REFERENCES yaf_Board(BoardID)
ALTER TABLE yaf_Category ADD CONSTRAINT FK_yaf_Category_yaf_Board FOREIGN KEY (BoardID) REFERENCES yaf_Board(BoardID)
ALTER TABLE yaf_Group ADD CONSTRAINT FK_yaf_Group_yaf_Board FOREIGN KEY (BoardID) REFERENCES yaf_Board(BoardID)
ALTER TABLE yaf_NntpServer ADD CONSTRAINT FK_yaf_NntpServer_yaf_Board FOREIGN KEY (BoardID) REFERENCES yaf_Board(BoardID)
ALTER TABLE yaf_Rank ADD CONSTRAINT FK_yaf_Rank_yaf_Board FOREIGN KEY (BoardID) REFERENCES yaf_Board(BoardID)
ALTER TABLE yaf_Registry ADD CONSTRAINT FK_yaf_Registry_yaf_Board FOREIGN KEY (BoardID) REFERENCES yaf_Board(BoardID)
ALTER TABLE yaf_Smiley ADD CONSTRAINT FK_yaf_Smiley_yaf_Board FOREIGN KEY (BoardID) REFERENCES yaf_Board(BoardID)
ALTER TABLE yaf_User ADD CONSTRAINT FK_yaf_User_yaf_Board FOREIGN KEY (BoardID) REFERENCES yaf_Board(BoardID)
ALTER TABLE yaf_User ADD CONSTRAINT FK_yaf_User_yaf_Rank FOREIGN KEY (RankID) REFERENCES yaf_Rank(RankID)
ALTER TABLE yaf_Active ADD CONSTRAINT FK_yaf_Active_yaf_User FOREIGN KEY (UserID) REFERENCES yaf_User(UserID)
ALTER TABLE yaf_CheckEmail ADD CONSTRAINT FK_yaf_CheckEmail_yaf_User FOREIGN KEY (UserID) REFERENCES yaf_User(UserID)
ALTER TABLE yaf_EventLog ADD CONSTRAINT FK_yaf_EventLog_yaf_User FOREIGN KEY (UserID) REFERENCES yaf_User(UserID)
ALTER TABLE yaf_Forum ADD CONSTRAINT FK_yaf_Forum_yaf_User FOREIGN KEY (LastUserID) REFERENCES yaf_User(UserID)
ALTER TABLE yaf_Message ADD CONSTRAINT FK_yaf_Message_yaf_User FOREIGN KEY (UserID) REFERENCES yaf_User(UserID)
ALTER TABLE yaf_PMessage ADD CONSTRAINT FK_yaf_PMessage_yaf_User1 FOREIGN KEY (FromUserID) REFERENCES yaf_User(UserID)
ALTER TABLE yaf_Topic ADD CONSTRAINT FK_yaf_Topic_yaf_User FOREIGN KEY (UserID) REFERENCES yaf_User(UserID)
ALTER TABLE yaf_Topic ADD CONSTRAINT FK_yaf_Topic_yaf_User2 FOREIGN KEY (LastUserID) REFERENCES yaf_User(UserID)
ALTER TABLE yaf_UserForum ADD CONSTRAINT FK_yaf_UserForum_yaf_User FOREIGN KEY (UserID) REFERENCES yaf_User(UserID)
ALTER TABLE yaf_UserGroup ADD CONSTRAINT FK_yaf_UserGroup_yaf_User FOREIGN KEY (UserID) REFERENCES yaf_User(UserID)
ALTER TABLE yaf_UserPMessage ADD CONSTRAINT FK_yaf_UserPMessage_yaf_User FOREIGN KEY (UserID) REFERENCES yaf_User(UserID)
ALTER TABLE yaf_WatchForum ADD CONSTRAINT FK_yaf_WatchForum_yaf_User FOREIGN KEY (UserID) REFERENCES yaf_User(UserID)
ALTER TABLE yaf_WatchTopic ADD CONSTRAINT FK_yaf_WatchTopic_yaf_User FOREIGN KEY (UserID) REFERENCES yaf_User(UserID)
ALTER TABLE yaf_Forum ADD CONSTRAINT FK_yaf_Forum_yaf_Category FOREIGN KEY (CategoryID) REFERENCES yaf_Category(CategoryID)
ALTER TABLE yaf_Active ADD CONSTRAINT FK_yaf_Active_yaf_Forum FOREIGN KEY (ForumID) REFERENCES yaf_Forum(ForumID)
ALTER TABLE yaf_Forum ADD CONSTRAINT FK_yaf_Forum_yaf_Forum FOREIGN KEY (ParentID) REFERENCES yaf_Forum(ForumID)
ALTER TABLE yaf_ForumAccess ADD CONSTRAINT FK_yaf_ForumAccess_yaf_Forum FOREIGN KEY (ForumID) REFERENCES yaf_Forum(ForumID)
ALTER TABLE yaf_NntpForum ADD CONSTRAINT FK_yaf_NntpForum_yaf_Forum FOREIGN KEY (ForumID) REFERENCES yaf_Forum(ForumID)
ALTER TABLE yaf_Topic ADD CONSTRAINT FK_yaf_Topic_yaf_Forum FOREIGN KEY (ForumID) REFERENCES yaf_Forum(ForumID)
ALTER TABLE yaf_UserForum ADD CONSTRAINT FK_yaf_UserForum_yaf_Forum FOREIGN KEY (ForumID) REFERENCES yaf_Forum(ForumID)
ALTER TABLE yaf_WatchForum ADD CONSTRAINT FK_yaf_WatchForum_yaf_Forum FOREIGN KEY (ForumID) REFERENCES yaf_Forum(ForumID)
ALTER TABLE yaf_Attachment ADD CONSTRAINT FK_yaf_Attachment_yaf_Message FOREIGN KEY (MessageID) REFERENCES yaf_Message(MessageID)
ALTER TABLE yaf_Forum ADD CONSTRAINT FK_yaf_Forum_yaf_Message FOREIGN KEY (LastMessageID) REFERENCES yaf_Message(MessageID)
ALTER TABLE yaf_Message ADD CONSTRAINT FK_yaf_Message_yaf_Message FOREIGN KEY (ReplyTo) REFERENCES yaf_Message(MessageID)
ALTER TABLE yaf_Topic ADD CONSTRAINT FK_yaf_Topic_yaf_Message FOREIGN KEY (LastMessageID) REFERENCES yaf_Message(MessageID)
ALTER TABLE yaf_Active ADD CONSTRAINT FK_yaf_Active_yaf_Topic FOREIGN KEY (TopicID) REFERENCES yaf_Topic(TopicID)
ALTER TABLE yaf_Forum ADD CONSTRAINT FK_yaf_Forum_yaf_Topic FOREIGN KEY (LastTopicID) REFERENCES yaf_Topic(TopicID)
ALTER TABLE yaf_Message ADD CONSTRAINT FK_yaf_Message_yaf_Topic FOREIGN KEY (TopicID) REFERENCES yaf_Topic(TopicID)
ALTER TABLE yaf_NntpTopic ADD CONSTRAINT FK_yaf_NntpTopic_yaf_Topic FOREIGN KEY (TopicID) REFERENCES yaf_Topic(TopicID)
ALTER TABLE yaf_Topic ADD CONSTRAINT FK_yaf_Topic_yaf_Topic FOREIGN KEY (TopicMovedID) REFERENCES yaf_Topic(TopicID)
ALTER TABLE yaf_WatchTopic ADD CONSTRAINT FK_yaf_WatchTopic_yaf_Topic FOREIGN KEY (TopicID) REFERENCES yaf_Topic(TopicID)
ALTER TABLE yaf_NntpForum ADD CONSTRAINT FK_yaf_NntpForum_yaf_NntpServer FOREIGN KEY (NntpServerID) REFERENCES yaf_NntpServer(NntpServerID)
ALTER TABLE yaf_NntpTopic ADD CONSTRAINT FK_yaf_NntpTopic_yaf_NntpForum FOREIGN KEY (NntpForumID) REFERENCES yaf_NntpForum(NntpForumID)
ALTER TABLE yaf_ForumAccess ADD CONSTRAINT FK_yaf_ForumAccess_yaf_AccessMask FOREIGN KEY (AccessMaskID) REFERENCES yaf_AccessMask(AccessMaskID)
ALTER TABLE yaf_UserForum ADD CONSTRAINT FK_yaf_UserForum_yaf_AccessMask FOREIGN KEY (AccessMaskID) REFERENCES yaf_AccessMask(AccessMaskID)
ALTER TABLE yaf_ForumAccess ADD CONSTRAINT FK_yaf_ForumAccess_yaf_Group FOREIGN KEY (GroupID) REFERENCES yaf_Group(GroupID)
ALTER TABLE yaf_UserGroup ADD CONSTRAINT FK_yaf_UserGroup_yaf_Group FOREIGN KEY (GroupID) REFERENCES yaf_Group(GroupID)
ALTER TABLE yaf_UserPMessage ADD CONSTRAINT FK_yaf_UserPMessage_yaf_PMessage FOREIGN KEY (PMessageID) REFERENCES yaf_PMessage(PMessageID)

Deleting FK’s from MSSQL

Posted by Monty on January 30th, 2009

SQL SERVER – 2005 – Find Tables With Foreign Key Constraint in Database

via SQL SERVER – 2005 – Find Tables With Foreign Key Constraint in Database « Journey to SQL Authority with Pinal Dave.

This script is a godsend. Basically, we had a botched install of Yet Another Forum, and we had to delete it (YAY!), but because of the FK constraints, we couldnt do a simple “DROP TABLE *” on the database, and nor could we just drop the database.

So I came up with this script that generated the SQL Script for me, based on the above:


SELECT 'ALTER TABLE ' +  OBJECT_NAME(f.parent_object_id) + ' DROP CONSTRAINT [' + f.name + ']' AS Moo, f.name AS ForeignKey,
OBJECT_NAME(f.parent_object_id) AS TableName,
COL_NAME(fc.parent_object_id,
fc.parent_column_id) AS ColumnName,
OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
COL_NAME(fc.referenced_object_id,
fc.referenced_column_id) AS ReferenceColumnName
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id

Can be refined alot better, but works. The resulting script is as follows – note I added the “drop table” by hand:

ALTER TABLE yaf_Topic DROP CONSTRAINT [FK_yaf_Topic_yaf_Forum]
ALTER TABLE yaf_Active DROP CONSTRAINT [FK_yaf_Active_yaf_Forum]
ALTER TABLE yaf_ForumAccess DROP CONSTRAINT [FK_yaf_ForumAccess_yaf_Forum]
ALTER TABLE yaf_WatchForum DROP CONSTRAINT [FK_yaf_WatchForum_yaf_Forum]
ALTER TABLE yaf_NntpForum DROP CONSTRAINT [FK_yaf_NntpForum_yaf_Forum]
ALTER TABLE yaf_UserForum DROP CONSTRAINT [FK_yaf_UserForum_yaf_Forum]
ALTER TABLE yaf_Forum DROP CONSTRAINT [FK_yaf_Forum_yaf_Forum]
ALTER TABLE yaf_Forum DROP CONSTRAINT [FK_yaf_Forum_yaf_Message]
ALTER TABLE yaf_Topic DROP CONSTRAINT [FK_yaf_Topic_yaf_Message]
ALTER TABLE yaf_Attachment DROP CONSTRAINT [FK_yaf_Attachment_yaf_Message]
ALTER TABLE yaf_Message DROP CONSTRAINT [FK_yaf_Message_yaf_Message]
ALTER TABLE yaf_Topic DROP CONSTRAINT [FK_yaf_Topic_yaf_Poll]
ALTER TABLE yaf_Choice DROP CONSTRAINT [FK_yaf_Choice_yaf_Poll]
ALTER TABLE yaf_PollVote DROP CONSTRAINT [FK_yaf_PollVote_yaf_Poll]
ALTER TABLE yaf_Forum DROP CONSTRAINT [FK_yaf_Forum_yaf_Topic]
ALTER TABLE yaf_Message DROP CONSTRAINT [FK_yaf_Message_yaf_Topic]
ALTER TABLE yaf_Topic DROP CONSTRAINT [FK_yaf_Topic_yaf_Topic]
ALTER TABLE yaf_Active DROP CONSTRAINT [FK_yaf_Active_yaf_Topic]
ALTER TABLE yaf_WatchTopic DROP CONSTRAINT [FK_yaf_WatchTopic_yaf_Topic]
ALTER TABLE yaf_NntpTopic DROP CONSTRAINT [FK_yaf_NntpTopic_yaf_Topic]
ALTER TABLE yaf_Forum DROP CONSTRAINT [FK_yaf_Forum_yaf_User]
ALTER TABLE yaf_Message DROP CONSTRAINT [FK_yaf_Message_yaf_User]
ALTER TABLE yaf_Topic DROP CONSTRAINT [FK_yaf_Topic_yaf_User]
ALTER TABLE yaf_Topic DROP CONSTRAINT [FK_yaf_Topic_yaf_User2]
ALTER TABLE yaf_Active DROP CONSTRAINT [FK_yaf_Active_yaf_User]
ALTER TABLE yaf_CheckEmail DROP CONSTRAINT [FK_yaf_CheckEmail_yaf_User]
ALTER TABLE yaf_PMessage DROP CONSTRAINT [FK_yaf_PMessage_yaf_User1]
ALTER TABLE yaf_WatchForum DROP CONSTRAINT [FK_yaf_WatchForum_yaf_User]
ALTER TABLE yaf_WatchTopic DROP CONSTRAINT [FK_yaf_WatchTopic_yaf_User]
ALTER TABLE yaf_UserGroup DROP CONSTRAINT [FK_yaf_UserGroup_yaf_User]
ALTER TABLE yaf_UserForum DROP CONSTRAINT [FK_yaf_UserForum_yaf_User]
ALTER TABLE yaf_UserPMessage DROP CONSTRAINT [FK_yaf_UserPMessage_yaf_User]
ALTER TABLE yaf_EventLog DROP CONSTRAINT [FK_yaf_EventLog_yaf_User]
ALTER TABLE yaf_User DROP CONSTRAINT [FK_yaf_User_yaf_Rank]
ALTER TABLE yaf_Category DROP CONSTRAINT [FK_yaf_Category_yaf_Board]
ALTER TABLE yaf_Rank DROP CONSTRAINT [FK_yaf_Rank_yaf_Board]
ALTER TABLE yaf_AccessMask DROP CONSTRAINT [FK_yaf_AccessMask_yaf_Board]
ALTER TABLE yaf_Active DROP CONSTRAINT [FK_yaf_Active_yaf_Board]
ALTER TABLE yaf_User DROP CONSTRAINT [FK_yaf_User_yaf_Board]
ALTER TABLE yaf_BannedIP DROP CONSTRAINT [FK_yaf_BannedIP_yaf_Board]
ALTER TABLE yaf_Group DROP CONSTRAINT [FK_yaf_Group_yaf_Board]
ALTER TABLE yaf_NntpServer DROP CONSTRAINT [FK_yaf_NntpServer_yaf_Board]
ALTER TABLE yaf_Smiley DROP CONSTRAINT [FK_yaf_Smiley_yaf_Board]
ALTER TABLE yaf_Registry DROP CONSTRAINT [FK_yaf_Registry_yaf_Board]
ALTER TABLE yaf_ForumAccess DROP CONSTRAINT [FK_yaf_ForumAccess_yaf_Group]
ALTER TABLE yaf_UserGroup DROP CONSTRAINT [FK_yaf_UserGroup_yaf_Group]
ALTER TABLE yaf_UserPMessage DROP CONSTRAINT [FK_yaf_UserPMessage_yaf_PMessage]
ALTER TABLE yaf_ForumAccess DROP CONSTRAINT [FK_yaf_ForumAccess_yaf_AccessMask]
ALTER TABLE yaf_UserForum DROP CONSTRAINT [FK_yaf_UserForum_yaf_AccessMask]
ALTER TABLE yaf_NntpForum DROP CONSTRAINT [FK_yaf_NntpForum_yaf_NntpServer]
ALTER TABLE yaf_NntpTopic DROP CONSTRAINT [FK_yaf_NntpTopic_yaf_NntpForum]
ALTER TABLE yaf_Forum DROP CONSTRAINT [FK_yaf_Forum_yaf_Category]

drop table dbo.yaf_AccessMask
drop table dbo.yaf_Active
drop table dbo.yaf_Attachment
drop table dbo.yaf_BannedIP
drop table dbo.yaf_Board
drop table dbo.yaf_Category
drop table dbo.yaf_CheckEmail
drop table dbo.yaf_Choice
drop table dbo.yaf_EventLog
drop table dbo.yaf_Forum
drop table dbo.yaf_ForumAccess
drop table dbo.yaf_Group
drop table dbo.yaf_Mail
drop table dbo.yaf_Message
drop table dbo.yaf_NntpForum
drop table dbo.yaf_NntpServer
drop table dbo.yaf_NntpTopic
drop table dbo.yaf_PMessage
drop table dbo.yaf_Poll
drop table dbo.yaf_PollVote
drop table dbo.yaf_Rank
drop table dbo.yaf_Registry
drop table dbo.yaf_Replace_Words
drop table dbo.yaf_Smiley
drop table dbo.yaf_Topic
drop table dbo.yaf_User
drop table dbo.yaf_UserForum
drop table dbo.yaf_UserGroup
drop table dbo.yaf_UserPMessage
drop table dbo.yaf_WatchForum
drop table dbo.yaf_WatchTopic
drop view dbo.yaf_vaccess

Microsoft.WebApplication.targets

Posted by Monty on January 28th, 2009

I seem to be getting quite a few hits regarding Microsoft.WebApplication.targets file, and im guessing its because people’s copies are missing, so I have decided to attach the file to this post. Enjoy!

Linky

<%@ Page directive and language

Posted by Monty on January 16th, 2009

Compilation Error

Compiler Error Message: BC32017: Comma, ‘)’, or a valid expression continuation expected.
<% Response.Write(”

Turns out if you dont declare the page language in the <%@ Page directive, it will default to a VB page and try to compile it that way.

Just another member of the public…

Posted by Monty on December 15th, 2008

Last night, on the way home from Airsofting (and dropping friends off at the train station), I came across quite a bit of traffic – loads of traffic infact, so I waited and waited, and finally got to see what was causing all the traffic – there was a guy, laying on the road (on a pedestrian crossing), with blood running down his face. I thought he’d been run over, thats what it seemed like. There was also about 20-30 people just gawping at him, just staring and doing nothing and being totally useless.

As I drove by the guy, my consciousness kicked in and I thought I couldnt leave this guy – I parked up as soon as I could, and basically ran to the scene of what happened. There were 3-4 dominoes pizza drivers, so I thought they might have known this person, but im not too sure now. I asked around, asking if anyone has called the ambulance, I didnt get a reply. I asked if somoene had called the police, someone said “Yeah, yeah we have”. I quickly pulled out my phone, dialled 999 and spoke to the ambulance operator, who told me that they have been notified by the police, and if the man’s situation deteriorates, call them back. After this point, I decided to kneel down and have a chat with the person, and see if he is actually alive.

When I knelt down, I could smell ETOH (alcyhol) on him, and I thought “Oh, hrm, probably a drunk”, but I still stayed there. I noticed he was wearing fairly decent clothing, he was just drunk – it was a Sunday night around 9:30, just guessed he was out for a few pints. His face was scratched up (He was laying down on his left hand side, I saw blood on the RHS of his face). He also had scratched up hands. I looked up and down him, and saw some scuff marks on his boots.

He seemed to be conscious, his eyes were open and blinking, he was breathing fine, I put my (gloved) hand on his arm and told him that everythings going to be fine, that an ambulance is on its way, and everythings going to be ok. I put my knee right upto his back and kept a hand underneath his head, because I thought there was a risk to his spine. While I was doing that,  I asked around to see if anyone knew what happened, and a young chap, around 25, clean shaven, fair hair, grey jacket, about 5ft8 said that the guy had tried to cross when “the light was red – green for cars” and just got hit by a car. I knew that holding the guy’s head up would be a good idea, man vs car there is going to be some problems for the spine.

After a few mins, the patient said to me “Im ok”, I told him something along the lines of “Ok, dont worry, the ambulance will be here soon”. After a minute or two, he said the same thing, and tried to get up – I held his head down, and told him not to move, that the ambulance will be here very soon and they will check him out. He muttered something about “that f***ing c**t didnt stop for me”.

A few minutes later, someone said “The ambulance is on its way” – he was standing up, I was kneeling on the floor, but after about 30 seconds it became clear – there were blue flashing lights heading this way. I told the patient (who once again said he was ok that the amublance is here now and they will look after him and not to worry), but it wasnt an ambulance, it was a police car (A new police car with LED flashy lights, instead of the old swirly lights).

One of the policeman got out of the car and headed straight towards me, and started asking me a question – I cut him off, and asked him “Do you have anything to cover him up with? He is cold”, he said “Yeah sure” and wondered back off to his car – he came back empty handed, so I told him to take off his coat which he did and covered the guy. After that, he told the crowd of gawpers to back off and there was nothing to look at here, and they should go. The other police officer closed off the road and started redirecting traffic – phew, because I was getting worried that I might get run over. The police officer who I ordered to take off his jacket was on the blower, saying (to his Sarge) that there was a male adult on the road whos been hit by a car and they were closing the road off…

A few minutes after that, a big yellow taxi (also known as an ambulance) turned up, and out jumped 2 paramedics (I heard the female one say “Wow, someone who knows what they are doing” when they got out of the ambu), who told even more people to shoo away, and worked around me. They left me there to hold his head up, trusting me to do a good job. The male started asking the patient questions such as what is your name (Mark), what happened, have you had anything to drink (the patient got defensive, then the paramedic said something along the lines of we dont care, we just need to know for our purposes, then the guy admitted to 3 pints), then asked if he had any heart trouble or diabetes. The paramedic then called over to the guy who hit the patient with his car, and asked him what speed he was doing, he said something like “25 or 30″, the paramedic then asked “Did the guy hit the windscreen, did his head ” then mimed bouncing off, the driver replied with “Yeah yeah he did”. The paramedic then asked the patient if he lost consciousness, he said “Yes”.

The female paramedic came back with 2 kitbags, and started unfolding a cervical collar, and started by trying to take off the patient’s coat, but wasnt managing it. She told the patient that they were going to cut it off, and he got agitated and tried to get up – me and the male paramedic held his neck down and told him to be calm and that it would be ok – the female paramedic tried to take off his jacket again, and with some fluffing around we managed to do it. I moved my knee, held his back with one arm, pulled his jacket off, then put my knee back where it was – it was aligning his whole body with his neck, as he was laying on his side.

She also started unfolding a metal board scoop thingy (A scoop stretcher), and adjusted it to his height – when we were going to roll the guy over onto the board, she asked me to get a police officer to help – one of the police officers was directing traffic, one was questioning the person whod run the poor guy over, and one was half in his police car, talking to his sarge over the radio – I pointed at him, and did the “come here now” sign, and he rushed over, telling his Sarge on the radio “Ill get back to you Sarge, im assisting the LAS”.

We manoevered him onto the board, and rolled him back onto the ground – then the female paramedic started screaming about her leg so I helped lift up the board a bit so she could get her foot out. The male paramedic set about trying to strap the patients head down to the boards that keep the head steady, when the female paramedic said “We need you down that end”. As I got to the other end of the patient, he tried to get up – when the paramedics shouted at him to keep still and dont try to get up. They managed to strap him down and told him to stop moving and stop trying to get up. Me, and the female paramedic were at his feet, the male paramedic and the police officer at his head, lifted the patient onto the board. WOW, he was alot heavier than I thought, even with 4 of us carrying him.

The paramedics loaded him up onto the big white taxi, and that was it for me. They thanked me for my help, and the male paramedic asked why I was helping, and if I ran him over – I explained to him that I was passing by and I thought id help out with my first aid skills.

After the whole incident, I followed the police officer (Who I ordered to take his jacket off) to go look at the car that did the “running over” – 55 reg BMW, had a nice spider web pattern on the windscreen, on the drivers side, presumably where the guy’s head hit the windscreen. I waited for the police officer to complete his enquiries on the radio, then I explained to him what I heard some of the witnesses saying – I asked him if that would have been any help, he said it was very helpful, because no one came forward and told the police that they saw what happened. He took my name and address and mobile phone number down and said I was very helpful today, and thanked me, then shook my hand.

ForEach for IEnumerable

Posted by Monty on December 11th, 2008

My Technobabble : ForEach, a simple but very useful extension method

This evening I was writing some code (Yay!) for an Xml based MEF catalog I am prototyping. I came across the need to invoke a set of methods on an IEnumerable that was returned from a LINQ to XML query. Unfortunately no such animal exists on IEnumerable.

This came in very useful, thanks Glenn – basically, I needed a ForEach to handle some lamda’s, on a Stack<T>, and this was very useful!

Commenting (And testing ScribeFire)

Posted by Monty on December 11th, 2008

The comment of all comments « Freekshow

What does the line of comment add? Does it tell you anything that the code does not? Indeed not. Zero points. This kind of gratuitous comments is all too common, and if you have many of these, will you keep reading them? Will you not start to blank out the comments in your mind, thereby possibly missing the important comment that actually does explain a certain coding decision?

Finanlly, what I consider to be a proper developer, agrees that commenting is bad. His example image proves my point:


The comment literally does not add anything – the code signature says everything you need to know – List<Joke> GetMostPopularJokes – Im guessing that its going to get you a list of the most popular jokes.

Testing ScribeFire for Firefox…

Testing Clipmarks…

Posted by Monty on December 10th, 2008
clipped from addons.mozilla.org
Instead of copying and pasting links, Clipmarks is like adding a pair of scissors to your browser, letting you capture exactly what you want others to see (text, images or video). Your clips are saved on clipmarks.com and can easily be syndicated to FriendFeed, Twitter, Facebook or other sites. You can also post anything you clip directly to your blog (supports WordPress, Blogger, Typepad and more) or send it directly to friends or co-workers via email. Clipmarks also gives you a way to help the environment by printing only the parts of a page that you need. By printing just what you need, you’ll save ink, paper and trees, thus making a contribution to a cleaner environment.
  blog it

Extending Nettiers Part 2 – Adding an OnItemCommand

Posted by Monty on November 21st, 2008

A few days ago, I decided to try to extend the Repeater Control, by adding an OnItemCommand, like the System.Web.UI.WebControl. Simple I thought. Shouldnt be that hard I thought.

I couldnt have been more wrong.

I have learned alot with the whole experience. I have learned about event bubbling, the way that events get passed up the control tree till they are handled by their parents. Iv learned about the way that items that are nested will need to be databound for the event to be fired, even if the data is being shown on the page, you still have to explicitly call DataBind(); And I got some help from a random guy (who works for telerik!) on StackOverflow, thanks!

Basically, this is the way it works. When a user hits a button (Like a <asp:button) on the page, it will bubble up (OnBubbleEvent) on the ITemplate (In this case, the actual type of the ItemRepeater). That will do its magic, work out if its a CommandEventArgs, if it is, then bubble it up:


protected override bool OnBubbleEvent(object source, EventArgs e)
{
if (e is CommandEventArgs)
{
RepeaterCommandEventArgs args = new RepeaterCommandEventArgs(this, source, (CommandEventArgs) e);
base.RaiseBubbleEvent(this, args);
return true;
}
return false;
}

Taken from System.Web.UI.WebControls.RepeaterItem.OnBubbleEvent(object source, EventArgs e);

From there, the Repeater will intercept that, then process it:

private static readonly object EventItemCommand = new object();

protected override bool OnBubbleEvent(object sender, EventArgs e)
{
bool flag = false;
if (e is RepeaterCommandEventArgs)
{
this.OnItemCommand((RepeaterCommandEventArgs)e);
flag = true;
}
return flag;
}

protected virtual void OnItemCommand(RepeaterCommandEventArgs e)
{
RepeaterCommandEventHandler handler = (RepeaterCommandEventHandler)base.Events[EventItemCommand];
if (handler != null)
{
handler(this, e);
}
}

public event RepeaterCommandEventHandler ItemCommand
{
add
{
base.Events.AddHandler(EventItemCommand, value);
}
remove
{
base.Events.RemoveHandler(EventItemCommand, value);
}
}

This is where the fun starts. First off, in the RepeaterItem source code, you cannot call:

RepeaterCommandEventArgs args = new RepeaterCommandEventArgs(<strong>this</strong>, source, (CommandEventArgs) e);
base.RaiseBubbleEvent(this, args);

because this refers to a RepeaterItem, which, in my case, it is not because its a Nettiers generated RepeaterItem. So you have to go off and create your own CommandEventArgs. If you create your own EventArgs, the event handler RepeaterCommandEventHandler wont work, because its not of the type RepeaterCommandEventArgs. Which is a pain in the arse!

Once you have created the EventArgs and the event handler though, its fairly simple from there – you just modify the Reflector’ed code (From the RepeaterControl), to get it to handle your custom Repeater Item’s event, and not the RepeateItem default event which you cant even call!

But your not done! I found out that the command wont get called when someone clicky’s on it, because you havnt called DataBind(); on the control, which explains some … oddities I spotted in the code when I was digging around inside the Repeater – I kept finding calls to “EnsureDataBound()” – which is called OnPreRender:

Note: I will provide code samples and Nettiers templates very soon, as soon as I have integrated my changes into Nettiers!

I am quite worried about the standard of coding these days

Posted by Monty on November 18th, 2008

Ok, this is going to make me sound like a really old fart, which im not! Honest!

I was reading the blog of a Microsoft developer, someone whos team owns the Framework Design Guidelines, someone who worked on the VS Code Analysis team, and worked on FxCop.

Sample from davesbox.com

He then later states:

Comment from DavesBox.com

Im sorry, but WHAT THE F**K? Your not a fan of defensive coding, but you want us to do it in framework classes? With your code sample above, if label is null, guess what happens to your code? You may not like defensive coding, so you are going to assume that all of your code samples will work?

SetLabelTextIfNotEmpty(null);
SetLabelTextIfNotEmpty(Label1);  // No instance

Please tell me how your code will react to this? I guarentee that it will throw a null reference exception, because, well im passing in null to your method!

ALWAYS CHECK FOR NULL!

Im sure this is a minor lapse on your part, Dave, and im sure your code does not usually reflect this – if we had someone on my team, coding like this, I would either insist they change the way they code, or I would shift them off my team. Not checking for simple things, like null, is a major lapse in coding. You simply cannot guarentee that it will not be null, and wishfull thinking and happy thoughts arnt going to change it.


Copyright © 2007-2010 Muntedhar Alhakim. All rights reserved.