Part 2: LINQ Tips & Tricks [Things to get you started efficiently]

This blog entry is in continuation with my first post on Part 1: LINQ Tips & Tricks [Things to get you started efficiently]

Continuing to share some other important tips that you can take care of while developing with LINQ.

[9] Loading Options while working with tables in Relationships

You always need to struggle for performance as a LINQ Developer; perhaps because to understand better how internal mechanism works.

using (BEDataContext context = new BEDataContext())
{
    var customers = from cust in context.Customers
                    select cust;
    foreach (var customer in customer)
        Response.Write(customer.CustomerAddresses.Count().ToString());
}

Read More…

Part 1: LINQ Tips & Tricks [Things to get you started efficiently]

While my development with LINQ I got around some helps, links, small tips and tricks which makes life much easier, improves query formulation capabilities and speedup the program execute performance. Here I am sharing some of the tips I found as a LINQ Developer.

[1] Use the var Keyword When Confused

Use var keyword as return type especially when you are capturing sequence of anonymous classes. For example, joining of 2 tables and retrieving a sequence of records containing (some/few) fields of both the tables. I appreciate the developers who understand and know exactly what data type is contained in the sequence they are retrieving. But, anyhow you get stuck and don’t know the sequence IEnumerable<T> holds what data type T; then var is the good choice.

 

Read More…

File download problem: filename with spaces truncated in FF and replaced by underscore in IE

This is a mere note to remember for future rather than a blog entry. While I am working on code to download file from server. I write below code:

        {
            int ChunkSize = 10000;
            string sFileFullPath = Server.MapPath("New Text Document.txt");
            System.IO.FileInfo toDownload = new System.IO.FileInfo(sFileFullPath);
            if (System.IO.File.Exists(sFileFullPath))
            {
                Response.Clear();
                using (FileStream iStream = System.IO.File.OpenRead(sFileFullPath))
                {                  
                    long dataLengthToRead = iStream.Length;
                    Byte[] buffer = new Byte[dataLengthToRead];                   
                    Response.ContentType = "application/octet-stream";                   
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);                                                          
                    while (ChunkSize > 0 && Response.IsClientConnected)
                    {
                        if (ChunkSize > dataLengthToRead)
                        {
                            ChunkSize = int.Parse(dataLengthToRead.ToString());
                        }                      
                        int lengthRead = iStream.Read(buffer, 0, ChunkSize);
                        Response.OutputStream.Write(buffer, 0, lengthRead);
                        Response.Flush();
                        dataLengthToRead = dataLengthToRead - lengthRead;
                    }
                }
                Response.Close();
                Response.End();
            }
            else
            {
                this.Page.ClientScript.RegisterStartupScript(GetType(), "ShowMessage", "<script language='javascript'>alert('No Files Available');</script>");
            }
        }

When I ran the application I found that the file which I need to download was containing spaces in its name like “New Text Document.txt”.

The problem is:

Read More

Core ASP.NET Card, Get it Free

Core ASP.NET Card

Core ASP.NET: This Refcard summarizes the most commonly used core functions and controls in ASP.NET

Try it! Its free but requires registration.

Read More

Create PDF files on-the-fly in ASP.NET (Using Crystal Report!)

Here is another requirement (perhaps a common requirement!) I needed to implement in one of on-going project website application. I needed to generate a PDF file as output from the inputs entered by end-user in a form. On Completing the form, end-user should be able to download a PDF file of what they have given the inputs in the form.

I need to generate PDF file in a given format and force the download dialog to let the user save generated PDF save on their/client machine.

I tried searching any free :) third party dlls which would help me to generate PDF files on-the-fly. Things I tried:

[1] Tried searching almost third party tools/dlls including Siberix PDF Library , PDF Vision .Net etc etc, which are having similar functionality. But not FREE :(

[2] Tried with iTextSharp – Tutorial and PDFsharp – Download PDFsharp Version 1.20 These dlls/libraries are totally free and really interesting to work with. All we need to do is define elements like HTML and add those elements to PDF document object which then finally generated as PDF document. But, this is the problem for me as I am having a huge PDF file needed to generate as output and it would required a loot of time to code for a document by placing line by line cells, TR and TD, Tables and background, Border colors, images etc. and most of all is annoying Texts to be placed in those elements with proper fonts, colors and padding/alignment.

At last after wasting time in googling and on free tools/libraries, I got an idea to create PDF on-the-fly. I was thinking of creating PDF files dynamically and I created it smoothly with out too much work and code.

This is how it goes:

Read More

asp:Menu Server Control – Cross Browser Compatibility (Safari/Chrome)

Cross browser compatibility struggle is getting upsetting while working with asp:Menu Server Control. Anyhow, It was not rendering/working well with Safari and Chrome.

A bit of googling… and I have a solution for this.

I have added below small piece of code snippet in my MasterPage’s Page_Load event

Read More

AJAX Accordion Control “Flickering” Issue while implementing in DotNetNuke Site

While working on DNN 5.0 site, I was trying to have a left menu navigation using AJAX Accordion. Even, I got succeeded in implementing that but i come accross an issue of – flickering the Accordion control in IE while opening and closing and navigating thru panes.

After googling, I came to know about the solution and applied which worked successfully.

Frist is, Turn off FadeTransitions Property, i mean set it to “False”, :( yes, its the cute effect everybody want to have while using accordion with. But, while I set it to false and removed the TransitionDuration property – The problem of flickering issue got solved. So, everybody speak with me – “I dont want FadeTransition effect :) in DNN with Accordion!”.

Read More

Cross Browser (CSS) Compatibility (setting Browser Specific Property values of controls)

You would obviously have come across the “Browser Compatibility” issues many times while designing a webpage. For example, assigning a CSS class to any control may render well with IE but it may not get rendered that perfect way in Mozilla; and if you fix for Mozilla then IE rendering may get affected. But, we not only need to manage in between IE and Mozilla, but we also need to think about other browsers as well like Opera, Safari and Netscape etc, etc.

Here is one trick how you can you apply “Browser Specific” CSS to a control. That is, while assigning CssClass or Class to any control you can just prefix the browser name followed by colon to specify the Browser Specific CSS Class.

For Example, look at the below snippet,

Read More…

How to send an email with attachment in asp.net 2.0

I have seen many posts over forums asking for how to send emails in asp.net.

In this blog post, I am going to post code snippet to send email in asp.net (with / without attachment).

MoreOver, for Complete FAQ for the System.Net.Mail namespace found in .NET 2.0 (Click here for System.Web.Mail). This is an excellent resource to understand total mail functionalities in asp.net.

Check out Below Code to send email in asp.net:

Read More…

Generate Thumbnail images in ASP.NET

I needed to generate Medium (250 X 250) and small (150 X 150) size images from Large (500 X 500) size images as Product images to display in a shopping cart application.

To generate thumbnail images (for scenario I wrote above, or to display iconic Product images within GridView along with Product Details):

Read More…

Follow

Get every new post delivered to your Inbox.