Blog posts

  • IMPORTANT! ASP.NET Security vulnerability

    September 20, 2010 | Tags: ASP.NET
    Microsoft have released a Security Advisory about a security vulnerability that have been found in ASP.NET. This vulnerability exists in all versions of ASP.NET.

    Exploiting the error messages an attacker can gain access to files on the web site like for instance the web.config file.

    Read Scott Guthrie's blog post concerning this ASP.NET security vulnerability and patch your sites ASAP!

  • LINQ: Join on multiple conditions

    September 20, 2010 | Tags: LINQ, Snippet
    Here's a sample of a LEFT OUTER JOIN in LINQ using two conditions:
    MyDataContext db = new MyDataContext();
    string username = "test";

    IEnumerable<MyType> query = from c in db.MyCategories
    join s in db.MySettings on new { Username=username, CategoryId=c.CategoryId } equals new { Username=s.UserId, CategoryId=s.CategoryId } into tmp
    from ss in tmp.DefaultIfEmpty()
    orderby c.CategoryName
    select new MyType(c.CategoryId, c.CategoryName, ss.UserId == null);
    Notice the part after on:
    join alias in tablename on new { Name1=alias.Field1, Name2=alias.Field2 } equals new { Name1=alias.Field1, Name2=alias.Field2 }

    Please note that I'm mixing the conditions in my example using one value from a table and one string from the code. It's also important that you set the same names one each side of the equals

  • Copy data from another table using INSERT

    September 20, 2010 | Tags: SQL, Snippet
    INSERT INTO MyDestinationTable (MyField1, MyField2, MyField3)
    SELECT MyField1, MyField2, MyField3 FROM MySourceTable
    If your two tables have the exact same structure you may omit the list of columns in both the INSERT and SELECT sub statements, but it doesn't hurt to be explicit.

  • The best links for learning LINQ

    September 17, 2010 | Tags: Database, C#, LINQ
    The NOT IN clause in LINQ to SQL
    MSDN: Visual Basic LINQ to SQL Samples
    MSDN: 101 LINQ Samples for C#

  • Insert and delete with LINQ to SQL

    September 17, 2010 | Tags: C#, LINQ, Snippet
    Needed to remove all old posts associated with a specific user and then add new posts. With a DataSet, this could be done by fetching the data, clear it and then add the new posts. A simular way is possible in LINQ-to-SQL:
    CalendarDataContext db = new CalendarDataContext();
    List<int> categories = new List<int>() // ..with some data of course

    // Get current posts
    IQueryable<SubscriptionSetting> settings = db.SubscriptionSettings.Where(s => s.UserId == username);

    // Delete all old posts

    // Add new
    foreach (int c in categories) {
    db.SubscriptionSettings.InsertOnSubmit(new SubscriptionSetting() { UserId = username, CategoryId = c });