Monday, 1 November 2010

cannot be performed on a file with a user-mapped section open

So..

I installed TortoiseSVN, when i tried to add a folder to a repository i had created with VisualSVN i kept getting the error:

"cannot be performed on a file with a user-mapped section open"

After googling around i could not find good answers to this, but I did mananged to fix the problem, heres how:

When attempting to add the folder again, I started windows process manager through task manager so that i could watch what tortoiseSVN process were running when the error occured..

I then allowed those through windows firewall and it worked fine. If you have a similar problem I hope that sorts it!

Paul

Friday, 1 October 2010

Delete duplicate rows sql server

Hi, here is a not to common task since you of course should not really end up with duplicate rows in a table, but, knowing that

 "The man who makes no mistakes does not usually make anything. ~Edward Phelps" 

 if you do end up with duplicates, then here is how to solve the problem. This SQL will delete the first occurrence of the duplicate entries. 

To change it so that it deletes the last entry, you would replace Min with Max.

DELETE FROM table_with_duplicates 
WHERE id IN ( 
     SELECT bad_rows.id from table_with_duplicates AS bad_rows 
     INNER JOIN ( 
           SELECT column_1, column_2, Min(id) AS min_id FROM table_with_duplicates 
           GROUP BY column_1, column_2 HAVING COUNT (*) > 1
      ) AS good_rows 
     ON good_rows.column_1 = bad_rows.column_1 
     AND good_rows.column_2 = bad_rows.column_2 AND good_rows.min_id <> bad_rows.id 
)

Wednesday, 1 September 2010

eee pc bootmgr is missing

ok, so along with a lot of the people that buy an eee pc, i tried to install a different operating system on bringing it home. having only put the bootable usb in once and tried to boot, everytime after, i received the "bootmgr is missing" ctrl alt delete to restart message, and then just went round in circles from there..

having googled forthe remedy for a while, most answers talk about how to make the bootable usb, setting the boot priority in your biosand having a usb stick less than 8gb, i found the solution to be reboot, and keep hitting F9!

this will make the asus recovery tool kick in, and from there you can hit recover and off you go.. once this is done and the operating system is fully installed, you can then install the different operating system..

Sunday, 1 August 2010

Learn NHibernate 6 - Advanced querying of Child Collections



disclaimer - at the moment, this post is in note format from a workshop I did, I need to it clean up, and add grammar
Traversing a relationship

HQL

where there would normally be a join in sql

SQL[sql]
select * from customer c inner join orders o on c.id = o.customerid where c.firstname = 'steve' and o.orderdate > 6/1/2010

[/sql]


but in hql becasue the relationships are already in the mapping file, there is no need to perform the join.

HQL[sql]
select c from customer c, c.orders.elements o where where c.firstname = 'steve' and o.orderdate > 6/1/2010
<div id="_mcePaste">[/sql]


Criteria[csharp]

ICriteria.CreateCriteria(typeof(Customer))
.Add(Expression.Eq("Firstname", "Steve"))
.CreateCriteria("Orders")
.Add(Expression.Gt("OrderDate", new Datetime.Now)))
.List<Customer>();

[/csharp]


Revisiting Data Access

Each mothod gets its own ISesssion instance

Each method is completely atomic.

recalling the pattern[csharp]

using (_SessionFactory.GetSession())
{
//data access happens here
}

[/csharp]


This means no lazy loading as the session has been disposed of already.

Solution? evolve our data provider to introduce a new abstraction

Having the data acess provider create its own ISession is a violation of SRP/SOC principles.

We should be giving the data provider the ISession

Lets add a parameter to the class' constructor.[csharp]

new NHibernateDataProvider(ISession session) {}

[/csharp]


Now something higher up in our object highrachy will be in control of the session. We can now remove all of the using statements in the individual data access methods GetCustomerByiId etc..

We no longer call GetSession anywhere.
Gettting child objects with HQL[csharp]

private void GetCustomersWithOrdersSince(DateTime orderDate) {
return _session.CreateQuery("select c from customer c, c.Orders.elements o where o.OrderDate > : orderDate").SetDateTime("orderDate", orderDate).List<Customer>();
}

[/csharp]

Gettting child objects with Criteria[csharp]

private void GetCustomersWithOrdersSince(DateTime orderDate) {
return _session.CreateCriteria(typeof(Customer))
.CreateCriteria("Orders")
.Add(Expression.Gt("OrderDate", orderDate))
.List<Customer>();
}

[/csharp]

Distinct in Criteria.

If we put the line of code:[csharp]

.SetResultTransformer(new NHibernate.Transform.DistinctRootEntityResultTransformer())

[/csharp]


above the List, it will proivide distinct Customer results

Important to understand is that using this distinct transformer in criteria will NOT generate the DISTINCT keyword in the SQL it generates. The HQL does. The transformer will bring back all results and then perform the distinct operation on the client side in NHiberante code. This can be a big performance hit if not understood or used correctly.

If you need to return distinct entities in criteria, add the following line of code to the above query:[csharp]

private void GetCustomersWithOrdersSince(DateTime orderDate) {
var ids = _session.CreateCriteria(typeof(Customer))

.SetProjection(Projections.Distinct(Projections.ProjectionList()

.Add(Projections.Property("CustomerId))))
.CreateCriteria("Orders")
.Add(Expression.Gt("OrderDate", orderDate))
.List<int>();

return _session.CreateCriteria(typeof(Customer))
.Add(Expression.In("CustomerId", ids.ToArray<int>()))
.List<Customer>();
}

[/csharp]


Now the distinct call will be wrapped around hte outside of the original sql generated by the criteria.

Wednesday, 14 July 2010

JQuery for each div with class, do some processing



Here is some JQuery i recently wrote to do some styling changes.

The code basically fires an event on a select box change, and then for every div with the class "localize", if the lang attribute value is not the same as the selection in the option box, it hides that div and makes sure any div where the lang attribute value is the same as the selection is showing.

[javascript]
//get the value of the selection box
var selectedValue = $('#cultureSelector').val();

//for each div with class localize
$('div.localized').each(function() 
{
     //get the value of the lang attr
     var langAttrVal = $(this).attr("lang");

     if(langAttrVal.indexOf(selectedValue) == -1) {
          $(this).attr('style', '');
          $(this).addClass('displaynone');
     }
     else 
     {
          $(this).removeClass('displaynone');
     }
});

[/javascript]

Sunday, 11 July 2010

Linq and Lambda equivalent of SQL Where in

Working with LINQ to SQL, several times I have come across a need of writing a LINQ query equivalent of T-SQL “Where IN” clause.

This is what T-SQL query looks like.

SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax

FROM Customers

WHERE (Country IN ('UK', 'USA', 'Australia'))

How do you write Where IN in LINQ?

Below is the query which can be used for a Linq “WHERE IN” scenario.

Query 1:

string[] countries = new string[] { "UK", "USA", "Australia" };

var customers = from c in context.Customers

where countries.Contains(c.Country)

select c;

Below is the query which can be used for a Lambda “WHERE IN” scenario.

Query 2

string[] countries = new string[] { "UK", "USA", "Australia" };

var customers = context.Customers.Where(c => countries.Contains(c.Country));