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 )
Serialize an untyped object 
Whoah! So what happens if you have an "Object", and you don't know exactly what it is, and you want to serialize it and make sure it retains its exact shape when it comes out the other end of the serialization process?

Simple - you store the "Type" of the object as well as the object itself in your serialization stream:

public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("ValueType", Value.GetType(), typeof(Type));
info.AddValue("Value", Value, Value.GetType());
}


To retrieve it later, follow this shorthand:


public MySerializedObjectConstructor(SerializationInfo info, StreamingContext ctxt)
{
Type t = (Type)info.GetValue("ValueType", typeof(Type));
Object Value = info.GetValue("Value", t);
}

  |  permalink  |  related link  |   ( 2.9 / 230 )

Back Next