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.

Monday, March 3, 2008

Post and receive XML to a URL

Here is a quick example of how to post and receive XML using HttpWebRequest:

//Some libraries that will be needed:
System.Net;
System.IO;
System.Xml;
System.Text;

public XmlDocument Send(string url, XmlDocument reqdoc)
{
//Setting up and posting request
  HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url);
  httpReq.Method = @"POST";
  httpReq.ContentType = @"text/xml";
  httpReq.Timeout = 600000; // 10 minutes (in milliseconds)
  httpReq.KeepAlive = false;

  Stream strm = httpReq.GetRequestStream();
  XmlDocument doc = new XmlDocument();
  doc.LoadXml("); //load xml
  doc.Save(strm); //strm will be the data to be sent
  strm.Close();

//Receiving result
  HttpWebResponse resp = (HttpWebResponse)httpReq.GetResponse();
  StreamReader srdr = new StreamReader(resp.GetResponseStream(),Encoding.GetEncoding("UTF-8"));
  string xmlstr = srdr.ReadToEnd();

  resp.Close();
  XmlDocument respdoc = new XmlDocument();
  try
  {
    StringBuilder sb = new StringBuilder(xmlstr);
    for(int c=0; c<sb.Length; c++)
    {
      if (sb[c]<'\x20')
      {
        sb[c]='\x20';
      }
    }

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

return respdoc;
}