ObjectDataSource: defining a class as a data component

Posted on: Tue Aug 24 14:49:45 -0700 2010. Updated on: Tue Aug 24 14:52:12 -0700 2010.
Category: DotNet

When you create a new object data source and you point it to a class, have you noticed the checkbox next to the dropdown that says only show data components? There is an easy way to make your classes show up if that is checked. Here's how:

[DataObjectAttribute]
public class MyDataClass
{   

// optionally give a method type
[DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
  public static List GetAllPeople()
  {
// return your data
}
}

For more info: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataobjectattribute.aspx

Send Email with auto merge in html

Posted on: Fri Aug 06 23:13:52 -0700 2010. Updated on: Fri Aug 06 23:13:52 -0700 2010.
Category: DotNet

The .net built in MailMessage supports merging replacements built in. It also supports building a message both in HTML and plain text in case the viewer does not support html. All this out of the box. Here is a great article that gives a good example that works: http://www.search-this.com/2009/02/05/aspnet-sending-email-both-in-html-and-plain-text/

Using the application Web Cache

Posted on: Sat Jun 05 17:09:31 -0700 2010. Updated on: Sat Jun 05 17:13:22 -0700 2010.
Category: DotNet

It is a good idea to use caching for frequently accessed objects to reduce trips to the database or your web services. Here is an example of using the System.Web application cache. You can even use this in dll's outside of your web project, as long as you reference System.Web.

string cacheName = "objectNameCache";
int cacheDurationMinutes = 10;
var cache = System.Web.HttpRuntime.Cache;
			
cache.Insert(cacheName, quote, null, DateTime.UtcNow.AddMinutes(cacheDurationMinutes), TimeSpan.Zero);

// now to access it... First check if null, then simply cast cache to outcoming object.
if (cache[cacheName] != null)
{
    quote = (DataTransferObjects.QuoteData)cache[cacheName];
}

Get Enum from matching string

Posted on: Wed May 19 13:19:08 -0700 2010. Updated on: Wed May 19 13:19:08 -0700 2010.
Category: DotNet

Getting the enum from the int representation of it is pretty easy, but you can't do the same kind of cast when you want to go from a string representation to the enum.

this link explains it weill: http://blogs.msdn.com/tims/archive/2004/04/02/106310.aspx

Deploy VS Database Projects workaround

Posted on: Tue May 18 12:27:06 -0700 2010. Updated on: Tue May 18 12:27:06 -0700 2010.
Category: DotNet

I like using the Visual Studio Database Projects to keep track of my databases in a particular project. The thing is, when deploying a brand new database, using it can be a bit not cool because there is no Deploy button. You have to go into the project properties, change the deploy action to actually push to the database, then you have to make sure the connection to the database works, and if you're using source control, you probably do not want to check in a build that deploys to the database every time.

So the workaround is:
1. Right click the project, and hit deploy, of course without pushing to the DB.
2. Look at the output window in VS at the bottom, and copy the location of the deploy script generated
3. Open up SQL Server management studio and hit File -> Open File and paste in the path.
4. Be sure you are in the right DB instance, and now you'll need to do some text replacing. Replace $(DatabaseName) in the whole document for you db name.
5. Then delete the lines of code at the top that have the colen :
6. Now hit run

Response.Redirect open new window

Posted on: Thu May 06 18:27:48 -0700 2010. Updated on: Thu May 06 18:27:48 -0700 2010.
Category: DotNet

Sometimes we need a button action or something during the postback to pop open a new window / tab using the very known Response.Redirect. This is easy to do, simply add OnClientClick="aspnetForm.target ='_blank';" in the button's property.

Find out more at this site: http://dotnetchris.wordpress.com/2008/11/04/c-aspnet-responseredirect-open-into-new-window/

Check in Code if in Debug or Release mode

Posted on: Fri Apr 30 11:28:22 -0700 2010. Updated on: Fri Apr 30 11:31:18 -0700 2010.
Category: DotNet

In c sharp it is easy to check if the code being executed is currently being run in debug or release mode:

#if DEBUG
// code goes in here
#else
// other optional code
#endif // dont forget this!

You can also use the RELEASE keyword to check if you are in release build mode.

Web page Performance: Asynchronous operations

Posted on: Wed Mar 10 16:14:13 -0800 2010. Updated on: Wed Mar 10 16:14:13 -0800 2010.
Category: DotNet

Do you have any postbacks in your web site that take the backend more than 1 or 2 seconds? If you are not sure, you can enable Trace in your web.config or use Fiddler to see what's going on. The thing is, for every long lasting postback, the process thread in IIS is locked. IIS only has a certain number of threads in the Application Pool, depending on your configuration. So, if you have a site with many requests, performance would start to suffer when those thread pools max out constantly.

The solution is using Asynchronous operations. This involves setting the Async="true" in your page directive at the top as well as using web services on the codebehind.

Here is a good article that explains how to do this. Scroll down to the bullet with Asynchronous operations and download the sample source. HIgh Performance ASP.NET applications

WCF REST Starter Kit Consumer quick example

Posted on: Fri Mar 05 16:13:29 -0800 2010. Updated on: Fri Mar 05 16:19:56 -0800 2010.
Category: DotNet

Here I will summarize some quick points about using the WCF Rest Starter Kit P2 to consume REST services. If you are not familiar with it, here is a good article from msdn: http://msdn.microsoft.com/en-us/library/ee391967.aspx

// quick code example .net accessing shopify api
private orders InitialOrderSync(string appURL, string apiKey, string pwd)
		{
// note here we can use http or https
			HttpClient client = new HttpClient("https://" + appURL);
// set optional credentials
			client.TransportSettings.Credentials = new NetworkCredential(apiKey, pwd);
			// the uri of the REST path to get, can also pass in parameters 
			HttpResponseMessage message = client.Get("admin/orders.xml");
			message.EnsureStatusIsSuccessful(); // make sure all ok
                        // serialize into object
			orders shopifyOrders = message.Content.ReadAsXmlSerializable();
			return shopifyOrders;
		}

You may be wondering how did I build the orders object based on the xml. This is very easy. Just copy the xml you will be serializing from start to finish, go into Visual Studio that has wcf rest kit installed, add a new Class, and go menu Edit -> Paste as XML Types. That's it.

As far as using requirements, add references to your project to all 3 binaries in the WCF REST kit installation directory folder.
in your code you'll need these guys for above to work:

using System.Xml.Serialization; using System.Xml; using System.Runtime.Serialization; using Microsoft.Http; using Microsoft.ServiceModel; using System.Net;

Page Properties with Viewstate done right

Posted on: Thu Feb 25 11:55:03 -0800 2010. Updated on: Thu Feb 25 11:56:41 -0800 2010.
Category: DotNet

Here is an example of having page properties that hold their value in viewstate so that when the same screen posts back but in a different mode, ie. multistep processes, you can still hold the value. Also note we put the value into a local variable as well, because the viewstate will only be set after the postback, so therefore if you tried to access the property in the same request, you won't get a null back.

public partial class WizardSignup : System.Web.UI.Page
	{
public int? PhysicalDockId
		{
			get
			{
				if (ViewState["PhysicalDockId"] == null && _physicalDockId == null)
					return null;
				else
				{
					return _physicalDockId ?? (int)ViewState["PhysicalDockId"];
				}
			}
			set
			{
				ViewState["PhysicalDockId"] = (_physicalDockId = value);
			}
		}
		private int? _physicalDockId = null;
}

Ads

Categories

About

Random foliage

This website is meant to be a reference for ASP Dot Net developers. The entries are a compilation of things I've figured out how to do and that I deem useful to keep of track for future reference. Assumptions: web development with: C Sharp (vb sucks), visual studio 05/08, .net 3.5, meant for programmers. Written by: James Reategui.