This blog is a knowledge base...where I clip cool tricks and urls

List and delegates

clipped from weblogs.asp.net

System.Text.StringBuilder sb = new System.Text.StringBuilder();

   31             sb.Append("<h1>List As Is</h1> using ForEach<br/> ");

   32             sb.Append("<br/><ul>");

   33             ninjas.ForEach(delegate(Ninja n)

   34             {

   35                 sb.Append(String.Format("<li>{0}, ({1})</li>", n._name, n._age));

   36             });

   37             sb.Append("</ul><br/>");

   38 

   39             ///////////////////////////////////////

   40             sb.Append("<h1>Age is greater or equal to 0</h1>using FindAll,and foreach<br/> ");

   41             sb.Append("<br/><ul>");

   42             List<Ninja> thoseAlive = ninjas.FindAll(delegate(Ninja n)

   43             {

   44                 return n._age >= 0;

   45             });

   46             thoseAlive.ForEach(delegate(Ninja n)

   47             {

   48                 sb.Append(String.Format("<li>{0}, ({1})</li>", n._name, n._age));

   49             });

   50             sb.Append("</ul><br/>");

 blog it

Async upload file

clipped from jeffzon.net

Let UpdatePanel support file uploading (1): Let's Get Started


by Jeffrey Zhao
30. March 2007 11:15

UpdatePanel cannot be used to upload files in ajax style from the very beginning. Eilon Lipton has written a blog post to explain the reason of that. As the post said, the two workarounds to resolve the problem are:


  1. Have a dedicated "Upload" button that does a regular postback instead of an async postback. You can achieve this using several techniques: Have the button be outside all UpdatePanels; have the button be the target of an UpdatePanel's PostBackTrigger; or call ScriptManager.RegisterPostBackControl() on it.

  2. Have a dedicated file upload page that doesn't have any UpdatePanels. Many web sites already do this anyway.
 blog it

C# Microsoft have added features that makes the language just a little bit dynamic

Dictionary<string, string> dic = new Dictionary<string, string>()
.Init(
new {
Key1
= "val1",
Key2
= "val3"
});

For version 3.0 of C# Microsoft have added features that makes the language just a little bit dynamic but still strongly typed. One of this feature is collection initializers for lists and dictionaries like we initialized arrays on earlier versions:

 blog it

ASP.NET MVC has arrived

clipped from blogs.msdn.com

Ok, ok, it took us a little longer than we expected, but we finally got the ASP.NET 3.5 Extension Preview posted! This is the release with all the MVC goodness you have been hearing about as well as some very cool stuff  such as Dynamic Data controls which makes building data driven web applications a breeze, AJAX history support, ADO.NET Data Services (aka Astoria), the ADO.NET Entity Framework and updated ASP.NET support for Silverlight.

 blog it

Dynamic Query Reflection within Linq

clipped from www.west-wind.com
As I suggested in my comment on the last blog post - read the docs and samples...and failing that ask a question on the LINQ forums: http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=123&SiteID=1

Even Anders hangs out on that forum - so you will get help if you ask a question on it. ;-)

Daniel Moth has a good pointer to some LINQ samples you can use: http://www.danielmoth.com/Blog/2007/08/linq-samples.html One of these LINQ samples is called "DynamicQuery", and it provides a useful library that enables you to write dynamic LINQ expressions that enable you to accomplish what you are after above.

For example, you could write:

var query =
db.Customers.
Where("City = @0 and Orders.Count >= @1", "London", 10).
OrderBy("CompanyName").
Select("new(CompanyName as Name, Phone)");
 blog it

Dynamic Query Reflection within Linq

clipped from www.west-wind.com
As I suggested in my comment on the last blog post - read the docs and samples...and failing that ask a question on the LINQ forums: http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=123&SiteID=1

Even Anders hangs out on that forum - so you will get help if you ask a question on it. ;-)

Daniel Moth has a good pointer to some LINQ samples you can use: http://www.danielmoth.com/Blog/2007/08/linq-samples.html One of these LINQ samples is called "DynamicQuery", and it provides a useful library that enables you to write dynamic LINQ expressions that enable you to accomplish what you are after above.

For example, you could write:

var query =
db.Customers.
Where("City = @0 and Orders.Count >= @1", "London", 10).
OrderBy("CompanyName").
Select("new(CompanyName as Name, Phone)");
 blog it

Lambda Expression for C# 3.0

