One More Theme

by Tommy 25. June 2009 19:00

Hey everyone. I converted a Drupal theme to BlogEngine and once again gave it a crazy name.

The layout is heavily inspired by the great work @ Steven Kuhn

Enjoy!

Mean Green Tea [Downloads: 2]

CodeBits: Object to SqlParameters Extension with Reflection

by Tommy 18. June 2009 19:00

Don't get me wrong, code generators are a good thing.  However sometimes they generate pages and pages of code that are really redundant thanks to reflection.

Especially for generating a DAL for you application.  Consider the following extension:

 

public static List<SqlParamter> BuildParameters(this object obj)
{
    List<SqlParameter> collection = new List<SqlParameter>();

    foreach (PropertyInfo propertyInfo in obj.GetType().GetProperties())
    {
        if (propertyInfo.GetValue(obj, null) = null)
        {
            collection.Add(new SqlParameter(propertyInfo.Name, DBNull.Value));
        }
        else
        {
            collection.Add(new SqlParameter(propertyInfo.Name, propertyInfo.GetValue(obj, null)));
        }
    }

    return collection;
}

This will take any object, and create a list of sql parameters from all properties. This might sound a little overboard, but consider the following function.

 

public static void ExecuteNonQuery(string query, string connection, List<SqlParameter> parameters)
{
    using (SqlConnection connection = new SqlConnection(connection))
    {
        using (SqlCommand command = new SqlCommand(query, connection))
        {
            command.CommandType = CommandType.Text;
            for (int i = 0; i < parameters.Count; i++)
            {
                command.Parameters.Add(parameters[i]);
            }

            command.Connection.Open();
            int rowsAffected = command.ExecuteNonQuery();
            command.Connection.Close();
        }
    }
}

Now I've wrapped the sqlcommand class into a static helper. What I have done is basically removed any need for generated classes (or manually coded... ugh!) functions to create parameters for a sqlcommand. It would probably work for any DBCommand. The point is and DAL generated code is pointless because we can boil it down to a few functions thanks to reflection. Then usage would be something as follows :

 

 
public function Main()
{
    // Read object from Database or create new object
    // Then manipulate said object
    // Update or Insert object into the Database
    
    DalWrapper.ExecuteNonQuery(queryUpdateOrInsert, connection, obj.BuildParameters());
}

Of course my query should really be a store procedure name. At least my BLL / DAL has been condensed by about 75% of what the auto-generated code was, and makes adding new tables and records a breeze since all I really have to do is strong type my object and define the stored procedures.

CodeBits: Export ICollection to Excel compatable format

by Tommy 16. June 2009 18:00

I've expanded the generic collection handler to include allowing export to excel without the need for the gridview hack.

 

Little background: The gridview hack is to save a collection to the page first in a gridview. From there it is pretty easy to post the data back to the page as a cvs file. However that requires the gridview to be loaded first with all the data and a second post to occur. It is also not user friendly to display the gridview on a page if it has more then 15 columns or so. This function will help avoid that. Just add it to any class that inherits the CollectionBase class.

 

Side Note: A full postback must occur. So if you're going to do the export from a button within an AJAX update panel, make sure the panel has the button set for a full postback.

      
    public void ExportToXls(string fileName)
    {
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.Clear();

        // Force as inline, otherwise browsers will think this is an .aspx file or not give it a filename
        HttpContext.Current.Response.AddHeader("content-disposition", "inline; filename=" + fileName);
        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
        HttpContext.Current.Response.Charset = string.Empty;

        HttpContext.Current.Response.Flush();

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                Table table = new Table();
                table.GridLines = GridLines.Both;

                TableRow header = new TableRow();

                // Use refection to give us a header row with the property names
                foreach (PropertyInfo propertyInfo in this[0].GetType().GetProperties())
                {
                    TableCell cell = new TableCell();
                    cell.Text = propertyInfo.Name;
                    header.Cells.Add(cell);                       
                }

                // Add some style to the header
                header.Style.Add(HtmlTextWriterStyle.BackgroundColor, "#008080");
                header.Style.Add(HtmlTextWriterStyle.Color, "#ffffff");
                header.Style.Add(HtmlTextWriterStyle.TextAlign, "center");

                table.Rows.Add(header);

                for (int i = 0; i < this.Count; i++)
                {
                    TableRow dataRow = new TableRow();

                    foreach (PropertyInfo propertyInfo in this[i].GetType().GetProperties())
                    {
                        TableCell cell = new TableCell();

                        // Error check incase the property is null
                        if (propertyInfo.GetValue(this[i], null) == null)
                        {
                            cell.Text = string.Empty;
                        }
                        else
                        {
                            cell.Text = propertyInfo.GetValue(this[i], null).ToString();
                        }

                        dataRow.Cells.Add(cell);
                       
                    }

                    table.Rows.Add(dataRow);
                }

                table.RenderControl(htw);
                HttpContext.Current.Response.Write(sw.ToString());
            }
        }

        HttpContext.Current.Response.End();
    }  

DataCollectionV2.cs [Downloads: 8]

Silly accessories

by Tommy 12. June 2009 20:00

I'm sure at some armory there is a fine print that accessories prevent status effects by already causing them, thus preventing someone from casting it on you.  I'm at a loss of words myself on a paper hat preventing status effects.  I'd at least put some flame stripes on there to increase my agility too.

 

On a rampage

by Tommy 11. June 2009 07:58

I felt like making another theme.  That seems to be all I do.  I need new name suggestions though.

BlackHeart [Downloads: 4]

Download counter

by Tommy 30. April 2009 18:00

I added an extension that I found that adds a download counter next to all links.  It's very nice.  It also provides much more detailed information from the Admin's side.  Here is the site I found it at.  I highly recommend it.

Link

I'm still alive!

by Tommy 22. April 2009 09:13

BlogEngine has been upgrade to 1.5.  I'm not really sure what that means, but no better time to post again.

Also, I've updated this theme for the 1.5 release. Not much to say about it, a little changes here and there.

Grey Shadow 1.2 [Downloads: 2]

Happy New Year

by Tommy 12. January 2009 19:43

Ok.  So it is a little late to wish everyone a happy new year.  That is because I switched hosting providers and the site was down the whole time.  I have returned though from my 2 week absence!  Uh, let the party begin?

Actually, I discovered a decent open source photobook application, so I'm going to start working on intergrating it with the site.  Stay tuned.

Windows XP Themes

by Tommy 14. October 2008 15:48

A long time ago I used to use StyleXP to give my desktop new themes.  It was cool, but buggy.  I started searching to find if Microsoft released more themes, and they did, but they were advertisements for something like Spiderman and changed all the icons.  I just wanted some new colors.  Well microsoft released a dark theme based off of the zune.  They also made one for Windows XP Media center edition and released it, but finding it online took some time, as it is not on their website anymore.  So here are the two themes.

ZuneDesktopTheme.msi (1.64 mb) [Downloads: 71]

RoyaleThemeForXP.exe (871.77 kb) [Downloads: 30]

Look out for King Slime

by Tommy 4. August 2008 16:47

Oh no it's a king slime!  I found this art work the other day.  I think it's pretty sweet.

 

About The Author

This is a blog about random things in my life.  Video games, anime, and lots of computer goodness.

Advertisement