November 2006 - Posts

If you have to work with mapping data to objects you know what a pain working with nulls has been historically. In the past, your database can deal with nulls very easy but your object model could not. As developers and architects, we would have to come up with “schemes” to deal with a null. It was a total pain in the aspirin bottle.  Now, with the Nullable Types in .NET, it is much better situation, though it is still a bit tricky when dealing with Nullable Types and generics. The two tricky things that I need to mess with are knowing if a type is “nullable” and what the heck is the “underlying” type so I can do type conversions (mainly for DLINQ expression building.) So here is some code that may help.

If you need to know if a type is Nullable or not:

bool IsNullableType(Type theType)

{

   return (theType.IsGenericType && theType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)));

}

You will find in DLINQ when creating dynamic expressions, that you may need to convert types, and often they are nullable.

Converting to the underlying type:

// UnderlyingType will equal System.DateTime

var nc = new System.ComponentModel.NullableConverter(Nullable<DateTime>);

Type underlyingType = nc.UnderlyingType;

Note: I totally stole this info from David Hayden because, as you know, plagiarism is the most sincere form of flattery.

Forget what I said earlier... get this:

http://www.jtleigh.com/people/colin/software/CopySourceAsHtml/

This is a much better solution.

For example:

        public IQueryable<Property> SecuredProperties

        {

            get

            {

                IQueryable<Property> q;

 

                //if the user is an admin, bypass security

                if (_Service.CurrentUser.IsAdmin)

                    q = from o in this.Organizations

                            from p in o.Properties

                            where

                                p.Deleted == 0

                                && o.Deleted == 0

                            select p;

                else

                    //get properties based on security

                    q = from o in this.Organizations

                            from vp in o.ViewPermissions

                            from p in o.Properties

                            where

                                vp.UserGUID == _Service.CurrentUser.Id

                                && vp.ObjectType == 105

                                && vp.View == 2

                                && p.Deleted == 0

                                && o.Deleted == 0

                            select p;

 

 

                return q;

 

            }

        }

Posted by wwright | 1 comment(s)

Have you ever had some horrific URL that you needed to email, tell someone over the phone or just type yourself? Wouldn’t it be nice if instead of

http://www.nddnug.net/Meeting.aspx?ID=fa92013d-9d75-43b5-b611-55ce3ec1ea18


you could just have a little URL http://www.nddnug.net/scottgu.aspx ?  


All it takes is a little magic in the Web.Config:

    <system.web>

      <urlMappings enabled="true">

        <add url="~/scottgu.aspx"

             mappedUrl="~/Meeting.aspx?ID=fa92013d-9d75-43b5-b611-55ce3ec1ea18" />

        </urlMappings>

    </system.web>

Ps. Thanks to Irena Kennedy for pointing out this little tidbit.

Have you ever needed to convert a document, a pic or some other digital doo-hickey and you just don't have the software on hand to do it. I know I have been working on some website for a client, and invariably they send me something in a digital form that is useless as is and I just need to convert it into something else, but alas, I just formatted my box or have some other hokey reason that I don't have the software handy and so I have to dig through the pile of install cd's to find the app, install it… where is that freaking product key??? Well now I can just Zamzar it! They do all kinds of file conversions…for free… just upload it to them, and they email you a link to the converted file!  Now I can finally be free of that cursed QuickTime!!!

http://www.zamzar.com/ 

 

 

Posted by wwright | with no comments
Filed under: , ,

If you are like us, you get someone else to do your skins for your website. When doing so, they may be unaware of how skinning, themes etc work in the ASP.NET world. As a matter of fact, the better they are, the less they will know about it. As I work through implementing the unbelievable sweet skin that we hired XHTMLIZED.COM to create for us, I have found a few issues that I will post here so that I can give better instructions to the next skinner, which will most likely be xtmlized.com.

1)       Don’t create a “label” css class as it interferes with .NET labels… especially when you set it’s left location to -99999.99

2)       If you are using stylesheets and themes, know that if you have “url(../images/someImage.jpg)” in your stylesheet, it doesn’t point to where you think it does. If your stylesheet is in your themes dir, it is relative to your themes dir.

3)       There are a lot of problems if your CSS uses “ID” to signify the style it needs, as obviously this has meaning in an ASP.NET control. Shane Henderson had a good idea. He suggested renaming all of the “#”’s to “.”’s so that they are all classes in the stylesheet.          

4)       If your CSS uses “LI” items for your menus, and you need to hide certain menu items, if you place your items in a “PlaceHolder” control you can turn em on and off with code by setting the visible property of the PlaceHolder. (Note: you can’t just set the visible to false on the Hyperlink as the “LI” tag still exists and will muck with your layout)

