November 19, 2004

ASP.NET Export To Excel

I was ask about how to do an export to Excel from ASP.NET yesterday. Since I've done this a few times now, I thought I'd post the code. To use it though, read up on HttpHandlers...

using System;
using System.Web;
using System.Data;
using System.Data.OracleClient;
using System.Data.SqlClient;
using System.Text;
using System.Configuration;
using System.IO;
using System.Web.SessionState;

namespace DiamondB.TextApp
{
/// <summary>
/// Export a SQL query to excel from ASP.NET
/// </summary>
public class ExportToExcel: IHttpHandler, IRequiresSessionState
{
public ExportToExcel()
{
//
// TODO: Add constructor logic here
//
}

#region IHttpHandler Members

/// <summary>
/// This is the main entry point. You can read about this in other places
/// from more reliable sources than me. The context object contains
/// the session, user, and other objects you are used to.
/// </summary>
public void ProcessRequest(HttpContext context)
{
string sHtml = "";
try
{
string sSQL = CreateSQLQuery();
sHtml = RenderGrid(sSQL);
}
catch(Exception ex)
{
sHtml = "<body>There was a problem executing the query.<br>"+ex.Message+"</body>";
}

// return the results to the browser
context.Response.Clear();
context.Response.ContentType = "application/vnd.ms-excel";
context.Response.Charset = "";
context.Response.Write(sHtml);
context.Response.End();
}

/// <summary>
/// DESCRIPTION: This property defines whether another HTTP handler can
/// reuse this instance of the handler.
///
/// NOTE: false is always returned since this handler is synchronous
/// and is not pooled.
/// </summary>
public bool IsReusable
{
get { return false;}
}

#endregion
/// <summary>
/// I have specialized dynamic queries that I create on the fly. That is why I have
/// the query creation routine separate. For most cases this is probably not needed.
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
private string CreateSQLQuery()
{
string sQuerySQL = "SELECT * FROM SCHEMA.TABLE"
return sQuerySQL;
}

/// <summary>
/// This code takes the SQL query, executes it, pumps the data into a data grid,
/// grabs the HTML out of the grid, and returns. Simple, right!
/// Also left in this code is some of my query logging code. Every time the query
/// is run I log the query, how long it took to execute, and how many rows were returned.
/// But that is just me.
/// </summary>
private string RenderGrid(string sql)
{
// I just put this here so something is outputted.
string sHTML = "<html><body><pre>" + sql + "</pre></body></html>";

int iRows = -1;
double dSeconds = 0.0;
DateTime dtStart;
try
{
string sConn = ConfigurationSettings.AppSettings["OracleConnectionString"];
OracleConnection oConn = new OracleConnection(sConn);
OracleCommand oCmd = new OracleCommand(sql, oConn);
oConn.Open();
try
{
dtStart = DateTime.Now; // get the current time
// get the data
DataSet ds = new DataSet();
OracleDataAdapter da = new OracleDataAdapter(oCmd);
da.Fill(ds);
// check how long the query took
TimeSpan ts = DateTime.Now - dtStart;
dSeconds = ts.TotalSeconds;
// setup and load the grid.
System.Web.UI.WebControls.DataGrid oGrid = new
System.Web.UI.WebControls.DataGrid();
oGrid.HeaderStyle.BackColor = System.Drawing.Color.Silver;
oGrid.HeaderStyle.ForeColor = System.Drawing.Color.Black;
oGrid.DataSource = ds;
oGrid.DataBind();

// get the html from the grid
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHTMLWriter = new
System.Web.UI.HtmlTextWriter(oStringWriter);
oGrid.RenderControl(oHTMLWriter);
sHTML = oStringWriter.ToString();
oHTMLWriter.Close();
oStringWriter.Close();
// get the number of rows returned
iRows = ds.Tables[0].Rows.Count;
}
finally
{
oConn.Close();
}
}
catch(Exception ex)
{
sHTML += ex.Message;
}
return sHTML;
}
}
}

November 16, 2004

Got my HotMail space...

So I finally got my extra 250 meg for my hotmail account.

Now the question is: does it matter? I've already got a 1 gig gmail account. (and 6 invites to anyone who needs one).

Well, let the spam begin.

November 10, 2004

November 08, 2004

My living hell

Or one version of it....

This post is a little late in coming, I should have posted it about a week or two before Halloween.
What is the problem, my Christmas decorations are already up!!! And not that they are up recently, no this happened BEFORE Halloween (or Reformation Day for those inclined).

Thank the Lord I have a fake tree, but for the love of sanity, this is to much. This has to be the only time my house has beat the Mall for Christmas decorations.

This is the type of crap that happens when your wife gets pregnant. If any of you were wondering if that whole nesting thing really happens -- IT DOES!!! No amount of progressive women’s movement for the last 100 years can change thousands of years of God-wired instinct!

What is nesting, nesting is your wife/significant female other trying to get the house ready for baby, in any way, shape, or form. This includes Christmas decorations for a baby that isn't suppose to come until May!

I think I've lost some control again.

November 03, 2004

I'm back

I did the one thing that will give any good conservative heartburn: I went to Seattle right before an election. I was there to watch the Seattle Seahawks pound the Carolina Panthers into the turf (we amazing weather while there as well - sunny every day).

But while there for the weekend I saw every liberal propaganda sign there was on every corner. There were people lined up around the stadium for blocks with signs. Do note: I did see one "Veterans for Bush" supporter. We tried to give him a little support as we passed.

