Object reference not set to an instance of an object

Ruslan's ASP .NET weblog

February 2008 - Posts

Sort a ListBox

I believe this method is available in .NET 3.5, but in earlier framework versions we have to implement this ourselves..

 

#region Helper functions

 

    /// <summary>

    /// Sorts the list box in descending order

    /// </summary>

    /// <param name="pList">the list to sort</param>

    /// <param name="pByValue">Sort the list by values or text</param>

private void sortListBox(ref ListBox pList, bool pByValue)

{

    SortedList lListItems = new SortedList();

 

    //add listbox items to SortedList

    foreach (ListItem lItem in pList.Items)

    {

        if (pByValue) lListItems.Add(lItem.Value, lItem);

        else lListItems.Add(lItem.Text, lItem);

    }

 

    //clear list box

    pList.Items.Clear();

 

    //add sorted items to listbox

    for (int i = 0; i < lListItems.Count; i++)

    {

        pList.Items.Add((ListItem)lListItems[lListItems.GetKey(i)]);

    }

}

 

    #endregion

}

CSV to TextReader

/// <summary>

/// Take a file path and return a TextReader

/// </summary>

/// <param name="file_path"></param>

/// <returns></returns>

private TextReader OpenFile (string file_path)

{

    try

    {

        // Read the CSV file in to a TextReader

        TextReader _rdr = File.OpenText(file_path);

        // Set file attributes

        File.SetAttributes(file_path, FileAttributes.Normal);

 

        return _rdr;

    }

    catch (Exception)

    {

        throw new Exception(String.Format(

                "Error trying to open file {0}. Check file exists and is accessible<br /><br />" +

                "Possible Issues:<br/><br />Files cannot be accessed via mapped network drives" +

                "<br />The file is open<br />The file has been viewed and then saved in Excel",file_path));

    }

}

AJAX UpdatePanel triggers, Conditional update mode

First of all I'd like to thank kowalskec for pointing me out the CopySourceasHTML VS add-in that will help me copy the code snippets from Visual Studio here without losing any text formatting.

Cheers buddy! =)

 

And now the task:

  • The BugsGridView GridView control and BugsListTimeLabel Label in the Default.aspx page are encapsulated inside an UpdatePanel with an ID of BugsListUpdatePanel. The UpdatePanel does not update when other UpdatePanels on the page generate postbacks.
  • The ActivityLabel Label control in the Default.aspx page is encapsulated inside an UpdatePanel with an ID of ActivityUpdatePanel. The UpdatePanel does not update when other UpdatePanels on the page generate postbacks. This UpdatePanel does not contain any other controls.
  • The ActivityUpdatePanel UpdatePanel control in the Default.aspx page defines a trigger which causes the UpdatePanel to asynchronously refresh when the Click event of the ClearButton Button is raised.
  • The BugsGridView_RowUpdated event handler in the _Default partial class refreshes the ActivityUpdatePanel UpdatePanel if changes have been made to the BugsGridView GridView control.

This will help me remember how to make the UpdatePanel do a postback by clicking a button outside of the panel, as well as call the UpdatePanel Update() method from code behind.

<!--

Done:

The Default.aspx page contains a ScriptManager control with an ID of

BugsPageScriptManager.

-->

<asp:ScriptManager ID="BugsPageScriptManager" runat="server" EnablePartialRendering="true" />

<!--

Done:

The BugsGridView GridView control and BugsListTimeLabel Label in the

Default.aspx page are encapsulated inside an UpdatePanel with an ID of

BugsListUpdatePanel. The UpdatePanel does not update when other

UpdatePanels on the page generate postbacks.

-->

<asp:UpdatePanel ID="BugsListUpdatePanel" runat="server" UpdateMode="Conditional">

<ContentTemplate>

Bugs as of:

<asp:Label ID="BugsListTimeLabel" runat="server" Text="" />

<asp:GridView ID="BugsGridView" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True"

DataSourceID="BugsDataSource" OnRowUpdated="BugsGridView_RowUpdated">

<Columns>...</Columns>

</asp:GridView>

</ContentTemplate>

</asp:UpdatePanel>

<!--

Done:

The ActivityLabel Label control in the Default.aspx page is encapsulated

inside an UpdatePanel with an ID of ActivityUpdatePanel. The UpdatePanel

does not update when other UpdatePanels on the page generate postbacks.

This UpdatePanel does not contain any other controls.

-->

<!--

Done:

The ActivityUpdatePanel UpdatePanel control in the Default.aspx page

defines a trigger which causes the UpdatePanel to asynchronously refresh

when the Click event of the ClearButton Button is raised.

-->

<asp:UpdatePanel ID="ActivityUpdatePanel" runat="server" UpdateMode="Conditional">

<ContentTemplate>

<asp:Label ID="ActivityLabel" runat="server" />

</ContentTemplate>

<Triggers>

<asp:AsyncPostBackTrigger ControlID="ClearButton" EventName="Click" />

</Triggers>

</asp:UpdatePanel>

 

<asp:Button ID="ClearButton" runat="server" Text="Clear"

OnClick="ClearButton_Click" />

 

    protected void BugsGridView_RowUpdated(object sender, GridViewUpdatedEventArgs e)

    {

        if (CheckForChanges(e, "Description", "Status", "AssignedTo"))

        {

            // Done:

            // The BugsGridView_RowUpdated event handler in the _Default partial class

            //  refreshes the ActivityUpdatePanel UpdatePanel if changes have been made

            //  to the BugsGridView GridView control.

 

            ActivityUpdatePanel.Update();

        }

    }

Add new Blog Post WYSIWYG horrors

First of all, I am not even talking about FireFox or Opera, it's  IE6 what I try to use to make a new post. I copy-paste the code from Visual Studion or even do it through Word, but there's no way I can get the same look of code snippets:

 This is copied straight from VS:

/// <summary>

/// Initialises webpage.

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

protected void Page_Load(object sender, System.EventArgs e)

{

...

{

}

else

{

...

}

}

 

And this - VS -> Word -> Here:

            /// <summary>            /// Initialises webpage.            /// </summary>            /// <param name="sender"></param>            /// <param name="e"></param>            protected void Page_Load(object sender, System.EventArgs e)            {            DateTime theDate = DateTime.Parse("1 January 2007");                  if( !IsPostBack )                  {      }                  else                  {                                          }

            }

The 1st is too much spacing between lines, 2nd looks OK even when I do a Preview here, I do get what I see, but, once I post it, it would wrap everything horribly, so in fact What You See Is Not What You Get (WYSINWYG)

 =(

Posted: Feb 06 2008, 10:51 AM by funky_rus | with 44 comment(s)
Filed under:
More Posts