"A lambda is an inline expression or statement block with a concise syntax that can be used in C# 3.0 and later wherever a delegate type is expected" . In a certain way we can consider Lambda Expressions as an evolution of the anonymous methods.

Look the examples below, at first sight the syntax looks very weird but when you know how they are built and you get used, you will see that it's extremely easy.

x => x + 1

x => { return x + 1; }

(x, y) => x == y

(int x, int y) => x * y

() => ExecuteAnyMethod();

Maybe you already noticed that a lambda consist of a list of input parameters, the operator "=>" (goes to) and a expression or statement. In the form of:


(input parameters) => Expression or Statement.


The list of parameters is optional, and you don't need to specify the types of the parameters in the cases where the type can be inferred. Parenthesis are also optional when we only specify one parameter.

 blog it

.NET quiz for geeks

clipped from www.ayende.com

Scot Hanselman has a list of question that you would interview a .Net candidate with. Here are my answers (without using MSDN/Google/Help/etc, just my head):

There are 102 questions, I couldn't answer 20 of them (19.6%), (you judge if I was correct or not).

I'm weak in ASP.Net & Xml, but that is no surprise to me.

 blog it

diving into LINQ

In my last column I talked about LINQ as the key feature in .NET 3.5. ASP.NET 3.5 is not going to see a whole bunch of new features only just a few new controls and the roll up of some of the slip stream features like Microsoft ASP.NET AJAX and IIS 7 support directly rolled into the .NET framework. The big news in ASP.NET 3.5, and .NET 3.5 in general, is LINQ and its related new features. In the last column I introduced some key features of LINQ and why I think it’s easily the most important feature in .NET 3.5. This time I’ll take a closer look at the data specific LINQ features in LINQ to SQL.
Unwrapping LINQ to SQL
 blog it

clean CSS separation

clipped from weblogs.asp.net


VS 2008 Web Designer and CSS Support

One of the big features that web developers will quickly discover with VS 2008 is its dramatically improved HTML designer, and the rich CSS support it brings. 
VS 2008 now uses the same web designer that ships with Microsoft's new Expression Web product.  In addition to providing lots of new functionality, you'll also find that this web designer is much faster than the VS 2005 version (which was based on a much older code base).
VS 2008 supports a new tool window inside the IDE called "Manage Styles".  This shows all of the CSS stylesheets, and their corresponding rules, for the page you are currently editing.  It can be used both when you are in design-view, as well as when you are in source view on a page:
 blog it

MVC in ASP.NET part 2

clipped from weblogs.asp.net
The ASP.NET MVC Framework enables you to use any data access pattern or framework you want in order to retrieve and manage your models.  If you want to use ADO.NET DataSets/DataReaders (or abstractions built on top of them) you can.  If you prefer to use an object relational mapper (ORM) like NHibernate, LLBLGen, WilsonORMapper, LINQ to SQL/LINQ to Entities you can absolutely use those as well.
For our e-commerce sample application I'm going to use the built-in LINQ to SQL ORM shipped in .NET 3.5 and VS 2008.  You can learn more about LINQ to SQL from my ongoing blog tutorial series that covers it (in particular make sure to check out my Part1, Part2, Part3 and Part4 posts). 
Mapping URL Parameters to Controller Action Methods
 blog it

MVC in ASP.NET

clipped from weblogs.asp.net
In most web frameworks (ASP, PHP, JSP, ASP.NET WebForms, etc), incoming URLs typically map to template files stored on disk.  For example, a "/Products.aspx" or "/Products.php" URL typically has an underlying Products.aspx or Products.php template file on disk that handles processing it.  When a http request for a web application comes into the web server, the web framework runs code specified by the template file on disk, and this code then owns handling the processing of the request.  Often this code uses the HTML markup within the Products.aspx or Products.php file to help with generating the response sent back to the client.
 blog it

Avoid Loops in Excel

clipped from www.ozgrid.com
To put it bluntly I very often avoid Loops, they are far too
slow in many cases. A common mistake we all make when first learning VBA is to use Loops when we really shouldn't. Take the simple example below for instance. It Loops through a range and places the word "Blank" in each blank cell within a
used range, i.e it assumes the last occupied cell is D500
Sub WrongWay()
Dim Bcell As Range
For Each Bcell In Range("A1:D500")
If IsEmpty(Bcell) Then Bcell = "Blank"
Next Bcell
End Sub
Now compare the above code to this one:
Sub RightWay()
If WorksheetFunction.CountA(Range("A1:D500")) = 0 Then
MsgBox "All cells are empty", vbOKOnly, "OzGrid.com"
Exit Sub
End If
On Error Resume Next
Range("A1:D500").SpecialCells(xlCellTypeBlanks) = "Blank"
On Error GoTo 0
End Sub
 blog it