But all said, everything went fine. I can cant say we were accosted by anyone but panhandlers (we just don’t see those in Boise), and they seem to be rather non-partial. The weather was great, the game was great -- we had tickets right in front of the cheerleaders, 20 yard line, 6 rows up.

Now that was last weekend. This is Wednesday, Nov 3, 2004. One day after the election. Kerry just conceded the vote to Bush. Daschle is gone. Republicans hold majorities in all branches of government. Hopefully they can keep tabs on Bush's spending. Because now they have no-one else to blame.

October 15, 2004

C# TO GET EDIT&CONTINUE!!!!

Straight from the horses mouth....

C# is going to get Edit and Continue -- just like VB.

http://blogs.msdn.com/somasegar/archive/2004/10/15/242853.aspx

I know a lot (few) of people are going to grumble about this, but I do think this will be a HUGE productivity gain for developers.

UPDATE: more info has been posted at http://msdn.microsoft.com/vcsharp/default.aspx?pull=/library/en-us/dnvs05/html/edit_continue.asp

October 14, 2004

The new Must Have device

I'm shopping for a laptop, this is not what I had in mind, but it is gosh-darn cooler than hech.

Welcome the OQO
http://www.oqo.com/

It is a laptop crammed into a space slightly bigger than a palm pilot.
It runs WinXP (look for a linux hack later), has 1 gh Transmeta proc, 256 MB ram, 20 gig HD, USB port, and a docking station is available (to plug the thing into a real monitor, keyboard, and mouse -- if you have need of such things)
Comes with wireless networking and bluetooth as well.

Cost: $1,900. -- hey what did you expect.

Drawback: not enough room to fit a CD drive, and the battery only lasts 2.5 hours.

That said, I think this is the device that could be a very good PC for a lot of people. If they can get firewire and a longer battery life into it I can definatly see me buying one of these.

Here is another article on the OQO.

September 28, 2004

Flex your WIKI

Microsoft has released FlexWiki to open source, on SourceForge of all places. This is the software that handles the Wiki pages for Microsoft's Channel 9 site.

The license is very open, you can basically use it however you want.

Looks like the entire site is written in C#, but I'll be looking into how to use it later.

September 25, 2004

MSN add-in

I just saw this add-in for MSN Messenger
http://www.msgplus.net/

Why do I think this is the greatest thing: it allows you to change the names of your contacts.
Be-it, you could do this with Jabber for the past couple of years...but everyone I talk to is on MSN Messenger service.

Why do I want to do this: because I have 3 Jim's, 2 Chris's, an Annie (not her name), 4 email addresses, and a niece that thinks it is cute to create names that look like (|)gUrL's*JuSt*wAnT*tO*hAvE*fUn*(L)
That, for me, is short for "smack me please".

September 17, 2004

Hungarian Notation considered harmful

I went to a NetDUG meeting last night on ASP.NET Best Practices, given by Carl Prothman. See the write up here.

Overall the presentation was fine, he have a lot of good information, tips, techniques, and so on. The one part that I took issue with was where he talked about Hungarian notation. He stated that for C# programmers, Hungarian notation should not be used. His premise was:
1. C# is strongly typed already
2. Visual Studio environment gives you all of the information anyway. All you have to do is hover over the variable.
3. There was also the reason that if the variable type has to change, you then have to change the variable prefix all over the place.

Here are my argument against his.
  1. Delphi, C, C++, VB, Java, and many other languages are also strongly typed, but it is still considered best practice for those languages. I really like C#, but I dont believe it has done anything to make Hungarian Notation obsolete.

  2. Great, so now if you print out the code, how is Visual Studio going to help you. Also, why should I have to move my mouse just to figure out what type a variable is, what it's scope is when I can just look at it. If I have to take my hands off of the keyboard, you are wasting my time. Also: I'm sorry, but you should not, under any circumstances, assume that everyone reading your code will be using Visual Studio, or any other environment for that matter. With the onset of Mono, SharpDevelop, and the new Delphi environment (which supports C#) that is dangerous argument. Programmers are fickle and prone to change quickly, tomorrows Visual Studio.NET might be Emacs (or VI -- cough).
    Motive 2: If I'm having trouble with a procedure I've written, I'll bring in another developer to look at that procedure. I dont want him to have to inspect the entire class just to figure out where the variables are declared. Now you are wasting the time of two developers.

  3. Haven't any of you people heard of "Search and Replace"? It has been a standard feature for years and has become very useful to the modern programmer. Changing a variables type is not a small matter anyway. Any time you change a type you should go through every place where that variable is used and make sure that it is still going to work. Think of it this way, one of the tips that Carl gave was that when you check in your code to your source code control (Source Safe, Vault, CVS, etc), you should inspect EVERY line you changed. When you change a variable type, you have affectively changed every line that variable is used on. What better way to make sure you look over every line than to change the variable name.
Finally I'll give a few environment of my own, not related to his.
I work in a large enviroment with many developers. Typically, one developer will start a project, work on it for a while, then hand it off to another developer, who has to make more changes. That second developer will have code from many other developers in his hands at once and has to be able to read it all.

Now let's be frank. C# allows developers to write some pretty sloppy code. Variables can be declared anywhere, in any form. They might be prefixed with private or public, or they might not. You can declare a exception class as IgnoreMe instead of IgnoreMeException. Curly brackets can go anywhere. C# is not a structured language. It is up to the programmers to add structure, and frankly, not using Hungarian Notation is just adding one more inconsistency variable into the mix.

To sum up: Hungarian Notation makes code easier to read and understand for other developers, removes reliance on other development environments, and up to this point, every argument I've heard of for getting rid of it has been insubstantial and petty.

If anyone cares to prove me wrong, please do.