Category: Uncategorized

  • SharePoint “Service Unavailable”

    I had this problem recently. Turns out the problem on my environment was an expired password for the account under which the SharePoint Central Administration application pool was running (DOMAIN\WSSSERVICE). Policy removed, password updated, problem solved.

    There are lots of other solutions too…

  • Silverlight MVVM and Testing

    During my investigations into this crazy land I have found a few good articles (and many not so good…):

    http://www.cauldwell.net/patrick/blog/MVVMAndTestingSilverlight.aspx

    http://silverlight.net/blogs/justinangel/archive/2009/02/25/silverlight-unit-testing-rhinomocks-unity-and-resharper.aspx

    Both use MVVM and both use RhinoMocks, but in subtly different ways.

  • Restore Database 2005 User Logins

    When a database is restored from one server to another user logins are dropped. This script will help rectify that:

    SET NOCOUNT ON

    --SELECT 'EXEC sp_change_users_login ''Auto_Fix'', '''+ user_id + ''', NULL, '''+ user_id + ''''
    SELECT 'EXEC sp_change_users_login ''Auto_Fix'', '''+ user_id + ''', NULL, ''password'''
    FROM staff s
    JOIN sys.sysusers su
    ON su.name = s.user_id
    WHERE s.is_Deleted = 'N'

    This will generate some further SQL statements that need to be executed. The passwords are reset to the values in the final parameter (i.e. password):

    EXEC sp_change_users_login 'Auto_Fix', 'wm', NULL, 'password'
    EXEC sp_change_users_login 'Auto_Fix', 'TTestA', NULL, 'password'
    EXEC sp_change_users_login 'Auto_Fix', 'TTestB', NULL, 'password'
    EXEC sp_change_users_login 'Auto_Fix', 'TTestC', NULL, 'password'

    To transfer logins between servers: http://support.microsoft.com/kb/246133

  • HTML Table to ASP.NET Table

    To convert an HTML table (from a string) to an ASP.NET table:

    Start with an HTML table:

    <table style="width: 100%;">
    <tr>
    <td colspan="2">
    <fieldset title="Policy Information">
    <legend><b>Policy Information</b></legend>
    <table style="width: 100%;">
    <tr>
    <td width="140">Policy Number:</td>
    <td width="150">$$Policy Number$$</td>
    </tr>
    </table>
    </fieldset>
    </td>
    </tr>
    </table>
    

    Create a Helper class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Xml;
    using System.Web.UI.WebControls;
    using System.Web.UI;
    using System.Drawing;&amp;lt;/pre&amp;gt;
    namespace Whatever
     {
     public static class TableHelper
     {
     public static Table m_table = new Table();
    
    public static Table CreateASPTableFromXML(XmlNode node)
     {
     Table table;
     /// fetch the root table node, this will iteratively fetch the rest.
     table = GetTableFromNode(node);
    
    return table;
     }
    
    static Table GetTableFromNode(XmlNode node)
     {
     Table table = new Table();
     table.BorderStyle = BorderStyle.Solid;
     table.BorderColor = Color.Black;
     table.BorderWidth = Unit.Pixel(1);
    
    /// create the table
     if (node.Name.ToLower() != "table")
     {
     throw new Exception(string.Format("'table' element expected, {0} found.", node.Name));
     }
    
    /// apply the attributes
     ApplyAttributesToControl
    
    (node.Attributes, ref table);// From this table element find all the child rows
     foreach (XmlNode rowNode in node.ChildNodes)
     {
     table.Rows.Add(GetRowFromNode(rowNode));
     }
    
    //TableRow row = new TableRow();
     //TableCell cell = new TableCell() { Text = "here" };
     //row.Cells.Add(cell);
    
    //table.Rows.Add(row);
    
    return table;
     }
    
    static TableRow GetRowFromNode(XmlNode node)
     {
     /// create the tableRow
     TableRow tableRow = new TableRow();
    
    if (node.Name.ToLower() != "tr")
     {
     throw new Exception(string.Format("'tr' element expected, {0} found.", node.Name));
     }
    
    /// apply the attributes
     ApplyAttributesToControl(node.Attributes, ref tableRow);
    
    // From this table row element find all the child cells
     foreach (XmlNode cellNode in node.ChildNodes)
     {
     tableRow.Cells.Add(GetCellFromNode(cellNode));
     }
    
    return tableRow;
    
    }
    
    /// Process each cell and look for content.
     static TableCell GetCellFromNode(XmlNode node)
     {
     /// create the tableRow
     TableCell tableCell = new TableCell();
    
    if (node.Name.ToLower() != "td")
     {
     throw new Exception(string.Format("'td' element expected, {0} found.", node.Name));
     }
    
    /// apply the attributes
     ApplyAttributesToControl(node.Attributes, ref tableCell);
    
    /// From this table row element find all the child objects
     /// these might be tables, text or html objects
     if (node.HasChildNodes)
     {
    
    foreach (XmlNode childNode in node.ChildNodes)
     {
     /// this might be an HTML object or another table... or both... (gulp)
     if (childNode.NodeType == XmlNodeType.Element)
     {
     if (childNode.Name.ToLower() == "table")
     {
     tableCell.Controls.Add(GetTableFromNode(childNode));
     }
    
    if (childNode.Name.ToLower() == "fieldset")
     {
     tableCell.Controls.Add(GetPanelFromNode(childNode));
     }
     }
     else if (childNode.NodeType == XmlNodeType.Text)
     {
     tableCell.Text = childNode.InnerText;
     }
     }
     }
    
    return tableCell;
    
    }
    
    /// Process each cell and look for content.
     static Panel GetPanelFromNode(XmlNode node)
     {
     /// create the tableRow
     Panel panel = new Panel();
    
    if (node.Name.ToLower() != "fieldset")
     {
     throw new Exception(string.Format("'fieldset' element expected, {0} found.", node.Name));
     }
    
    /// apply the attributes
     ApplyAttributesToControl(node.Attributes, ref panel);
    
    /// From this panel element find all the child objects
     /// one of which will be a legend tag
     /// then it might be HTML elements, tables or text
     if (node.HasChildNodes)
     {
     /// the legend node will be applied as a panel GroupingText property
    
    foreach (XmlNode childNode in node.ChildNodes)
     {
     /// this might be an HTML object or another table... or both... (gulp)
     if (childNode.Name.ToLower() == "legend")
     {
     panel.GroupingText = childNode.InnerText;
     }
    
    if (childNode.Name.ToLower() == "table")
     {
     panel.Controls.Add(GetTableFromNode(childNode));
     }
     }
     }
     else if (node.InnerText != string.Empty)
     {
     panel.Controls.Add(new Literal() { Text = node.InnerXml});
     }
    
    return panel;
    
    }
    
    ///
    
     /// Add attributes to any web control
     ///
    
    /// Reference to the control to which the attributes are being added
     /// The attributes being added
     static void ApplyAttributesToControl( XmlAttributeCollection attribs, ref T ctrl)
     where T : WebControl
     {
     foreach (XmlAttribute attrib in attribs)
     {
     ctrl.Attributes.Add(attrib.Name, attrib.Value);
     }
     }
    
    }
     }
    
    

    Open the HTML file, and pass the root node to the helper class:

    Table table;
    
    /// check if a template exists for this request form...
     /// TODO: Update this to use the request form name, not the ID.
     string TemplatePath = Server.MapPath(string.Format("~/Private/RequestFormTemplates/{0}.xml", _selectedRequestId));
     if (File.Exists(TemplatePath))
     {
     System.IO.StreamReader myFile = new System.IO.StreamReader(TemplatePath);
     string myString = myFile.ReadToEnd();
     myFile.Close();
     myFile.Dispose();
    
    FileStream fs = new FileStream(TemplatePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
     XmlDocument xmldoc = new XmlDocument();
     xmldoc.Load(fs);
    
    table = TableHelper.CreateASPTableFromXML(xmldoc.DocumentElement);
    
    }
    

    The helper class needs to be fleshed out some more, to provide support for other elements and valid HTML, but it’s almost there.

    If there is an easier way, let me know!

  • Silverlight unit testing requirements

    When setting up a Silverlight Unit Testing class the following using statements and inheritance must be applied:

    These are required for the TestPanel and EnqueueX methods (particularly inheritance).

    For more on the Inner Class Pattern, take a look at the Type Mock training blog.

  • Asynchronous Lambda Method calls

    You won’t get any benefit using this code, but it makes for more manageable source. So from Pete Browns blog:

    All network calls in Silverlight are asynchronous. The proliferation of event handling functions this can cause just makes for really cluttered code. Using lambda expressions seems to make my code a bit cleaner. (You can also use regular anonymous delegates, but the lambdas involve less typing)

    public void LoadPolicyDetail()
    {
    IvcDataServiceClient client = new IvcDataServiceClient();

    client.GetPolicyDetailCompleted += (s, e) =>
    {
    if (e.Result != null)
    {
    _policyDetail = e.Result;

    if (PolicyDetailLoaded != null)
    PolicyDetailLoaded(this, new EventArgs());
    }
    else
    {
    if (PolicyDetailLoadError != null)
    PolicyDetailLoadError(this, new EventArgs());
    }
    };

    client.GetPolicyDetailAsync();
    }

    The event handler is right in-line with the loading function. The event args remain strongly typed, so you have access to everything you normally would with an additional function.

  • Persisted Properties

    Back in classic ASP.NET I’d persist data extract from a web service in base class property as follows:

    private string m_stringData;
    public string _stringData
    { get {
    if (m_stringData==null)
    {
    //fetch data from my web service
    m_stringData = ws.FetchData()
    }
    return m_stringData;
    }
    }

    This way I could always simple use _stringData and know that I’d always get the data I was after (sometimes I’d use Session state as a store instead of a private member variable).

    In Silverlight with a WCF this doesn’t work, because the WCF service has to be called asynchronously.

    So, how do I get round this?… There are a number of routes to take. One method involves wrapping the messages synchronously and can be found at codeplex

    Another alternative that is less verbose, and arguably easier, to follow is using a lambda expression as follows:

    private string m_stringData;
    public string _stringData
    {
    get
    { //if we don't have a list of departments, fetch from WCF
    if (m_stringData == null)
    {
    StringServiceClient client = new StringServiceClient();
    client.GetStringCompleted +=
    (sender, e) =>
    {
    m_stringData = e.Result;
    };
    client.GetStringAsync();
    }

    return m_stringData;
    }
    }

    A final solution was posted on this stackoverflow thread

  • Invalid or malformed application: Check manifest

    When changing the namespace of a SL application, the startup object information must also be changed