LINQ need primary key

what the fuck??
clipped from blog.benhall.me.uk
Type: System.InvalidOperationException
Source: System.Data.Linq
TargetSite: Void Attach(T)
HelpLink: null
Stack: at System.Data.Linq.Table`1.Attach(T item)
at SqlServer.SqlContentProvider.SaveDocument(Document document) in E:\My Documents\Visual Studio Codename Orcas\Projects\SqlServer\SqlContentProvider.cs:line 53
at SimpleWiki.DAL.Tests.DocumentTests.TestInsertingDocument() in E:\My Documents\Visual Studio Codename Orcas\Projects\Tests\DocumentTests.cs:line 28

I thought I had everything correctly, however I forgot to set a primary key in the table, hence Linq was unable to do anything with the object.  So, lesson for the day Linq must have a Primary key for the table :).

 blog it

convert with generics

clipped from nayyeri.net

Who has said we should write a simple code?!!  I used List<T>.ConvertAll() function to get a List<String> from a List<Double> then converted my List<String> to an Array of Strings.

List<T>.ConvertAll() method is a new .NET 2.0 Generic method which gets a System.Converter object and converts each element in a List<T> from type T to the outgoing type.  It seems this method is using a predicate but actually it doesn't do this.

public String Points

{

    get

    {

        return String.Join(" | ",

            (String[])this.myStudent.Points.ConvertAll

            (new Converter<Double, String>(delegate(Double value)

            {

                return Convert.ToString(value);

            }

            )).ToArray());

    }

}

 blog it

BBuilder - Open source batch builder

clipped from www.codeproject.com
Screenshot - BBuilder1.png

Introduction

One day my friend told me he needed to change his way of releasing builds. It meant he had to fix a batch to compile and publish several products. Such a process can be tedious since it has to be very reliable and you have to remember all the required DOS commands. Each step of a batch is in fact a command that can be accomplished in pure DOS or executing external executable files. So I thought it would be cool to make a batch editor in the following way:


  1. Choose a command from a toolbox

  2. Support re-ordering

  3. Save it to disk for further use

  4. Visualize all commands in a simple way

  5. Enable adding custom tools as plug-ins

  6. Test the batch before it goes somewhere (Scheduled Tasks) with optimizations

Background

Screenshot - BBuilder2.png
 blog it

GridView DataBind LINQ to ASP.NET controls

Cool!
clipped from weblogs.asp.net
Over the last few weeks I've been writing a series of blog posts that cover LINQ to SQL.  LINQ to SQL is a built-in O/RM (object relational mapper) that ships in the .NET Framework 3.5 release, and which enables you to easily model relational databases using .NET classes.  You can use LINQ expressions to query the database with them, as well as update/insert/delete data.
Below are the first four parts of my LINQ to SQL series:
In today's blog post I'll cover the new <asp:LinqDataSource> control that is shipping as part of ASP.NET in the upcoming .NET 3.5 release.  This control is a new datasource control for ASP.NET (like the ObjectDataSource and SQLDataSource controls that shipped with ASP.NET 2.0) which makes declaratively binding ASP.NET UI controls to LINQ to SQL data models super easy.
Sample Application We'll be Building
 blog it

ASP.NET MVC

Was it inspired by Django???
clipped from weblogs.asp.net


#

re: ASP.NET MVC Framework



Monday, October 15, 2007 3:22 AM
by
Brandon Bloom

I am a C# junkie, but I use Django for web development because ASP.net places far too much focus on the designer. With Django, I find an artist to do the CSS file and I just make a bunch of divs in the template.

Looks like you guys nailed it with this. Maybe I'll starting using ASP.net when this is available. Awesome stuff :-)

 blog it

Tuning Performance - LINQ to SQL

clipped from davidhayden.com
LINQ To SQL - Lazy Loading Properties and Specifying PreFetch When Needed - Sweet Query and Performance Control
This is another post in a series of posts on LINQ To SQL in how I think I have discovered a potential performance problem of LINQ To SQL only to find an elegant solution to the problem using LINQ To SQL. If you haven't been reading along with the discovery, here are a few previous posts on LINQ To SQL in this discussion of performance and query tuning:
 blog it