Working on a project to post some data to a sharepoint 2010 list via powershell thought I would share the code

$assembly = [Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")
$url="http://SERVER/_vti_bin/listdata.svc/LISTNAME"
$webclient = new-object System.Net.WebClient
$webclient.UseDefaultCredentials = $true
$webclient.Headers.Add("Accept", "application/json")
$webclient.Headers.Add("Content-Type", "application/json; charset=utf-8");
$stringToUpload="{`"Title`":`"test`"}"
$resultString=$webclient.UploadString($url,$stringToUpload)

Replace SERVER and LISTNAME with your info on line 2

Tags: , , | Categories:

So if you have ever programmed for longer than well about 5 seconds you have probably run into an error. So what to do with the errors? I found out about this fantastic little trick for error handling in VB.Net that helped me with a SaaS project I'm working on.

As most dynamic projects go I needed to pass information from a text box into SQL statement (Linq2SQL, stored proc). Again, as most dynamic projects go I knew that I would need to valid what went into the text box before using it. In this case I need to verify that

1) the text box actually had something in it, if not set it to zero,

 

if TbSomething.text = "" then TbSomething.text = 0

2) verify that what was in the textbox was a number and 3) that it was a positive number.

Try
	i = CInt(TbSomething.Text)
	If i < 0 Then Throw New Exception("Negative Number")
	Catch ex As InvalidCastException
		errormsg += "Must be a number<br/>"
	Catch ex As Exception When ex.Message = "Negative Number"
		errormsg += "Must be a positive number<br/><br/>"
End Try

master.errormsg = errormsg

The interesting part of this was the When clause. When using Catch and an Exception type is not enough or you are using a custom Exception you can use the When clause to additional conditions. Then When clause could execute a method or be a simple comparison as above. Another example was using it to allow the application to retry an operation and finally give up if a condition was true thus allowing the Catch When to fire.

Once the error(s) where caught I used the error display method I wrote about in this post

Tags: , , , , , , | Categories:

I often find myself needing to display some kind of message to the users of my application. Be it an error message or to notify the user an operation has completed. I hate rewriting code. So I wanted a way in which I could have a placeholder in a master page and be able to programmatically display error messages. Of course the other benefit of placing this in  a master page is being able to update the styles or controls in one place. I would hate to find myself needing to make a simple change to the class name of a div I’ve placed all over a huge project. Here’s my solution:

Place the following code in your master page: (note you will need jqueryui for this)

 

<div class="ui-widget" id="Div_Warning_Container" runat="server" visible="false" style="width: 600px;">
<div class="ui-state-highlight ui-corner-all" style="margin-
top: 20px; padding: 0pt 0.7em;">
<p><span class="ui-icon ui-icon-info" style="float:
left; margin-right: 0.3em;"></span>
<strong><asp:Literal ID="LitGlobalWarningMsgTitle"
runat="server" /></strong> <asp:Literal ID="LitGlobalWarningMsg" runat="server" /></p>
</div></div>



<div class="ui-widget" id="Div_Error_Container" runat="server" visible="false"
style="width: 600px;">
<div class="ui-state-error ui-corner-all" style="padding: 0 .7em;">
<p>
<span class="ui-icon ui-icon-alert" style="float: left; margin-
right: .3em;"></span>
<strong><asp:Literal ID="LitGlobalErrorMsgTitle" runat="server" /></
strong><br />
<asp:Literal ID="LitGlobalErrorMsg" runat="server" /></p>
</div>
</div>

 

You now have an nice little area for error messages and a separate one for general informational messages

In your master page code behind:

 

