Skip navigation

Category Archives: Asp.net (C#)

Articles that are strongly related to Asp.Net utilizing the C# programming language.

I am working on a project currently where I wished to share non secure data across domains using Javascript and JSON objects.

After several hours of tinkering and researching I finally was able to figure out a solution which works in the normal .NET framework.

Read More »

Short and Sweet.

I have a project at work right now where I needed to generate a unique id for an object based on the parent repeaters ItemIndex.

I found this bit of code which worked beautifully

<ItemTemplate>
<%# ((RepeaterItem)Container.Parent.Parent).ItemIndex %>
</ItemTemplate>

I’m working on a project right now where I have an XML data source that is provided by a third party and I need to import the data into a usable object model which I plan to store in MongoDB.

I didn’t want to have to write a ton of redundant code for each and every field on the object in a factory pattern. There is literally about 40 to 50 fields on this object.

What I discovered is this cool thing called reflection.

Here is the code snippet:

var name = "PropertyName";
var newValue = "This is my value I want to set";

PropertyInfo prop = obj.GetType().GetProperty(name, BindingFlags.Public | BindingFlags.Instance);
if (null != prop && prop.CanWrite)
{
      prop.SetValue(obj, Convert.ChangeType(newValue, prop.PropertyType), null);
}

Essentially the first two lines here are ensuring that firstly the property exists on the object and secondly that the property is settable.

Since all the properties on my object match up to fields on my datasource, at least the fields I want to translate over do, this works great.

Here is the method I wrote that allows me to pass an XElement with a Generic and iteratively populate all the fields of the object from values nested within the XElement.

private static T BuildByReflection<T>(XElement element) where T : new()
{
   var obj = new T();
   foreach (var subElem in element.Elements())
   {
      var name = subElem.Name.LocalName;
      PropertyInfo prop = obj.GetType().GetProperty(name, BindingFlags.Public | BindingFlags.Instance);
      if (null != prop && prop.CanWrite)
      {
          prop.SetValue(obj, Convert.ChangeType(subElem.Value, prop.PropertyType), null);
      }
   }

   return obj;
}

I mentioned this cool little method in my previous post and felt it was worth an explanation.

Up until recently when I wanted to sort a generic list of objects I used the methods I outlined back in June of 2011.

Recently using LINQ I’ve discovered a much more compact code method:

.OrderBy();

Let’s assume we are using our old CoffeeMaker class…

class CoffeeMaker
{

[...]

public int ID;
public string Name;
public decimal Price;

}

And we have a list of CoffeeMakers that we want to sort on the Name.

We simply do the following:

List cMakers = GetCoffeeMakers();
cMakers.OrderBy(cm => cm.Name);

Much shorter for scenarios where you wont be reusing the sort routine frequently and the use doesn’t dictate the neccessity of implementing the full IComparable interface.

At work we constantly struggle with timeouts on our data sources no matter if they are sqldatasource or tableadapter.

As I understand it from my research that is because .Net applications utilize their own timeout settings and while you may have timeouts set on your SQL Database the .Net timeouts are relatively short duration, most defaulting to 30 seconds. With the sheer volume and complexity of some of the queries we have to run, this simply is too short. We sometimes need another 15 to 30 seconds to prevent timeouts.

Read More »

I found myself recently with the need to be able to create System.Drawing.Color objects that were shades of a specific color in terms of “lighter” or “darker”. For those familiar with color theory you may be aware that the best color space to easily manipulate that information falls within HSL where the values of L are the H and Value in relationship to Black and White. Read More »

This is a list of the code conventions I try to adhere to in my own perosonal programs.  This is based on industry standards and my own personal preferences.
Read More »

I had a pretty discouraging run in today with the Page Life Cycle and this new paradigm of ASP.NET called Control Bubbling.  Something that I did not realize in my lucky initial implementations was that the declaration of the event handler in the actual page.

Read More »

One of the coolest features in Asp.Net in my own opinion is the ability to packaged code snipets into custom controls which you can then reuse through out your project.

If you get into this very deep at all you will find that as you try to utilize controls to their fullest potential you quickly come to the point where you need to communicate between a control and the page the control is nested in.  Specifically you need to handle an event that is fired within the custom control on the page the custom control is nested in.  The technical term that I have come to know this process by is called Bubbling.

Read More »

Follow

Get every new post delivered to your Inbox.