In your markup:

                    <asp:PlaceHolder runat="server" ID="plhAdmin">

                    <li><asp:HyperLink runat="server" ID="hlUsers" Text="Users" NavigateUrl="~/Admin/Users.aspx"></asp:HyperLink> </li>

                    <li><asp:HyperLink runat="server" ID="hlRoles" Text="Roles" NavigateUrl="~/Admin/Roles.aspx"></asp:HyperLink> </li>

                    <li><asp:HyperLink runat="server" ID="hlSubscriptions" Text="Subscriptions" NavigateUrl="~/Admin/ManageSubscriptions.aspx"></asp:HyperLink> </li>

                    </asp:PlaceHolder>

And in your Code:


        bool bIsAdmin = Page.User.IsInRole("Admin");

        plhAdmin.Visible = bIsAdmin;



Posted by wwright | 1 comment(s)
Filed under: , , ,

Check out Process Monitor! If you ever needed to know what process was accessing what, this is a good start.

http://www.microsoft.com/technet/sysinternals/processesandthreads/processmonitor.mspx

Posted by wwright | with no comments
Filed under: ,
Posted by wwright | with no comments
Filed under: ,

We have all seen it… had it… cursed it. Now you can have it for a screen saver!

But don’t think this is just a screenshot kinda thing. This shows actual things about your system making it look VERY legit. There is also an option for “fake disk activity.”  Now…if I could just get this on workstation of my boy Shane Henderson.

http://www.microsoft.com/technet/sysinternals/Miscellaneous/BlueScreen.mspx

Posted by wwright | 1 comment(s)
Filed under: ,

LINQ enables developers to query data sources directly out of code with a SQL like query language. You could write

to get all customers out of a table.

This new way of queries is only possible because of several new language features in C# 3.0, which Anders explained in his 75 min. session:

  • Local Variable Type Inference
    The compiler automatically substitutes the var keyword with the corresponding type.
    So you could write a code like this:

var a = 5;
var b = a + 5;
Console.WriteLine(b);
Console.WriteLine(b.GetType());

The compiler will automatically detect, that a and b are of type Int32.

  • Extension Methods
    All types can virtually be extended by custom methods. If you specify a static method in a static class, which has the first parameter marked with the this keyword, the type of the parameter will be extended with the method.

s = "Hello World";
s.WriteToConsole();
var s2 = s.Concatenate(
"!");
...
public static class
ExtensionClass {
    public static void WriteToConsole(this string st) {
        Console.WriteLine(st);
    }
    public static string Concatenate(this string st, string st2){
        return st + st2;
    }
}

The this marked parameter specifies the type which should be extended and is hidden when calling the method throught the type. All other params can be used to pass additional data to the extension method.

  • Lambda Expressions:
    You probably know the FindAll(..) method of Generic-List, where a predicate delegate is passed. This delegate specifies the condition of the search.
    In .Net 2.0 you could use the following statement to get all Int32 greater than 8 out of a list, by using an anonymous method.

List<int> list2 = list.FindAll(delegate (int param){ return param > 8;});

With Lambda Expressions this paradigm is advanced, so that you can specify the condition directly in a particular syntax:

var list2 = list.FindAll(i => i > 8); // all i, where i > 8

  • Object Initializers:
    It's possible to initialize type directly at declaration. This can be done by either setting properties or adding elements into an IEnumerable list (like already with array).

var list = new List<int> {1,2,3,4};
var point = new Point() {X=1, Y=1};

  • Anonymous Types:
    When executing database queries you get different columns back in a recordset. As developers we are generally lazy.. Why create different classes just for storing different database columns? Let the compiler do the work! By instantiating a new type without a type name, but using an object initializer the compiler will create a matching type for you on the fly.

var person = new {Name="Max", Age=23};
Console.Write("Name: {0} ({1})", person.Name, person.Age);

You'll be able to access person just like a regular object, but it will have a "unspeakable" name. You can only access it by using the var statement.

  • Automatic Properties:
    Same thing... hack for lazy devs.. How often have you specified a default property accessor for a field. Now you can do it by simply specifying it as followed:

public string Name { get; set; }

This will be equivalent to:

private string _name;

public string Name
{
   get { return _name; }
   set { _name = value; }
}

Besides there are also lots of other cool new features in C# 3.0 (Expression Trees, Partial Methods, ...).
If you want to play arount a little bit with C# 3.0 + LINQ just download the
CTP here.

BTW: This isn’t mine…I stole… err… copied it directly from:

http://blogs.msdn.com/knom/archive/2006/11/13/anders-hejlsberg-about-c-3-0-linq-at-teched-europe-in-barcelona.aspx

Posted by wwright | with no comments
Filed under: , , ,

Since I have begun my DLINQ bender I have spent a lot of time searching for code examples and the like. Now it will be a little bit easier.

http://www.searchaspnet.net/

Posted by wwright | with no comments
Filed under: , , , ,
More Posts Next page »