Posts

Donate

WPF Controls Not Rendering Properly Or Correctly On Windows 7 OS

The problem was that the windows 7 theme was changed to classic window . Turning it back to Windows 7 default view, solved it. Greg

Check If Controls On another WPF Window Have Been Rendered Using Multithreading

Given you have two WPF windows namely MainWindow and OtherWindow. You basically want to check if the controls in main window have successfully loaded using statements in OtherWindow. The solution is to use Application dispatcher object. if (Application.Current.Dispatcher.CheckAccess()) { while ( true ) { foreach (Window window in Application.Current.Windows) { if (window.Title.Equals( "Project Title" )) { if (window.FindName( "your_control" ) != null ) { //do your stuff here... } } } break ; } } else { Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(CheckMainWindow)); } Cheers!

Garbage Collection GC.Collect() Method In C# Slowing Web Crawlers Speed

In our web crawlers, we encountered slow crawling of items specially XML feeds. We checked our codes if there are lines generating bottlenecks. It turned out that we placed GC.Collect() in an inner loop that's processing item per item. The solution is to comment out GC.Collect() in the inner loop and transfer it to the outer loop statement. Here's the code: while (!EndOfPage) { do { if (date != String.empty) { //comment this statement, transfer outside inner loop... //GC.Collect(); //processing of items code statements... } } while (!(url.Equals(String.Empty))); GC.Collect(); PageNo++; if (PageNo > totalPage) { EndOfPage = true ; } } Credits to my fellow developer for the discovery. Greg

Regular Expression Match Capture Index Property Returns 0 Instead Of -1 In C#

Yesterday, while working on Regex Index property, I encountered a weird problem. Supposedly, string.indexOf(string variable) returns -1 value if there's no occurrence of such substring. However, using Regex.Match(source.ToLower(), @"regex pattern" ).Index returns 0 if instead of -1 if no such substring occurs. The solution was pointed out in stack overflow: Return index position of string after match found using Group.Success in which case returns a boolean value. So, here's the tip: if ((Regex.Matches(source.ToLower(), @"<ol[^>]*>" ).Count < 1) && (Regex.Matches(source.ToLower(), @"<ul[^>]*>" ).Count > 0)) { if (Regex.Match(source.ToLower(), @"<ul[^>]*>" , RegexOptions.IgnoreCase).Success) { // your if statements here.... :) } } Greg

Custom DatagridviewCheckboxColumn In Windows Forms

Image
Here's a simple class implementation of a DatagridviewCheckboxColumn. public class DatagridviewCustomCheckboxColumn : DataGridViewCheckBoxColumn { public DatagridviewCustomCheckboxColumn() { this .CellTemplate = new DatagridviewCheckboxCustomCell(); } } class DatagridviewCheckboxCustomCell : DataGridViewCheckBoxCell { public int row_index { get ; set ; } public int CheckboxHeight { get { //your_desired_checkbox_height is a variable //that contains the desired height of your checkbox //you may set or get the property value.. return your_desired_checkbox_height; } } public int CheckboxWidth { get { //your_desired_checkbox_width is a variable //that contains the desired width of your checkbox //you may set or get the property value.. return your_desired_che

How To Highlight GridView Row On Click In ASP.NET Web Forms

This is based from the article of Vincent Maverick Durano's blog on HIGHLIGHT GRIDVIEW ROW ON CLICK AND RETAIN SELECTED ROW ON POST BACK. I just added some statements on javascript to check if the selected row is less than total gridview row count and server side code for paging. JAVASCRIPT CODE: <script type= "text/javascript" > var prevRowIndex; function ChangeRowColor(row, rowIndex) { var parent = document.getElementById(row); var currentRowIndex = parseInt(rowIndex) + 1; //count number of gridview rows var rowscount = $( "#<%=grdCustomer.ClientID %> tr" ).length; if (prevRowIndex == currentRowIndex) { return ; } else if (prevRowIndex != null ) { parent.rows[prevRowIndex].style.backgroundColor = "#FFFFFF" ; } //check if current index is a number... if (IsNumeric(currentRowIndex)) { //check if row index

Get Absolute Server Path Of Resources In ASP.NET MVC

In a scenario where you want to load images or other resources, Url.Content() does not return the exact resource path like this (D:\Cart\CartProject\Images\Item4.jpg) . Hence, resulting into a null exception or empty resource. Instead of using Url.Content() or ResolveUrl(), i get the server path of the resource using HttpContext of the application. return new ImageResult(HttpContext.Server.MapPath( "~/Images/Item" + id + ".jpg" )); Note: Testing is done locally and not on a virtual directory... Source: Find Absolute Path the the App Data Folder from Controller Cheers!

Display Bitmap Image In ASP.NET MVC View From Database With Image Data Type

I encountered this weird problem rendering bitmap image from a database table that a friend of mine develops in his company. The task is to display the image on the mvc view. I thought it was a gif or jpeg or png format. But to my surprise as I rendered the binary data on webpage, the image was a bitmap type.. The code to show the bitmap image is as follows. That is to render the bitmap image into a jpeg format. C# Code: public ActionResult ShowCategories () { var model = from n in oContext.ApplianceCategories select n; return View (model); } public void ShowImage ( int id) { var image = ( from m in oContext.ApplianceCategories where m.CategoryID == id select m.Picture).FirstOrDefault(); TypeConverter tc = TypeDescriptor.GetConverter( typeof (Bitmap)); Bitmap bitmap1 = (Bitmap)tc.ConvertFrom(image); bitmap1.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); } I got the trick from stack o

ASP.NET MVC generates error = "OutputStream is not available when a custom TextWriter is used"

In a simple application I'm creating, I have a custom ActionResult class that returns an image data. In my view, I simply used: <%= Html.Action( "GetImage" , "ImageCustom" , new {id = item.CategoryID}) %> to call the custom action class ExecuteResult method. This method reads the image byte array and store's it in a stream object. However, the call generates an error stated in the title. Html.Action writes html which implements text writer class. The solution would be using Url.Action() embedded in the image src attribute: <img src= '<%= Url.Action("GetImage","ImageCustom", new {id = item.CategoryID}) %>' alt= "No Photo" width= "100" height= "100" /> Greg

Object doesn't support property or method call in consuming JSON object From ASP.NET MVC In Internet Explorer

Recently, I've followed a simple tutorial regarding consuming JSON object in ASP.NET MVC. The error stated from the title of this post appears in IE9. The original snippet is this: $(document).ready( $.getJSON( 'http://localhost:6222/home/customerjson' , function (item) { $( '#result' ) //show product name in .html( '<p>' + item.CurrentCustomer.CustomerName + '</p>' ); } ) ); The solution is to enclose $.getJSON() in a function() statement based from the modified code below. The popup script just went away. $(document).ready( function (){ $.getJSON( 'http://localhost:6222/home/customerjson' , function (item) { $( '#result' ) //show product name in .html( '<p>' + item.CurrentCustomer.CustomerName + '</p>' ); } )

Donate