Saturday, December 10, 2011

XML Deserializer

XML Deserializing is great! Basically you can define any class structure, use the XML Deserializer to convert any XML to the object you defined, so long as the structures match, of course. This makes it way easy to parse XML. I just used it yesterday for the first time. Here's a great resource below:

http://msdn.microsoft.com/en-us/library/tz8csy73.aspx

In the msdn sample above, it uses an xml file, but if you want to read from an xml string, simply use the following syntax to populate your XmlReader object and the rest is the same:

XmlReader reader = XmlReader.Create(new System.IO.StringReader(xmlSTRING));

Thursday, December 8, 2011

Generic Handlers in .NET and AJAX

If you've ever come across an instance where you don't want to use UpdatePanels but you need some ajax functionality (i.e. you need to do ajax manually), generic handlers are the way to go. I could probably write an entire post on why UpdatePanels are not always a great idea but here is one alternative.

To start with, create a generic handler. This should be easy enough. In the ProcessRequest(HttpContext context) method, you want to grab your parameters from the context. Here's an example:

string url = context.Request.QueryString["url"];

Once you've done that, take your parameters, do whatever functionality you need, and the .ashx should return the value via context.Response.Write().

On the client side, use the following javascript to read the response:

function createXMLHttpRequest() {
try { return new XMLHttpRequest(); } catch(e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
alert("XMLHttpRequest not supported");
return null;
}

var xmlHttpReq= createXMLHttpRequest();
xmlHttpReq.open("GET", "your.ashx?v1=1&v2=2&etc", false);
xmlHttpReq.send(null);
var yourJSString = xmlHttpReq.responseText;

Then, take that response value and use JQuery or javascript to make whatever change you need on the front end. It's that simple! Now you are making AJAX calls from scratch, and you can feel like you know something.

Friday, November 11, 2011

SMTP 4 DEV

Have you ever run into the problem of sending test emails from a custom application? The problem is that you have to make sure your test emails don't inadvertently get sent to clients, and then you run into the question of whether you should send them to yourself.. but of course there are times when you want to make sure that emails CAN be sent to clients if you wanted to. Well, there's a great tool I just found out about called SMTP4DEV. It listens to port 25 from your machine, and captures all of the emails that get sent out from your machine and essentially intercepts them so you can open them up and look at them. It's a very easy download and install. Check it out here!

http://smtp4dev.codeplex.com/

Wednesday, October 19, 2011

Registering Client Side Script from Server-Side.. in a Web User Control.. in an UpdatePanel

If you've ever had to register client side script from the server-side, you know that in .NET 4.0, the way to do it on a normal ASPX page is to use the ClientScript.RegisterStartupScript syntax like below:

Normal ASPX Page
string Message = "Hello World!"; ClientScript.RegisterStartupScript(this.GetType(), "alert", string.Format("alert('{0}');", Message), true);

and if you need to register from within an update panel, you'll need to use a ScriptManager like the code below:

Within Update Panel
string Message = "Hello World!"; ScriptManager.RegisterStartupScript(this.GetType(), "alert", string.Format("alert('{0}');", Message), true);

However, I ran into a problem today where my above code didn't work because my UpdatePanel was within a Web User Control! What I figured out is that for the type parameter, you need the current PAGE type, not the user control type, so your code should look like this:

Within Update Panel Within A Web User Control
string Message = "Hello World!"; ScriptManager.RegisterStartupScript(this.Page.GetType(), "alert", string.Format("alert('{0}');", Message), true);

Ah the joys of updatePanels and Web User Controls. I hope this helps someone!

Friday, October 14, 2011

SVN Structure

Here's a great article on how to set up a trunk/branch/tag structure for svn.

http://ariejan.net/2006/11/24/svn-how-to-structure-your-repository


This is especially helpful when you need to do parallel development, or if you're in a state where you are developing new code while bug-fixing existing code, while still having to do releases.

Thursday, October 6, 2011

How to Change Your Default Browser in Visual Studio

Here are the steps to change your default browser in VS2010. I had to do this today so...

1) Open a WebForm file in VS (anything ending in .aspx will do)
2) Select the "Browse With..." option from the File menu
3) Select your preferred browser from the list and click the "Set as Default" button

Voila!!

SQL Source Control

I'm sure there are others out there that have had this problem with their database. Here's the scenario:

With your code you have a nice code repository set up. You are able to check in code, get latest, merge code, keep a nice version history with comments, etc. but when it comes to the database, all of that is a little harder to manage. Maybe you keep your scripts in SVN, but then you have to modify them in SVN before you update the server, make sure it compiles, commit the script in SVN, etc. but it's not all in one neat little IDE as it is with your code. It takes a few more steps to get all of the benefits of having a code repository when it comes to dealing with the database.

In comes Red-Gate's SQL Source Control. We just downloaded the trial and started using it. Setup literally took five minutes, and now we're able to use source control directly in SQL Management Studio!! It's quite brilliant if you ask me.

Check out the link below and watch the funny animated video if you'd like a quick overview of how it all works.
http://www.red-gate.com/products/sql-development/sql-source-control/

