Tuesday, June 17, 2008

Assembly.GetEntryAssembly()

I have to use a library designed for use with Winforms.
I have to use it in Asp.net though, so at one point I got to a method using unguarded Assembly.GetEntryAssembly() call that failed.

After a little research and found this in the MSDN:



The GetEntryAssembly method can return null reference (Nothing in Visual Basic) when a managed assembly has been loaded from an unmanaged application



and became evident that the reason for the problem was that the library was call from the unmanaged IIS process.

I searched for a generic solution, but finally only managed to find a hint here:http://www.developersdex.com/asp/message.asp?p=2912&r=5817390

so now I'll add this code to solve the problem

if (null != Assembly.GetEntryAssembly())

{

// proceed with Assembly.GetEntryAssembly()

}

Else

{

// read a setting from the config file and set the entry assembly to this value if not empty

System.Configuration.ConfigurationManager.AppSettings["EntryAssembly"]

}


and thats all for now ...

Monday, May 19, 2008

Linq OrderBy Descending fun

So for a few days I've been playing with Linq with production code and it looked great and (at least the usual stuff) pretty straightforward.
The underlying c# is scary of course, reminding me of the nightmare from the C++ generics/COM days. But at least now the IDE has evolved and IntelliSense works nicely.

Anyway - I came to the point were some tweaking was needed and some parametrizing - mainly i needed the order by to be runtime desided - 'dynamic' if you wish - so i started reading around blogs and sample code, but all seem to be aiming too high and I didn't have much time to really dive into Linq (which I should do someday anyway)... still quantity changes lead to some understanding of the problem in question , mostly after looking at (not really reading) samples at http://www.ddj.com/database/205604421, http://srtsolutions.com/blogs/billwagner/archive/2006/03/29/ordering-linq-results.aspx
and http://community.bartdesmet.net/blogs/bart/archive/2008/04/27/q-is-iqueryable-the-right-choice-for-me.aspx

I came up with this really small snippet of code that solved easily my problem



public static OrderedEnumerableRowCollection
OrderByEnhanced
(this EnumerableRowCollection source, Func keySelector, bool ASC)
{
if (ASC)
return source.OrderBy(keySelector);
else
return source.OrderByDescending(keySelector);
}




ain't it funny - it is so simple, but looking for a solution to my problem lead me to reading a lot about Linq and helped me learn a lot too, so that time was not waisted, so to say

cha!

Wednesday, April 16, 2008

OUTPUT clause in INSERT/UPDATE/DELETE statements

just playing with the new (for sql server 2005 and for me) OUTPUT clause.
I had some hopes for it but turns out i cant write something like


insert targettable (col1,col2)
output Inserted.col1, sourcetable.somecol
select somecol1,somecol2 from sourcetable



that is - i cant output values from the Inserted and also values from the source table :(
a bit disappointing in a way, of course, because i wanted to have backwards mapping between the source and inserted tables, but now I just decided to use one of the columns in the inserted table as this mapping ids placeholder

voila...

EDIT:
according to msdn article on T-SQL Delete
it is possible to OUTPUT exactly what I need...


DELETE Production.ProductProductPhoto
OUTPUT DELETED.ProductID,
p.Name,
p.ProductModelID,
DELETED.ProductPhotoID
INTO @MyTableVar
FROM Production.ProductProductPhoto AS ph
JOIN Production.Product as p
ON ph.ProductID = p.ProductID
WHERE p.ProductModelID BETWEEN 120 and 130;



strange...now I'll have to achieve the same results somehow...