Archive for December, 2016

Empty string returned from long Oracle text field

I was returning data from an Oracle procedure using an OracleDataAdapter. I noticed that one text field that can hold large values was returning an empty string. So in the following, the “description” value was empty, when I knew the “ParkDescription” field in the database had a value:

string description = ds.Tables[0].Rows[0]["ParkDescription"].ToString();

Running the query in Toad, I could see the data was there. To resolve this, I needed to set the InitialLONGFetchSize  propety of the data adapter’s SelectCommand object to -1:

OracleDataAdapter adp = new OracleDataAdapter(comm);
adp.SelectCommand.InitialLONGFetchSize = -1;
Advertisements

Leave a comment

Defining a method in a view

I know there will be much gnashing of teeth from MVC purists for even thinking about designing a view this way, but if you need to define a method inside a view, you can do it like I show below. In this instance, I am writing a method that will take a date, determine if it is null, and return either the short date or an empty string:

First, the method, defined at the top of the view:

@{
 var getDate = new Func<DateTime, string>((date) => date != DateTime.MinValue ? date.ToShortDateString() : String.Empty);
}

And then calling the method:

@foreach (Models.Coaster item in Model.Coasters) {
 <p>@getDate(item.Date)</p>
}

Leave a comment

Bootstrap modal popup window persists

I had a problem with Bootstrap modal popup window not going away properly. I was returning search results in an MVC application, and if there were multiple results returned, the popup would be displayed. The modal popup was standard code you see on the Bootstrap web site, and I had a jQuery method to get results from a Web API call and, after a successful result, would close the popup by programmatically clicking the modalClose button:

function getEmpInfo(id) {
    var url = 'http://localhost:12345/api/EmpController/';

    $.ajax({
        url: url + '?id=' + id,
        type: 'GET',
        dataType: 'json',
        cache: false,
        success: function (d) {
            populateGeneralInfo(d);
            $("#modalClose").click();
        }
    });
}

As I said, if the getEmpInfo once, it would work, but more than once and the page would basically freeze – the entire screen would disabled. The popup window itself would disappear, but the page stayed dark.

After using the IE developer tools, I noticed that there was a modal DIV tag that was remaining, and that was causing the problem:


To resolve this, I modified the success callback of the getEmpInfo JS method, and added code to remove that recalcitrant DIV:

        success: function (d) {
            populateGeneralInfo(d);
            $("#modalClose").click();
            $('div[class*="modal-backdrop"]').remove();
        }

Leave a comment

Showing page numbers when an ITextSharp table spans multiple pages

I’ve been using ITextSharp to create reports in PDF format in my MVC application. Normally, when I need to create page numbers, I use the PdfPageEventHelper to create a header row, including the date, title, and page number, like so:

        public partial class Header : PdfPageEventHelper {
            int counter = 1;
            private Font headerFont;

            public Header() { }

            public override void OnEndPage(PdfWriter writer, Document doc){
                PdfPTable headerTable = new PdfPTable(3);
                headerTable.WidthPercentage = 90.0f;
                headerTable.TotalWidth = doc.PageSize.Width - 10;
                headerTable.HorizontalAlignment = Element.ALIGN_LEFT;

                PdfPCell cell = new PdfPCell(new Phrase(DateTime.Now.ToShortDateString(), headerFont));
                cell.Border = 0;
                cell.HorizontalAlignment = Element.ALIGN_LEFT;
                headerTable.AddCell(cell);

                cell = new PdfPCell(new Phrase("SCOTT'S REPORT", headerFont));
                cell.Border = 0;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                headerTable.AddCell(cell);

                cell = new PdfPCell(new Phrase("Page: " + counter++, headerFont));
                cell.Border = 0;
                cell.HorizontalAlignment = Element.ALIGN_RIGHT;
                headerTable.AddCell(cell);

                headerTable.WriteSelectedRows(0, -10, 10, doc.PageSize.Height - 5, writer.DirectContent);
            }
        }

However, certain reports consisted of tables that would span multiple pages, and the event was firing. Instead, what I needed to do was use the IPdfPTableEventSplit  interface. So I first created a class that implemented the interface, then created a counter to keep track of the page. The final line then placed the page number in the top right corner:

        public class TableRowCounter : IPdfPTableEventSplit {
            private int pageCount = 0;

            public void SplitTable(PdfPTable table) {
            }

            public void TableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {
                var writer = canvases[PdfPTable.BASECANVAS].PdfWriter;

                BaseFont baseFont = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.CP1252, false);
                Font font = new Font(baseFont, 9);

                pageCount += 1;
                var text = "Page: " + pageCount.ToString();

                ColumnText.ShowTextAligned(writer.DirectContent, Element.ALIGN_LEFT, new Phrase(text, font), 775, 525, 0);
            }
        }

To fire off this event, I added my new class to the PdfPTable’s TableEvent event:

PdfPTable table = ReportTable.CreateTableHeader(endDate.Day);
table.TableEvent = new TableRowCounter();

Leave a comment