Tuesday, October 21, 2014

Catch WebException from HttpWebRequest POST error response.

A WebException will be thrown if a non 200 status code is returned from the server.  To check the message returned, catch it and read the response.


try
{ 
  byte[] buf = Encoding.UTF8.GetBytes(payload);
  httpReq.ContentLength = buf.Length;
  httpReq.GetRequestStream().Write(buf, 0, buf.Length);
 
  HttpWebResponse resp = (HttpWebResponse)httpReq.GetResponse();

   if (resp.StatusCode != HttpStatusCode.NoContent && resp.StatusCode != HttpStatusCode.NotFound)
  {   
    return resp.GetResponseStream(); //Successful response
  }
}

catch (WebException wex)
{ 
  using (var stream = ex.Response.GetResponseStream())

  using (var reader = new StreamReader(stream))
  {   

    Console.WriteLine(reader.ReadToEnd()); //Message returned from client
  }

}
catch (Exception ex)
{
    // Something more serious happened
    // like for example you don't have network access
    // we cannot talk about a server exception here as
    // the server probably was never reached

}
 

Friday, October 17, 2014

Allow HTTP GET and POST of data to an .aspx page.

In Page_Load, use Request directly to allow GET and POST of data from client.

Request.QueryString[] will return value from the GET request of query string. 
i.e., http://[url]?data=somedata
Request.QueryString["data"] //will return "somedata"

Request.Form[] will return value of POST request.
i.e.,

<form action="http://[url]/Default.aspx" method="post">
    <input name="data" type="text" value="somedata" id="data" />
    <input type="submit" name="Button1" value="Button" id="Button1" />
</form>
Request.Form["data"] //will return "somedata"

Request[] will look through all the collections below to find a match:
HttpRequest.QueryString
HttpRequest.Form
HttpRequest.Cookies
HttpRequest.ServerVariables

i.e., if Request["data"] is used, the page will accept GET and POST request without specifying which specific method the page will allow.

Wednesday, October 1, 2014

How to fix kubuntu that halts while booting on battery

To fix a machine that freezes during boot when its on battery, follow this:

1.) Edit grub and reboot the machine
     a.) sudo nano /etc/default/grub
     b.) add nolapic between the quotes on the line GRUB_CMDLINE_LINUX=""
     c.) sudo update-grub


The test temporarily, enter into grub during boot up:
1.) Hold left Shift button during boot
2.) Press the 'e' button
3.) Edit the Linux line to include the option acpi=no
4.) Optionally, remove the "quite splash" option from the Linux line to see ubuntu boot up *
5.) Hit Ctrl+x to reboot

*This will also help debug the problem if these instructions don't fix the problem.

Wednesday, July 23, 2014

SQL selecting varchar xml as nodes with namespace to produce multi-row results

Items covered below:

1.) Selecting a VARCHAR column as XML
2.) Selecting XML nodes containing a namespace
3.) Cross Apply XML column to produce multiple rows of results

TABLE example:
TestTable(
    Id INT,
    Data VARCHAR)

Data Column example:

<Props xmlns="http://tempuri.org/Test.xsd"&gt;<Type><Fun>a</Fun><Sup>1</Sup></Type><Type><Fun>b</Fun><Sup>2</Sup></Type></Props>

SQL example:
;WITH XMLNAMESPACES (N'http://tempuri.org/Test.xsd' AS NS)

SELECT
Id,

X.S.value('(NS:Fun)[1]', 'VARCHAR(8)') AS 'Fun',

X.S.value('(NS:Sup)[1]', 'VARCHAR(8)') AS 'Sup'

FROM TestTable

CROSS APPLY (SELECT CAST(Data AS XML)) AS [XML](Data)

CROSS APPLY [XML].Data.nodes('/NS:Props/NS:Type') AS X(S)

Tuesday, June 10, 2014

SQL query column with XML stored as VARCHAR

XML stored as VARCHAR can be queried by CASTing


--Example XML
Notification.EmailRequest...

--Query
SELECT a.Id, m.Message, a.TransmissionDtTm

FROM Message m

INNER JOIN MessageArchive a ON a.messageid=m.messageid

WHERE a.TransmissionError = 0

AND CAST(m.Properties AS xml).value('(/Props/Type)[1]', 'VARCHAR(128)') LIKE '%Email%'

Thursday, May 22, 2014

Use of Javascript to allow RadioButton grouping to work in Repeater


1.) JavaScript
<script language="javascript" type="text/javascript">
   function RadioSelectionChanged(groupName, current) {
     var group = document.getElementsByName(groupName);

     for (i = 0; i < group.length; i++) {
        //.NET uses a span tag to define the control "name", so the first control in the span control will be the radiobutton        if(group[i].childNodes.length > 0 && group[i].childNodes[0].type == 'radio')
         group[i].childNodes[0].checked = false;
    }     current.checked = true;
  }</script>

2.) Control
<asp:Repeater ID="Rptr" runat="server">
  <ItemTemplate>
    <asp:RadioButton ID="Rb" runat="server" onclick="RadioSelectionChanged('GroupName', this);" name="GroupName"  />
   </ItemTemplate>
</asp:Repeater>

Wednesday, May 21, 2014

Convert class to DataRow

To convert a class instance to a DataRow


public DataRow ConvertClassToDataRow(ClassName theClass)
{ 
  DataTable table = new DataTable();
  DataRow row = table.NewRow();
  PropertyInfo[] properties = theClass.GetType().GetProperties();

  foreach (PropertyInfo property in properties)
  {   
    DataColumn col = new DataColumn(property.Name, property.PropertyType);
    table.Columns.Add(col);    row[property.Name] = property.GetValue(theClass, null);
  }
       
 
  return row;
}