December 16, 2004

Getting web page text (winforms)

Ever try to call a web page from client site .NET code? Most often this is doen through web services, but occationally you have information that you want to retrieve that does not exists in a web service -- only in the web page. So from .NET, how do you get it?

Like this:
private string RetrieveWebPage(string url)
{
HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create(url);
// if you are dealing with NTLM, you will need this line
oRequest.Credentials = CredentialCache.DefaultCredentials;


HttpWebResponse oResponse = (HttpWebResponse)oRequest.GetResponse();

Stream oStream = oResponse.GetResponseStream();

// read the stream in the encoded "utf-8" format
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader oReader = new StreamReader(oStream);
string sPage = "";
while (oReader.Peek() > -1)
{
sPage += oReader.ReadLine() + "\n";
}

return sPage;
}

December 07, 2004

New Online Book: How a computer works

Beatiful book! Lots of pictures, but a little out of date (doesn't cover 64 bit computing), but written in such a way that your mother will not be turned off by it.

http://davidguy.brinkster.net/computer/

Have fun!

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.