Skip to main content

Posts

Showing posts from 2013

Bare metal HTTP

This time I’ll show you how to create a simple console app that requests resources over the internet using the lowest level APIs (at least the lowest level available in .NET). The whole point of this post is to prove that there is no magic behind entering a URL in the web browser and get a web page out of that. This little app requests a webpage and prints out the response to the console. If you want, you can extend it to save the content to a local file and then show it up in the web browser or whatever. (Maybe I'll do that in a future post). Here I’m using IP and TCP (Network and Transport protocol respectively) to connect the app to the web site, send a request and get a response. The code is self-descriptive but as usual if you have any doubt feel free to contact me. (For the sake of simplicity, I'm not handling exceptions nor writing single responsibility methods and stuff like that). If you are interested in learn more about http and how the web works, I h...

Oracle - How to insert a string longer than 4k using C#

And this is another PITA sponsored by our good friends of Oracle ;) After being struggling whit this issue for a whole day, I finally figure it out. There is a bunch of posts on the web saying that you should change the column type on the table from let’s say LONG to CLOB or stuff like that… but I have tried it and it has no effect if you are working with C# (maybe make sense if you are working with PL. I’m not sure, haven’t test it). The thing is even if you change the column type, you’ll still get an error saying “the string literal is too long…” The way to insert strings larger than 4K into Oracle databases is using bindings. And yes, you can insert 4k plus strings into LONG columns, there is no such 4k limit. This is how you do that: * I skip error handling, disposing objects, etc… just for brevity, of course you shouldn't ;) Another interesting thing that almost every posts got wrong is that you don’t have to specify the parameter type, the library will choose th...

How to upgrade an Acer Aspire V5 122P to SSD

Just two weeks ago I've upgrade my Acer v5 to SSD, and to my surprise, by following these tutorials, everything worked as expected ;) http://www.youtube.com/watch?v=z8OJTZGdVq4 http://www.youtube.com/watch?v=ZBJsh-lkQt8 http://www.youtube.com/watch?v=dm7xY_hviaY For those who doesn’t know about the Acer Aspire V5, I think it’s the cheapest touchscreen Win8 ultrabook on the market. I forced myself to use it as development machine (running visual studio 2012, R#, SQL Server and all that stuff) and for 450 bucks, it’s more than I expected. The touchpad is decent enough (although it can be better), the keyboard works great and by throwing another 100 for a SSD into the mix, increased the overall system performance, making the Acer a budget dev machine. The only drawbacks I found so far it’s the AMD 1GHz processor (of course you cannot change that) and the plastic case, but for the price I payed for it, I’m more than satisfied. If you are on a budget, I high...

How to get windows.external to work when using Awesomium.NET – Take 2

I got asked to provide the code from my post on How to get windows.external to work when using Awesomium.NET . Since I don’t have it (I don’t know why, but it’s gone…). I decided to create a new example, post it  on this repo at github  and hopefully don't loose it anymore ;). I’m assuming you are familiar with Awesomium.NET and just can’t figure out how to get windows.external at work (which is a must have on embedded web browser controls). I’m not going to show you what is Awesomium or how to use it, there’s a lot of good tutorials on the official site  and it has no sense I retype that here. So, enough jibber jabber, let’s go to the code. First of all, be sure you setup the right platform prior to compile the code (Last time I checked Awesomium.NET only works on x86). If you download the sample code, you will find this html file within the solution.  The whole point of _external.html_click is to marshall the function call from javascript to c#...

How to replace text with images on Word documents using C#

This post it’s a reply to a question I got from a previous post that shows how to work with Word templates from C# code . If you haven’t read it, I recommend to do so because I’m not going into details here. But basically it was about how to create a Word document from a template and perform same text manipulation. So, the question from Marcel Kieboom  was “Do you know if it would be also possible to replace one of the words with an image which is locally stored?” The answer is yes, and this is how you can do that. Based on the same convention I had used in the previous post, I should have a template like this: What I’m trying to do here is replace the text [angus-young] for an actual pic of Angus. The technique I’m using it’s pretty common on web sites and basically consist in have an image with a matching name for each keyword I want to replace and then create the image path dynamically. This is the C# code to do that. * When I execute t...

Hidden memory leaks in WinForm Apps

