Monday, June 30, 2008

Retrieving a file that is stored in a database as an image.

1. Create a "File Server" as an HttpHandler
-Within the web.config, set up a page (filemanager.aspx below) that will handle http requests.

<system.web>
  <httpHandlers>
    <add verb="*" path="filemanager.aspx" type="Project.FileManagerHttpHandler, DLLName"/>
  </httpHandlers>
</system.web>

2. Within the html page, create a hyperlink to the http handler
http post to server: <a href="http://url/fileserver.aspx?uniqueIdentifier">click here</a>

3. Create the class that will handle the http request

public class FileServerHttpHandler : IHttpHandler, IRequiresSessionState
{
  // Override the ProcessRequest method.
  public void ProcessRequest(HttpContext context)
  {
    try
    {
      Guid token = new Guid(context.Request.QueryString.ToString());

      BinaryWriter bw;
      context.Response.Clear();
      using (SqlDataReader dr = SqlHelper.ExecuteReader(DBHelper.ConnectionString, "FileGetByFileToken", token))
        {
        if (dr != null)
        {
          if (dr.Read())
          {
context.Response.ContentType = dr["filetype"].ToString();
            byte[] o = (byte[]) dr["filedata"];
              bw = new BinaryWriter(context.Response.OutputStream);
              bw.Write(o);
            }
            dr.Close();
          }
        }
      }
    catch (Exception)
    {
    }
  }

  // Override the IsReusable property.
  public bool IsReusable
  {
    get { return true; }
  }

Saturday, June 28, 2008

Howard Stern on the iPhone

Thanks to the work of Millard Software (www.millardsoftware.com), iPhone owners can now enjoy Howard Stern and the rest of Sirius Satellite Radio through their iPhone!

This is how to set it up:
1. You need to have a jailbroken iPhone, this can be achieved by downloading and installing ZiPhone here.

2. Execute and install the ZiPhone jailbreak onto your iPhone

3. Add the following repository to your Installer:
http://www.millardsoftware.com/files/downloads/usirius/iphone/iphone.xml
more info: 
a. click the "installer" which will be on your homescreen
b. click on "Sources" form bottom menu
c. click on "Edit" which will be on the top right of screen
d. click on "Add" which will be on the top left of screen
e. type in the above URL


4. Select uSirius - iPhone Edition in the Multimedia section.

If you have a Sirius subscription, you enter your credentials and will have Howard Stern and Sirius Radio on your iPhone. If you don't have a subscription yet, you can receive a free three day trial by signing up at Sirius

Monday, April 28, 2008

My Morning Jacket and The Black Keys at Red Rocks Amphitheatre

August 21st 2008, The Black Keys will be opening for My Morning Jacket at Red Rocks Amphitheatre and I have tickets!! It's the only show that the two bands will be playing together on their separate tours. I've never been to Denver, so this will be an all around great trip. Red Rocks, a Hard Rock Cafe I don't have a shot glass from, downtown Denver, and some of the best music around. NICE!!

Post Http request in code behind example

public XmlDocument Send(string url, XmlDocument reqdoc)
{
//Start by creating the request using given URL
  HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url);
  httpReq.Method = @"POST";
  httpReq.ContentType = @"text/xml"; //Using XML
  httpReq.Timeout = 600000;//10 minutes (in milliseconds)
  httpReq.KeepAlive = false;

  Stream strm = httpReq.GetRequestStream();
  reqdoc.Save(strm);
  strm.Close();
//Create HttpWebRewponse to handle the result
  HttpWebResponse resp = (HttpWebResponse)httpReq.GetResponse();

  StreamReader srdr = new StreamReader(resp.GetResponseStream(),Encoding.GetEncoding("UTF-8"));
  string xmlstr = srdr.ReadToEnd();

  resp.Close();

  respdoc = new XmlDocument();

  try
  {
    StringBuilder sb = new StringBuilder(xmlstr);
//Remove undefined chars
    for(int c=0; c<sb.Length; c++)
    {
      if (sb[c]<'\x20')
      {
        sb[c]='\x20';
      }
    }

    respdoc.LoadXml(sb.ToString());

    respXmlStr = respdoc.OuterXml;
  }
  catch(Exception e)
  {
    string msg = "Invalid XML response returned: " + e.Message;
    LogExceptionData(msg,xmlstr);
    throw new Exception(msg,e);
    }
      return respdoc;
    }
}

Friday, March 7, 2008

Loop through SQL table

If you have a SQL result set that you would like to loop through, here is how you'd do it.


--Begin with defining the dataset you want to work with.
declare @res table (ID int, orderNum int)
insert into @res
select ID, orderNum
from A_Table a
inner join B_Table b on b.ID=a.ID
order by orderNum

declare @orderNum int

--Create the cursor that will cycle through the result set.
declare keys cursor for
select orderNum
from @res

open keys
fetch keys into @orderNum
while (@@fetch_status = 0)
begin
    exec orderDelete @orderNum
    fetch keys into @orderNum
end
close keys
deallocate keys

...And there you have it.

Thursday, March 6, 2008

Javascript pop-up errors from asp.net code behind

This is how to generate client side pop-up errors after processing code behind:
It starts with the StringCollection:
private StringCollection clientMessages;

while processing in code behind, add any errors to clientMessages:
if(Error)
clientMessages.Add("you can't do that");

Now in the OnPreRender event...
protected override void OnPreRender(EventArgs e)
{

// Display client messages to browser if any.
if ((clientMessages != null) && (clientMessages.Count > 0))
{
  StringBuilder sb = new StringBuilder(123);
  StringBuilder msg = new StringBuilder(123);
  sb.Append("<script language=\"javascript\" for=\"window\"   event=\"onload\">\n");
  sb.Append("function __ShowClientMessages(){\n");
  sb.Append("alert(\"");
  for (int i=0, j=clientMessages.Count; i<j; i++)
  {
    msg.Length = 0;
    msg.Append(clientMessages[i]);
    msg.Replace("\n","\\n").Replace("\"","\\\"");
    msg.Append("\\n");
    sb.Append(msg.ToString());
    //sb.Append(clientMessages[i].Replace("\n","\\n")).Append("\\n");
  }
  sb.Append("\");\n");
  sb.Append("}\n");
  sb.Append("window.attachEvent(\"onload\", __ShowClientMessages);");
  sb.Append("</script>");
  this.RegisterStartupScript("ClientMessages", sb.ToString());
  sb.Length = 0;
}

And here is how to register the client side script
protected void RegisterStartupScript(string script)
{
  this.RegisterStartupScript(Guid.NewGuid().ToString(),
  "<script language=\"javascript\">\n"
  + script
  +"</script>");
}

And there you have it, javascript pop-up errors from code behind.

Tuesday, March 4, 2008

Bar Rock The Vote

Arcade Fire came to the Beachland Ballroom in Cleveland, OH in support of Barack Obama. They played two free shows to sold out crowds, and here is an image of the most intense musician I've ever had the opportunity to see. The guy was like a bear.