Bug in OracleDataReader causing InvalidCastException?

Just had a weird error where loading a small dataset from an Oracle database was sometimes causing an invalid cast exception “Specified cast is not valid” on the dataset Fill method.

There was nothing wrong with the execution of the procedure, and the exception was happening within the oracle ADO classes. Specifically:

Stack: at Oracle.ManagedDataAccess.Client.OracleDataReader.GetDecimal(Int32 i)
at Oracle.ManagedDataAccess.Client.OracleDataReader.GetValue(Int32 i)
at Oracle.ManagedDataAccess.Client.OracleDataReader.GetValues(Object[] values)

I tried changing it to use a datareader and as you can see, I can read the value with GetFloat but not with GetDecimal. The value is also not some out of control decimal value with 38 places after the decimal point. And, you can see that it fully believes that the datatype is Decimal.

We found through trial and error that if we truncate the data in Oracle to only 2 decimal places that this stops the errors, but we are not sure why we are getting these results.

I thought I would post in case others came across this.

2015-07-30 10_27_45-Clipboard

Using the Telerik findControl client side method

We recently ran into an issue where we needed to get a reference to a telerik control on the client side but that control was going to be inside a grid or repeater so that there would be many copies all with unique generated IDs. It

looked like maybe the function $telerik.findControl could work for us, but there was limited documentation and I couldn’t find a single example online of someone successfully using it. Pretty much every post I cam across had the person being routed to some other more convoluted work around rather than addressing why their findControl call wasn’t working. So after getting it working I figured I’d give a quick explaination.

The method is:

var telerikControl = $telerik.findControl( domElementOfSomeParent , controlIdFromServerSide );

So just to be very clear, domElementOfSomeParent is an HTML DOM object that you can get from javascript or jquery or whatever.  It defines the scope for the search for the control.  Only elements that are ancestor (children, grand children etc) of domElementOfSomeParent will be considered.  And controlIdFromServerSide is a string that is the same as the ID you are specifying on the server, even if that ID is for a control that is going to be rendered multiple times because it’s in some gridview or repeater.

What you’ll get back is the telerik client object for your control.

Responsive Tables

Here is an interesting way I came across online of someone using responsive CSS to deal with tables.  Once the window width gets below 700px it switches from a table to more of a list of tables where each table becomes a row in the previous table. 

http://codepen.io/anon/pen/MwWwbY

It’s pulled off with this CSS (especially the ::last-child and ::before lines:

@media (max-width: 700px) {
table {
display: block;
width: 500px;
margin: 0 auto;
}
thead {
display: none;
}
tbody {
display: block;
}
tbody tr {
display: block;
border: 1px lightgray solid;
border-radius: 4px;
margin-bottom: 5px;
}
tbody tr td {
display: block;
overflow: hidden;
height: 1.2em;
border-bottom: 1px lightgray solid;
}
tbody tr td::last-child {
border: 0;
}
tbody tr td::before {
display: inline-block;
content: attr(title);
width: 25%;
font-weight: 600;
text-align: right;
border-right: 1px lightgray solid;
padding-right: 10px;
margin-right: 10px;
}
}

This:

image

changes into this:

image

Easy way to debug JSON being passed to ASP.NET MVC Action

Just a quick tip.  If you are ever debugging an MVC Action where you are passing JSON directly to the server (or for that matter where you are debugging anything complicated enough that you care about the raw data being passed to your method) I found this to be helpful.

Run this code in the debugger (or you can just paste it into a Watch item)

Request.InputStream.Position = 0

And then you can add this as a watch item

new System.IO.StreamReader(Request.InputStream).ReadToEnd()

This will write out the entire raw input data, which is very helpful if you want to see the exact JSON being passed to your method.

Printing Directly From ASP.NET

This is clearly something you wouldn’t often do, but I have a project where the server is going to be a local machine in a remote and temporary jobsite location and the client needs the server to print directly to a special label printer.

Here is some code that accomplishes this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Drawing.Printing;
using System.Drawing;

namespace TestingPrinting.Controllers
{
public class HomeController : Controller
{
public ActionResult Print()
{
PrintDocument pd = new PrintDocument();
pd.DefaultPageSettings.Margins.Left = 30;
pd.DefaultPageSettings.Margins.Top = 10;
pd.DefaultPageSettings.Margins.Right = 10;
pd.DefaultPageSettings.Margins.Bottom = 10;
pd.OriginAtMargins = true;
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
// Set the printer name.
//pd.PrinterSettings.PrinterName = "\\NS5\hpoffice

pd.PrinterSettings.PrinterName = "HP LaserJet 200 color M251 PCL6 Class Driver";
pd.Print();

return View();
}

void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
Font printFont = new Font("3 of 9 Barcode", 18);
Font printFont1 = new Font("Times New Roman", 11, FontStyle.Bold);
SolidBrush br = new SolidBrush(Color.Black);
ev.Graphics.DrawString("hello", printFont, br, 10, 65);
ev.Graphics.DrawString("world", printFont1, br, 10, 85);
}
}
}