'be sure to set the title AFTER the message. the message property will automatically set a title
Public Property errormsg() As String
Get
Return LitGlobalErrorMsg.Text
End Get
Set(ByVal value As String)
errormsgtitle = "Error(s):"
LitGlobalErrorMsg.Text = value
Div_Error_Container.Visible = True
End Set
End Property
Public Property errormsgtitle() As String
Get
Return LitGlobalErrorMsgTitle.Text
End Get
Set(ByVal value As String)
LitGlobalErrorMsgTitle.Text = value
End Set
End Property
'be sure to set the title AFTER the message. the message property will automatically set a title
Public Property warningmsg() As String
Get
Return LitGlobalWarningMsg.Text
End Get
Set(ByVal value As String)
warningmsgtitle = "Warning:"
LitGlobalWarningMsg.Text = value
Div_Warning_Container.Visible = True
End Set
End Property
Public Property warningmsgtitle() As String
Get
Return LitGlobalWarningMsgTitle.Text
End Get
Set(ByVal value As String)
LitGlobalWarningMsgTitle.Text = value
End Set
End Property

 

From your child (or is it slave?) page you can then do:

master.errormsg = "Text"
master.errortitle = "Title"

Be sure to set this:

<%@ MasterType VirtualPath="Site.master" %>

and set autoeventwireup to true

Ta Da!

Tags: | Categories:

So I've got a hosting plan at Godaddy (i.e. you are here) with about 7 sites on the hosting package. Problem is that my development is based on .Net 4 and blogengine is running (as of this post) .Net 3.5. The blog engine will run like this without any problems (at least in my experience), the problem comes when attempting to make new posts. I kept getting a 500 error message.

Solution:

Either allow VS 2010 to update your web.config file, which would involve downloading blogengine locally and setting it up in VS or make these changes to your web.config file:

Line 34-43:

From:
<compilation debug="false">
            <assemblies>
                <add assembly="System.Management, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
                <add assembly="System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
                <add assembly="System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
                <add assembly="System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
                <add assembly="System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
            </assemblies>
           
To:
<compilation debug="false" targetFramework="4.0">
            <assemblies>
                <add assembly="System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
                <add assembly="System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
                <add assembly="System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
                <add assembly="System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
                <add assembly="System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                </assemblies>

Line 46:

From: <httpRuntime enableVersionHeader="false" useFullyQualifiedRedirectUrl="true" maxRequestLength="16384" executionTimeout="3600" requestLengthDiskThreshold="16384"/>
To: <httpRuntime enableVersionHeader="false" useFullyQualifiedRedirectUrl="true" maxRequestLength="16384" executionTimeout="3600" requestLengthDiskThreshold="16384" requestValidationMode="2.0"/>

Line 51:

From: <pages enableSessionState="false" enableViewStateMac="true" enableEventValidation="true">
To:<pages enableSessionState="false" enableViewStateMac="true" enableEventValidation="true" controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID">

 

If you decided to let VS update the web.config file you still MUST add requestValidationMode="2.0" to line 46. Hope it works for you. Happy posting!

Tags: , , , , | Categories:

Mostly blogging to myself here because I had to look this up again :) This is one of the ways to use CSS to center your website designs. I like this method better then the margin 0px auto method as that method has caused some other alignment issues where this method doesn’t. So here’s the code:

#wrapper { position: absolute; left: 50%; margin-left: -350px; width: 700px; }

Notice that is margin-left: -(width+borders+padding)/2.

The wrapper div should be placed outside of all other content.

 

Update:

 

@Kevin Gillen

suggests:


A more simple option would be something like this:

.wrapper
{
     width: 700px;
     margin-left: auto;
     margin-right: auto;
}

And, you'd specify your div as follow

<div class="wrapper"> ... content in here ... </div>

 

Tags: | Categories:

September 10 30

Facebooker's beware

jpeterson

This doesn't really have to do with programming but I want to share this anyway. I've seen lots of people "like" these pages on facebook that once you go to the fan page the page has instructions to "unlock" their "secret" content. Here's an example

 

 

 

 

 

 

 

 

 

 

 

 

 