*Here's a direct link to the promo video, which I thought was quite clever:
http://www.red-gate.com/products/sql-development/sql-source-control/assets/video/SQL-Source-Control-Animation2/SQL-Source-Control-Animation.swf

Wednesday, October 5, 2011

Creating a DB project based off of SQL Server

If you ever need to create a database project based off of an existing database, here's a link that shows you how to do it very quickly and easily with VS 2010.

Link

You may want to do this if you want to save your schema and things like that in source control.

Another neat advantage to creating a database project is that you can actually compile it and it will show you any errors you may have buried deep in your database. We did this recently on one of our databases and realized that we had a few Views that were no longer being used, and that referenced column names that no longer existed. Without using a DB project and trying to compile, those views probably would have sat on that database untouched for the rest of time.

Monday, October 3, 2011

SVN Setup

I'm setting up a brand new dev environment, and we use SVN here. I found myself referencing this blog from a friend of mine, Greg Galipeau which highlights while file types he likes to exclude from SVN settings. Check it out:

http://www.greggalipeau.com/?s=svn&x=0&y=0

Tuesday, September 27, 2011

URL Routing

URL Routing is so much easier in .NET 4.0. A quick overview of routing is basically where you would pass values to a folder structure instead of parameters in the url string.

For example:
http://website.com/food/burgers

instead of:
http://website.com/food/foodItemCateogry.aspx?CategoryID=3

The first example is much nicer for SEO reasons, makes it easier to remember, and also makes the site a little less "hackable". I don't really know how else to explain it but here's an article that explains it all.

http://msdn.microsoft.com/en-us/library/cc668201.aspx

I recently had to implement this on a site I was working on and it worked great, and it was SUPER easy in .NET 4.0.

Thursday, September 22, 2011

Using JQuery to Crop Images

Below is a great article I found on how to use jquery and jCrop to crop images. I recently had to use this and it turned out great. The code itself was very easy to implement and I had cropping/uploading in a matter of hours.

http://www.mikesdotnetting.com/Article/95/Upload-and-Crop-Images-with-jQuery-JCrop-and-ASP.NET

A Trick to Deal With Image Cacheing

Let's paint a very common scenario. Suppose you have to upload images to a folder for your web application. You upload your image and it looks great. Then, suppose you have to upload an additional image, and you want to name it the same thing. The problem you'll inevitably face is that because of how browsers cache images, it's very likely that you see your old image again, even though you know for certain that it's been replaced by your new image in the folder. This can get quite frustrating!! Well, there's a very easy workaround to this. All of you have to do is append a random string to the imageUrl, and the browser will think of it as a different image, and not display the cached image. Here's an example, of an image I have displayed in a DataGrid:

protected void AdminGrid_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{

System.Web.UI.WebControls.Image imgPreview = (System.Web.UI.WebControls.Image)e.Row.FindControl("imgPreview");

Random rand = new Random();
imgPreview.ImageUrl = "../Images/Products/" + product.ImageName + "? rand=" + rand.Next().ToString();

}
}


I use the Random class to generate my random string which I append to my imageUrl, and now cacheing will never apply to this particular image. Easy peasy, lemon squeezy.

Tuesday, September 20, 2011

Great LINQ Examples

I found a great reference site with LINQ statements on it. I check here often for samples when I'm trying to figure out how to do something.

101 LINQ Samples

By the way, I LOVE LINQ. LINQ will give you intellisense on your queries, and it allows you to do your filtering and sorting in memory. I've also found that developing in LINQ is much faster because you end up going back to the database less, and focus on your code more. Critics of LINQ will say that LINQ adds additional overhead and will cause your performance to suffer, which is probably true, but in most cases the difference is marginal, and for me, the benefits outweigh the cost. Like any tool or technology out there, the key is understanding when to use what, and not just blindly saying that something sucks or something is great for everything. This is rarely the case.

Monday, September 19, 2011

Using ASP.NET Membership Tables

ASP.NET Configuration page couldn't read security settings, wha???

I had this problem after re-creating a database that I built, that happened to use the ASP.NET membership tables. For some reason, when using ASP.NET Configuration to manage my roles and security settings, the configuration page had trouble reading the fresh schema I just generated. It turns out, it's because I need the following values in aspnet_schemaversions:

Feature | CompatibleSchemaVersion | IsCurrentVersion
common 1 1
health monitoring 1 1
membership 1 1
personalization 1 1
profile 1 1
role manager 1 1

These values are usually entered into the membership tables by default when you generate them the first time, using .NET's scripts! This is helpful to know.

Profile Customization

I had a lot of trouble trying to figure out why I couldn't add profile properties to my user registration in ASP.NET 4.0. All of the blogs and searches I found said it was easy - all you have to do is add a few tags to your web.config, and then a custom class should be generated for you on build, and "voila!", so easy!

The problem was, no matter how I messed with the web.config to add those profile property tags, it seemed like I could never see this auto generated custom class. I dug a little deeper and found out that the reason why I couldn't use this feature, is because it doesn't work with Web Projects!! It only works with ASP.NET Web Sites. This is a distinction that many people overlook. Anyhow, there is a way to do it, and I've posted the very helpful link below.

http://weblogs.asp.net/jgalloway/archive/2008/01/19/writing-a-custom-asp-net-profile-class.aspx