Monday, October 27, 2008

Hotmail Embedded Images in HTML email not working

With one of my recent projects going live I experienced an issue with hotmail email client not showing the emails sent by my application correctly. Especially embedded images in the email. These images were replace by a gray box "i_safe.gif" making the email look bad. This was only happening in hotmail. Again Microsoft had to be special.
Gmail, Yahoo, Exchange Outlook etc was rendering the email perfectly fine.

Hotmail just need to have additional information in the email LinkedResource object.

Setting the properties LinkedResource.ContentType.Name and LinkedResource.TransferEncodig fixed this problem.

backImageResource.ContentType.Name = "dress_back";

backImageResource.TransferEncoding = TransferEncoding.Base64;
Here is my code example that work the emails working.








// Create the html AlternateView

AlternateView htmlView = AlternateView.CreateAlternateViewFromString(template.Body, null, "text/html");

// Add Linked Resources for the embedded images

LinkedResource frontImageResource = new LinkedResource(Utility.GetImageStream(DressFrontView, ImageFormat.Jpeg), MediaTypeNames.Image.Jpeg);

frontImageResource.ContentId = "dress_front";

frontImageResource.ContentType.Name = "dress_front";

frontImageResource.TransferEncoding = TransferEncoding.Base64;



LinkedResource backImageResource = new LinkedResource(Utility.GetImageStream(DressBackView, ImageFormat.Jpeg), MediaTypeNames.Image.Jpeg);

backImageResource.ContentId = "dress_back";

backImageResource.ContentType.Name = "dress_back";

backImageResource.TransferEncoding = TransferEncoding.Base64;





// Relate resources with AlternativeView

htmlView.LinkedResources.Add(frontImageResource);

htmlView.LinkedResources.Add(backImageResource);






After adding the ContentType.Name and TransferEncoding the e-mails displayed corrently in hotmail.

Friday, October 24, 2008

Flash uploading large files to ASP.NET: HTTP 500 Error

If you are having trouble uploading large files via flash or standard asp.net form its probably because there is a max request length set in the machine.config file. Max request length by default is set to 4MB.

When exceeding the max request length you get a HTTP 500 error code.
If you check the Application Event viewer on the server side you might see errors such as this:

Event code: 3004
Event message: Post size exceeded allowed limits.
Event time: 10/24/2008 5:11:39 PM
Event time (UTC): 10/24/2008 10:11:39 PM
Event ID: 640ae5c3a03349b18112b5289fd09989
Event sequence: 12
Event occurrence: 3
Event detail code: 0




To solve this problem override your web.config with the following configuration element.

Also update the executionTimeout to allow more time before it shuts down the request.



<configuration>

<system.web>

<httpRuntime maxRequestLength="11000" executionTimeout="60" />

<!-- 50MB max request size-->




More information can be found at the MSDN site

http://msdn.microsoft.com/en-us/library/e1f13641(VS.71).aspx

IE7 very picky with Javascript & Json: IE counts null objects.

I just came across with another debugging situation with javascript code in IE7.
IE7 takes in consideration null objects.

For example, below is a JSon array of objects.







var markers = [
{
'abbr': '1',
'latitude':33.029764,
'longitude':-97.090090,
'dealer': {
"companyName": "Armstrong Office Interiors","address1" : "3205 Dwyer St","city" : "Flower Mound","state" : "TX","website": "www.armstrongofficeconcepts.com","phone" : "469-568-6648", "address2": "", "zipCode": "75022" , "fax": "(469) 293-6602" , "email": "jarmstrong@armstrongoffice.com" , "dealerType": 1 , "distance": "3.0" , "showcase": "N" , "locationId": "19830" , "contactName": "Armstrong Office Interiors" , "gsa": "Y"}
,
'wp':'n'
}];










In this case as you can see there is only 1 object.
If you do markers.length, you get a total of 1. And that's correct.

No lets say we add a comma "," to the end of the first object.







var markers = [
{
'abbr': '1',
'latitude':33.029764,
'longitude':-97.090090,
'dealer': {
"companyName": "Armstrong Office Interiors","address1" : "3205 Dwyer St","city" : "Flower Mound","state" : "TX","website": "www.armstrongofficeconcepts.com","phone" : "469-568-6648", "address2": "", "zipCode": "75022" , "fax": "(469) 293-6602" , "email": "jarmstrong@armstrongoffice.com" , "dealerType": 1 , "distance": "3.0" , "showcase": "N" , "locationId": "19830" , "contactName": "Armstrong Office Interiors" , "gsa": "Y"}
,
'wp':'n'
}
, // Added extra comma here.
];









run markers.length again and now you will get a count of 2.
In firefox you will still get a count of 1.

Wednesday, October 15, 2008

OnItemCommand event does not fire on Repeater

After breaking my brain into species figuring out why the OnItemCommand event was not firing for my repeater, I finally come to very simple & stupid conclusion.

You need to rebind the repeater on the Page_Load event every time.
I was only binding the repeater if(!Page.IsPostBack) { // Bind Repeater }...

Binding the repeater in the Page_Load event took care of the issue.





<asp:Repeater ID="repAvailableFilters" runat="server" OnItemCommand="repFilters_ItemCommand">

<HeaderTemplate>

<ul class="style-1">

</HeaderTemplate>

<ItemTemplate>

<li>

<asp:LinkButton ID="lbtnAddFilter" runat="server" CommandArgument='<%# Eval("ID") %>'

CommandName="AddFilter" ><%# Eval("Name") %></asp:LinkButton>



</li>

</ItemTemplate>

<FooterTemplate>

</ul>

</FooterTemplate>

</asp:Repeater>


Tuesday, October 14, 2008

ToString() Number format

To write a number in a formatted numeric format. Use the 'N' argument in the ToString() method.







int n = 1000;

Console.WriteLine(n.ToString("N"));

// Will produce

1,000.00

// If you don't decimals
Console.WriteLine(n.ToString("N0"));

// Will produce
1,000



Monday, October 13, 2008

Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.

I ran across this error when trying to export the grid view data to excel.
I do have a form tag setup runat=server. But it continue to fail.

Solution: Create an HtmlForm object and add the grid view to the form in code.





string style = @" ";

Response.ClearContent();

// Create form tag

HtmlForm form1 = new HtmlForm();

form1.Controls.Add(gvData);


Response.AddHeader("content-disposition", "myfilename.xls");

Response.ContentType = "application/excel";

StringWriter sw = new StringWriter();
StringWriter header= new StringWriter();


HtmlTextWriter htw = new HtmlTextWriter(sw);

gvData.RenderControl(htw);


// Style is added dynamically

Response.Write(style);

Response.Write(sw.ToString());

Response.End();



Tuesday, October 07, 2008

Delete svn folders /files Recursive Subfolders batch file

Create a batch file with the following code to remove all svn files + folders from your trunk.





FOR /F "tokens=*" %%G IN ('DIR /B /AD /S *.svn*') DO RMDIR /S /Q "%%G"