Create A "Default" Object using DefaultValueAttribute 
So I have an object with a bunch of properties. I'm using the C# / .Net Property Grid to work with the object. But what I'd really like to do is create an object with all the necessary default properties so that it can start from a normal, consistent state.

For example, here's how I list a property:

[DescriptionAttribute("My description text goes here"),
DefaultValue(true), XmlElement("UseDateRanges", typeof(bool)),
CategoryAttribute("Factor Settings")]
public bool UseDateRanges
{
get { return _UseDateRanges; }
set { _UseDateRanges = value; }
}


With all these attributes, the overall property shows up nicely in the PropertyGrid and serializes well to XML via the XMLSerializer. Pretty fun!

Now, when it comes time to make an object, I'd like to make a new object with all the default settings that I specified using DefaultValue(). Here's how I do it:

public static RevenueWindowSettings GetDefault()
{
RevenueWindowSettings rws = new RevenueWindowSettings();
Type t = typeof(RevenueWindowSettings);
PropertyInfo[] list = t.GetProperties();
for (int i = 0; i < list.Length; i++) {
AttributeCollection ac = TypeDescriptor.GetProperties(rws)[list[ i ].Name].Attributes;
DefaultValueAttribute def = (DefaultValueAttribute)ac[typeof(DefaultValueAttribute)];
if (def != null) {
list[ i ].SetValue(rws, def.Value, null);
}
}
return rws;
}


This function produces a new object, runs through all properties defined on the object, and for every one that had a default value set, it puts that value in place. Pretty nice!
  |  permalink  |  related link  |   ( 2.9 / 178 )
List All Worksheets in Excel 2007 Using Ole DB 
This is a rather useful solution to a rather complex and annoying problem. I'm writing a .NET app that opens up an excel file and loads in some data from it. However, I want to use the Excel 2007 Ole DB provider rather than excel itself (I don't want to launch the whole application). Since some excel files have multiple worksheets, or since some of them have renamed worksheets, this is how I figure out which worksheet to use.


OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties=\"Excel 12.0;HDR=YES\";");
conn.Open();
DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
string sheetname = dt.Rows[0].ItemArray[2].ToString();


The Row 0 and ItemArray 2 indices are magic numbers. If you want your code to be more robust, you should find the correct column in the datatable that has the columnname "TABLE_NAME" and use that. In this case, I use row 0 because I only want to pay attention to the first worksheet in the file.
  |  permalink  |  related link  |   ( 3 / 216 )
Fun with Subversion on Windows 
Hi there,

I recently had to deal with a Subversion repository that produced some strange errors. Here's what happened.

We run Subversion using mod_dav_svn on Apache on OpenSSL. The end result is that we connect through an encrypted environment and our data is safe. However, although our environment worked fine when I set it up, users started to tell me that they weren't able to commit any files. The error they received was this:

Error: MKACTIVITY of '/sourcecode/!svn/act/23ae8076-d94a-1d43-9824-5cfe874b60a5': Could not read status line: An existing connection was forcibly closed by the remote host.

What did this mean? I searched for MKACTIVITY and I got a bunch of different hints. Some hints said that the problem was related to case sensitivity (I always use lowercase - case sensitivity is a bug carefully preserved by Unix for historical reasons). Another hint said that there might be a proxy in between Subversion and the HTTPS server. Another hint said that there might be an authentication problem.

The real answer? I had left the Subversion 1.4.3 binaries in place and installed 1.4.5 elsewhere. When I removed the 1.4.3 binaries Apache refused to load. For some reason, Apache was linking to the wrong version!

Now that I understood the problem, I just moved the 1.4.5 binaries into the location where the old ones used to live, and everything works fine.

Let that be a lesson to you - don't upgrade carelessly!
  |  permalink  |  related link  |   ( 4.1 / 498 )
Add Microsoft Word Interop to Your Application 
So I build a lot of data import/export tools. I discovered that it's not as easy as I'd think to import text from a Microsoft Word document into my .NET application. I wasted a few hours hunting around for how to add the reference to the component in my app! Frankly, it's pretty frustrating! However, here are the steps to get it going.

1) Download the appropriate version of Microsoft Office "Primary Interop Assemblies".

Here's the links you need.

Office 2003 PIA

Office 2007 PIA

2) Add the reference to your application.

In Visual Studio:
Select Add Reference from the Project menu.
Select the COM tab of the Add Reference window and double-click the appropriate type library file listed.
For example, for Microsoft Word 2007, select "Microsoft Word 12.0 Object Library".

Select OK to end and add the reference.

That's it!
  |  permalink  |  related link  |   ( 4.1 / 522 )
Add a Javascript Include to a page for a custom control 
I wrote a custom control - non AJAX - recently. This control depends upon a script file. I want this custom control to automatically add the script to each webpage where I use the custom control.

Now, Microsoft's AJAX patterns use an implementation of IScriptControl to add javascript references to a page, but I didn't want to do that. So instead I wrote this tiny block of code in the control:

protected override void OnPreRender(EventArgs e)
{
ScriptManager sm = ScriptManager.GetCurrent(Page);
ScriptReference sr = new ScriptReference("myscript.js");
sm.Scripts.Add(sr);
}


This adds the reference dynamically at the correct point in the page's load process. Enjoy!
  |  permalink  |  related link  |   ( 3 / 265 )

Back Next