Monday, November 30, 2009

Difference between System.DateTime.Now and System.DateTime.Today

Per Microsoft:
Because it returns the current date without the current time, the Today property is suitable for use in applications that work with dates only. For details, see Choosing Between DateTime, DateTimeOffset, and TimeZoneInfo. In contrast, the TimeOfDay property returns the current time without the current date, and the Now property returns both the current date and the current time.

Wednesday, November 25, 2009

How do you add drop down items to a drop down which uses a datasource

Ran into this the other day. I'm filling a drop down (combo box) from the code behind using a datasource. But, the data source of course doesn't include a default or empty value for the top of the list. So, what do you do?

What you can do is on the aspx file add the attribute AppendDataBoundItems="true".
Once you have added this attribute to the drop down (combo box), you can add a new drop down item to the combo box.
Example:

Monday, November 23, 2009

does not contain a definition for 'RadGridTradeOrders_NeedDataSource' and no extension method 'RadGridTradeOrders_NeedDataSource'

I fixed this by going into the code behind of the aspx file and removed the line: onneeddatasource="RadGridTradeOrders_NeedDataSource"

I think this occurred because I was editing the a radgrid control by changing the data field name. Because my datasource was dynamic, changing this field caused the attribute above to be added automatically.

NOTE: I also believe that making custom changes in the source code (aspx) for this control causes unnecessary issues. Therefore, I would recommend using the design view to set properties on the telerik controls.

Sunday, November 22, 2009

ASP.NET RSS Toolkit

Research: "ASP.Net RSS Toolkit gives ASP.Net applications the ability to consume and publish to RSS feeds.


Features include:
RssDataSource control to consume feeds in ASP.NET applications
Works with ASP.NET data bound controls
Implements schema to generate columns at design time
Supports auto-generation of columns at runtime (via ICustomTypeDescriptor implementation)
Caching of downloaded feeds both in-memory and on-disk (persisted across process restarts)
Generation of strongly typed classes for feeds (including strongly typed channel, items, image, handler) based on a feed URL (the toolkit recognizes RSS, Atom and RDF feeds) or a file containing a sample feed. Allows programmatically download (and creation) of feeds using strongly-typed classes."

Reference: ASP.NET RSS Toolkit

RssFeed

Research: "RssFeed is an open source custom ASP.NET server control that displays the contents of a specified RSS feed in an ASP.NET web page. This control was developed by Scott Mitchell in October 2003. You can see RssFeed in action at the Recommended Blogs section on 4GuysFromRolla.com. There are also some live demos of RssFeed with complete source available."
Reference: RssFeed

ASP 101 - An ASP.NET RSS Feed Reader

Research: An ASP.NET RSS Feed Reader
If I was going to go through the trouble of doing this in ASP.NET, I was going to do it right and, while it is possible, I didn't want to call the COM objects that we used the first time around. This is .NET after all so naturally I wanted to use the .NET Framework's XmlDocument object. Aside from using the .NET object, this part of the script is basically the same. We create an XML object and load an XML file into it either from the remote server or from the file system."
Read More: ASP 101 - An ASP.NET RSS Feed Reader

Reading RSS Feeds with .NET

Research: "RSS feed reader
A custom RSS Feed Reader can be developed in ASP.NET 2.0 targeting the feed: http://localhost/RSSFeeds/EmployeeDetails.aspx
ASP.NET 2.0
1. Open Microsoft Visual Studio 2005 and create a 'Web Site' with name RSSFeedReader. After the project is created, right click on the project node and click on 'Add New Item'.
2. In the 'Add New Item' dialog box, select 'Web Form' and enter the name RSSRead.aspx in the 'Name' area and click on Add.
3. Open the 'Source' view of RSSRead.aspx."

Read More: Publishing And Reading RSS Feeds In ASP.NET

Tuesday, November 17, 2009

How do I loop through an enum

It is so easy to create enums and reuse them in multiple projects. You can use them in many places, one of the most common is to use them in drop down lists. Here, we'll show you how to create an enum, how to access the values using a foreach, then how to use an enum to fill a drop down list in c#.

First, make sure you have an enum defined:

public enum DaysOfTheWeek

{
 Monday,
 Tuesday,
 Wednesday,
 Thursday,
 Friday,
 Saturday,
 Sunday
}

Now that we have an enum, we would want to loop through it.

foreach(int value in System.Enum.GetValues(typeof(DaysOfTheWeek)))

  //To access the index (0,1,2,3,4,5,6) use the variable value stated above
  int intIndex = value;
 //To access the text of the index, get the names
  string strName = System.Enum.GetName(typeof(DaysOfTheWeek), value)
}
An example of filling a drop down list with enum values using the information above:

foreach(int value in System.Enum.GetValues(typeof(DaysOfTheWeek)))

{
  //Create the lineitem so we can add it to an existing dropdown
  ListItem liDay = new ListItem();

  //To access the index (0,1,2,3,4,5,6) use the variable value stated above

  liDay.Value = value.ToString();
 
  //To access the text of the index, get the names
  liDay.Text = System.Enum.GetName(typeof(DaysOfTheWeek), value)

  //Add it to the drop down
  ddlDaysOfTheWeek.Items.Add(liDay);

}