(click to enlarge)
Things like this scream at me as insecure and/or scam-ish. Notice the "Terms of Use" at the bottom, safely tucked away below the large white space and the "call to action buttons"? After clicking on "Terms of Use" you get this: "While pressing one of the buttons you are allowing the system to publish on your profile. We have good intentions; if you think otherwise please let us know :) ." (bold added) This is one of the few pages I've seen that actually tell you what's going on. Still though what exactly do those buttons do? Why let's look at the source. Warning: a bit nerdy here :)

Code:

<iframe src="http://www.facebook.com/plugins/like.php?href=http://www.facebook.com/pages/5-things-all-girls-want-guys-to-do-on-a-first-date/134286739953058&amp;layout=button_count&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=21" style="border: medium none; overflow: hidden; width: 80px; height: 26px;" allowtransparency="true" id="lpcframe" name="lpcframe" scrolling="no" frameborder="0"></iframe>

<iframe style="width: 100%; height: 40px; border: medium none; opacity: 0;" src="http://www.facebook.com/sharer.php?u=http://www.facebook.com/pages/5-things-all-girls-want-guys-to-do-on-a-first-date/134286739953058" scrolling="no"></iframe>

<a href="http://truthsexposed.com/man/landing.php">Step 3</a>

What is all that? Both the first two buttons are link directly to the facebook share url. What that means is that when clicking on those buttons those pages are added to your "other likes" on your facebook profile and added to your wall (which causes all of your friends to do it too). This should concern you. Why? Because these things happen without you realizing it (most of the time). Unfortunately, you are giving your express permission to do this because it does require a response from you (i.e. clicking the button(s)). If that wasn't bad enough often when you click the final step to actually view the information there some survey or something that you must complete to see their magical content.

So what do you do if you really what to see the content? I stumbled across this website http://www.bypassfanpages.com/ (warning: there is some not so nice language and other things you may not wish to see/read on this site). They've got post from a lot of these pages that reveals the content directly.

In conclusion, you really should go look at your profile and review the applications you have given permissions to and the pages you have "liked".

To review the pages:

  1. go to facebook.. duh
  2. click edit my profile (beside your picture)
  3. go to likes and interest
  4. click show other pages (at the bottom)
  5. remove

To review applications:

  1. go to facebook
  2. click account at the top right
  3. click application settings
  4. change the "show" drop down list to each one, one at a time and carefully look at what applications you have enabled. There's so many! Do you really need 4 "pieces of flair" applications?
  5. remove
Tags: , , | Categories:

You never know who might be coming behind you after you've spent time coding a large (or small for that matter) application so be kind, follow good practices.

  • Either make your variables easy to figure out what they do or take the time to comment your functions and code.
  • Three "M's" of coding Modular, Modular, Modular!

    There have been countless times that I've been able to use the same block of code  (or slight variations as the project calls for) to do the same thing in an entirely different project. I hate coming behind someone else, looking at their code and wondering why they repeated something unnecessarily.
  • Folder Structure

    Keep neat folder structures. Keep your CSS under a “styles” or “css” folder. Images under “images”. Not doing this is almost as bad as not doing the above. I don’t want to sift through folders upon folders looking for that one last js file I needed.
  • Reinventing the wheel

    Yeah it’s nice that you wrote that really long fancy function to find the aspect ratio of an image but someone already did that. Don’t waste your time just to put your name on it. You’ve got better things to do. I’m all for taking something and adding function to what’s already there, but complex code may have already been provided by some really nice person on asp.net or stackoverflow.
  • Errors

    Make sure you have try catch blocks. Your clients (and possibly their clients) will at some point but something in that textbox that you weren’t planning for and you will wonder why on earth they tried that. On that note, validation user input. If you are going to reference their last name in some other page or portion of the application… you might want to check they entered it.

So those are a few that I could come up with. What do you think? (and be nice this is my first attempt at this :)

Tags: | Categories:

September 10 28

New blog

jpeterson

Watched a video by Scott Hanselman about every developer needing a blog, so here's mine. Hopefully I'll actually try :)

Tags: | Categories: