Posts

Showing posts from 2009

How a FileZilla bug made me sweat

Happy that I finished some minor task, I was starting to deploy on PR.

Then when I test, surprise, I get the error "Could not load file or assembly 'XXXX' or one of its dependencies. An attempt was made to load a program with an incorrect format.". Pfu. Never got this before, so I started googling. A lot of messages and the conclusion it was that the server is a 64 bit version and I have a 32 bit dll version. But not in my case, both where 32 bit, and the mystery raises more and more.

So I start doing foolish things, like when you do when the PR site is down and customers want to buy things from you. I even uploaded a 64 bit version, I restarted my machine, compiled, uploaded again with AnyCPU, 32, 64 even DEBUG mode. Nothing. Same error.

I've had a fresh build of Windows 7, so I thought this would be the problem. All day I had minor problems with the OS so I was suspecting it too.
Running out of time.

I go remote to the server. I recycle the App Pool. Nothing. Othe…

ASP .Net server control for youtube embeded videos

Here is ASP .Net server control for youtube embeded videos.
The param definitions can be found here
Only Src param is required, other parameters are generated only if have another value than the default one.
Usage

<cc1:YouTubeEmbed ID="YouTubeEmbed1" runat="server" Src="http://www.youtube.com/v/P38oxTrWYp0&f=videos&c=ytapi-AdrianIftode-youtubelyricsmas-ia9d2g0e-0"
AutoPlay="false"
AllowFullScreen="false"
Border="true"
PrimaryBorderColor="0x6b8ab6"
SecondayBorderColor="0x2b405b"
GenieMenu="true"
HighDefinitionPlayback="false"
LoadRelatedVideos="false"
Loop="false"
ShowAnnotations="true"
ShowClosedAnnotations="true"
ShowInfo="false"

Think LINQ deffered execution like an non-clustered SQL View

LINQ deferred execution is when you create the query, but this is not executed until you need it. So first you tell what you need and you'll get it when is needed by calling methods like ToList, First, Single, etc.


var ctx = ...
var query = from p in ctx.products
where p.enabled == true && c.deleted == false
select p;

After calling query.ToList(), the LINQ creates the SQL, sends it to server, get the results and then creates the products list. query is an IQueryable<T> object and ToList creates an IList<T> object.

An non-clustered SQL View acts pretty same like the LINQ non deferred execution. First you define the query, then you call it or use it in any other queries.

CREATE VIEW vwProducts
AS
SELECT p.*
FROM dbo.products p
WHERE p.enabled = 1 AND p.deleted = 0

SELECT *
FROM vwProducts p
WHERE p.description like '%potatoes%'

SQL Server will expand the View vwProducts when the last query is executed.

So, how to see the first LINQ query like a v…

CLR custom aggregates for SQL Server

Having the tables "product" and "product_stock" I want to display for each product the comma-separated list of sizes and this list is ordered by a specific order.

CREATE TABLE product(
product_id int,
code varchar(50) )

CREATE TABLE product_stock(
product_stock_id int,
size_order int ,
size varchar(20) ,
product_id int )

-- desired output
Product_id Sizes In Stock
35 UK7, UK8, UK9, UK11, UK10.5, UK12, UK10
36 L, M, S, XL, XXL, XXXL


In SQL this can be achieved with cursors, but everyone suggest not to use them. After some digging I found out about custom aggregates and they saved the day.

In the MSDN article is an example which concatenates the strings and this I'm writing does the same, only it adds the ordering feature.

The problem occurs with the ordering because SQL doesn't allow to use the "ORDER BY" clause in subqueries or if the "GROUP BY" exists, then the only columns allowed are the ones in grouping or in th…

Tracking codes in DEBUG-RELEASE mode

While you work on a website which includes some JavaScript for counters and statistics is better not to allow or to "hide" them during the development time.

One solution is to comment the source code and every time before the page is published, to uncomment the relevant lines. This leads to errors sooner or later, we use to forget things (uncomment things).

A better option is to use preprocessor directives #if, #endif with the DEBUG compile option.
A protected(at least) boolean field must be added in the code behind.


public partial class home : System.Web.UI.Page
{

#if (DEBUG)
protected static readonly bool Debug = true;
#else
protected static readonly bool Debug = false;
#endif
}



Now the field Debug can be used in the web form. The following google analytics code will exist only when the source code is compiled in Release mode.

<%if (!Debug) { %>
<script type="text/javascript">
var gaJsHost = (("https:" == document.…

Levenshtein distance - use on web site search

Levenshtein distance is the minimum number of character deletion (D), insertion (I) or substitution(S) operations to transform a string to another.

I used this algorithm to give alternatives to an user when he gets no results after he submitted search by a specific keyword. Often happens when the keyword is misspelled or even there are no matches.

If an e-shop sells very expensive bracelets and has one called "Diamond Tennis Bracelet", it would be nice when the customer searched for "tenis" and he found nothing to give him a close suggestion like "tennis" and no random ones.

Here follows the Levenshtein algorithm implementation in C# in a O(mn) space

public static int distanceMN(string source, string destination, bool ignoreCase, int threshold) {
if (ignoreCase)
{
source = source.ToLower();
destination = destination.ToLower();
}
if (source == destination)
return 0;

Eval: Sometimes I like spagheti code

I just discovered some interesting features of the construct <%# Eval() %>. Eval function uses reflection to evaluate a property of a bound object.

I have two classes Product and Category and I want to display the available products in a page, by using a data bound control.

public class Product {
public int ID;
public string Name;
public int Stock;
public DateTime Date;
public Category Category;
} public class Category {
public string Name;
public DateTime Date;
}

In a data template ( ItemTemplate for sample ) I can use the Eval function like:

1. very simple evaluation without using formats
<%# Eval("Name") %> - will output the name
<%# Eval("Stock") %> - will output the stock ( ..and so on )

2. using a format
<%# Eval("Date","{0:yyyy MMM dd}") %> - for sample: 2009 Apr 06
<%# Eval("Stock","{0} items") %> - will output "10 items" if the stock is 10

3. Combining two or more prop…

Using UrlRewritingNet and Elmah together

UrlRewritingNet is an Url rewriting tool for ASP .Net and Elmah is a module for logging unhandled errors.
UrlRewritingNet can set a default location for “directory” requests via defaultPage property in <urlrewritingnet> section. When a file without extension is requested the defaultPage value is appended to the original URL. Elmah provides a handler for getting the errors summary, usually called elmah.axd. This handler also responds to the followings requests: 
/elmah.axd/rss – RSS errors list feed 
/elmah.axd/digestrss – RSS digest 
/elmah.axd/download –  comma separated errors list
/elmah.axd/about – about page 
/elmah.axd/stylesheet – the stylesheet used
/elmah.axd/detail?id=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX – html single error summary 
/elmah.axd/xml?id=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX – xml single  error summary
/elmah.axd/json?id=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX – json single error summary 

These requests are like a request for a file with no extension. This is why the  U…

Un required validator pentru DropDownList cu RangeValidator

Validatorul RangeValidator va aparea in UI ca un RequiredValidator. Utilizatorul nu va putea trece mai departe daca nu alege o optiune de la 1 la 5. :)

<asp:DropDownList ID="ddlTitle" runat="server">
<asp:ListItem Value="0">Select Title</asp:ListItem>
<asp:ListItem Value="1">Mr</asp:ListItem>
<asp:ListItem Value="2">Mrs</asp:ListItem>
<asp:ListItem Value="3">Miss</asp:ListItem>
<asp:ListItem Value="4">Ms</asp:ListItem>
<asp:ListItem Value="5">Dr</asp:ListItem>
</asp:DropDownList>

<asp:RangeValidator ID="rangeValidator"
Text="*"
ErrorMessage="The title is required."
ControlToValidate="ddlTitle"
Type="Integer"
MinimumValue="1"
MaximumValue="5"
runat="server"
><…