Dealing with memory leaks in .NET apps it is always a hard thing to do. While most of the time the GC does a great job freeing us to worry about memory management, there are some edge cases where we have to be very careful to not screw things up. In this post I am going to show a common issue I see when doing code reviews on WinForm apps. First, let’s take a look at this code and see what it is wrong with it. As you can guess, the problem with the code above is that creates a memory leak ;). But why? We are effectively disposing FrmFoo... There are two issues with the following line, The first one, the GC will never be able to collect the garbage created inside the anonymous function generated by the C# compiler to back the lambda expression attached to the control’s click event. Even when we are disposing the form, the object created by the C# compiler is rooted inside the scope of that function and cannot be destroyed; hence the memory leak. ...

Loading large html pages using Awesomium.NET

While there is no limitation in the page size per se, you may experience some weird behavior (errors) when trying to load large html files using the Awesomium WebView LoadHTML method. I been using this framework for a week now and I’m more than satisfied, it’s a great piece of software, but the error message you get when a large page fails to load, it is a bit confusing “the uri is too long” …. I was working with two files, one of them was large, the other was small, but URL length was the same for both, let say foo.html and bar.html . When I load foo.html things work great, but when I try to load bar.html, boom, I got an error saying that something is wrong with URI’s length? here is where I went “WTF, both URIs have the same length!” ;) The thing is that the framework internally is using the .NET Uri class which have a limitation of 65K something characters, if the html that you are trying to load exceeds that limitation (not the url, the html) you will get that excep...

How to get windows.external to work when using Awesomium.NET

Note: There is an updated version of this post right here  (with source code and all stuff) I been doing some experiments with the Awesomium  web browser framework, and after a lot of failing attempts, I finally got windows.external calls working. Awesomium  is a windowless port of Chromium/WebKit. For WinForm developers, an alternative way to embed web pages in your winforms or WPF apps without having to use the WebBrowser control. For a long time, I been using the IE embedded browser to show reports (webpages) in winform apps, with let say…, acceptable results ;), but the thruth is there is a lot of goodness out there into the wild with better support for css, html 5, etc. So I decided to install Awesomium.NET and give it a try. While the API is pretty intuitive, I had some trouble trying to call C# methods from Javascript, a fairly easy thing to do with the classic webbrowser control. This is how things work when using embedded IE Browser ...

WinForms, How to enable sorting for a datagridview that uses linq queries as data source

How to enable sorting for a datagridview that uses linq queries as data source it is a frequently asked question on the web. The short answer is you cannot, but let dig a little deeper. Why the sorting functionality works as expected when you use it on a grid binded to a DataTable and miserably fails when you bind it to an IEnumerable<DataRow>?  A bit of research later… Easy, the DataTable class provides its own implementation for sorting rows whereas IEnumrable<T> does not. I faced this problem building the power grid control, because (to make paging records easy) internally I was using linq queries. As soon as I realized that the datagrid sorting was broken, I started to look on the web an easy way to solve this problem (other than implement my own sorting mechanisms, of course). I found a couple of alternatives but this one  convinced me. This class has a neat way to solve generic sorting with a minimal impact on performance. I’ve tested it sorti...

WinForms, how to implement conditional format for DataGridViews (part 2)

In the previous post I showed you how to implement conditional format functionality for datagridviews using C#, working on conditions that we already know at compile time, which means that those conditions will be written by programmers, not end users. In this post I’ll show you how to deal with user defined conditional formats, which is a more useful thing to do if what you want to provide this kind of functionality. With this new feature the users are going to be able to write conditions in an Excel fashion (formula like, if you will) To implement this feature I’ve upgraded the project that we been using for this series to .NET Framework 4.5 in order to use the latest version of the Roslyn compiler. The Roslyn compiler is the backbone of this feature. If you are interested in Roslyn, I made a series on How to build a DSL on top of Roslyn  that covers some basic stuff of this great piece of software. As in the Roslyn DSL series I'll rely on source to source translati...

WinForms, how to implement conditional format for DataGridViews

Being able to apply conditional formats to datagridviews is a must nowadays, users of our app probably make heavy use of this feature on Excel spreadsheets, and they want it to be present on any "grid alike" screen. I’ll show you two ways to implement this feature the first one is using pre-built formats, or formats specified in C# at compile time (better performance) and  the second one that allows users to add their own conditional formats with formulas written in an excel fashion . In the last case the performance is not good enough for prod., but I’ll be sanding rough edges and hopefully make it work in a near future. To work on this feature, I'm going to use the same datagrid I used in the previous post, which you can download from here . The whole functionality is already implemented, so I'll just show you how to use it. The only thing you have to do is register the conditional format and the component will take care of the rest.  To register a...