Monday, August 16, 2010

How to enable a List to allow sorting on its class attributes

In my earlier post here, I had tried to convert a List to a DataView, in order to display a tabular result format in an asp:ListView control.

Once I got to display the table, I needed to sort the display based on CustomerID field of the Class “Customer”.

Thanks to Anand Malli, for having a great post on the Code Project for describing it beautifully and in a very simple, extensible manner.

Here is what I landed up doing:

Step #1: Inherited my class from IComparable

public class Customer : IComparable<Customer>
Step #2: Implement the IComparable member
public int CompareTo(Customer other)
{
throw new NotImplementedException();
}
Step #3: Add the Comparison member to the class
public static Comparison<Customer> CustomerIDComparison = delegate(Customer p1, Customer p2)
{
return p1.CustomerID.CompareTo(p2.CustomerID);
};
Step #4: Finally, getting the sort to work before setting the DataSource of the ListView.
lstCustomer.Sort(Customer.CustomerIDComparison);
And that’s it. A big thanks to Anand for sharing this information.
Technorati Tags: ,,,

Convert a List to a DataTable or a DataView

Let’s say we have a class Customer, defined as:

public class Customer
{
public string Name { get; set; }
public string Address { get; set; }
public string ContactNumber { get; set; }
public int CustomerId { get; set; }
public string Email { get; set; }
public string CustomerType { get; set; }
}
Now, let us try to instantiate an object type <List> from this class.
List<Customer> lstCustomer = new List<Customer>();

And let us fill this with some data, I add just 2:
lstCustomer.Add(new Customer { Name="myName1",Address="1234 Spring Pike", ContactNumber="123-456-7899", CustomerId=1002, Email=”abcd@cc.com”, CustomerType="New" });
lstCustomer.Add(new Customer { Name="myName2",Address="1234 Spring Pike", ContactNumber="123-456-7899", CustomerId=1002, Email=”abcd@cc.com”, CustomerType="New" });

Now the actual action, let’s convert this List “lstCustomer” to a DataTable……

//#region Convert the List to a DataTable.

List<Customer> items = lstCustomer;
var tb = new DataTable(typeof(Customer).Name);
PropertyInfo[] props = typeof(Customer).GetProperties(BindingFlags.Public BindingFlags.Instance);
foreach (PropertyInfo prop in props)
{
Type t;
if (prop.PropertyType != null && (!prop.PropertyType.IsValueType (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))))
{
if (!prop.PropertyType.IsValueType)
{
t = prop.PropertyType;
}
else
{
t = Nullable.GetUnderlyingType(prop.PropertyType);
}
}
else
{
t = prop.PropertyType;
}
tb.Columns.Add(prop.Name, t);
}

foreach (Customer item in items)
{
var values = new object[props.Length];
for (int i = 0; i < props.Length; i++)
{
values[i] = props[i].GetValue(item, null);
}
tb.Rows.Add(values);
}

//#endregion
And finally, convert the Datatable to a Dataview
//#region Convert the datatable to dataview.

DataTable dt = tb;
DataView dv = new DataView(dt);

//#endregion
You may very well extract the conversion of a List to a DataTable into a separate method and pass the List “lstCustomer” to that method. I’ll leave the exercise for the rest who are interested to do so. Also, this is not a tested code, so please, no warranties… use it at your own responsibilities and make changes as you wish.
Technorati Tags: ,,,

Friday, August 13, 2010

Setting up the Log file locations is SharePoint 2010

Health Monitoring alerts in SP 2010 are the good things to happen to us Administrators :)

I like the way now SP 2010 gives you the pop-up alerts in Central Admin, to review and mitigate the same.

The very common issues faced in the MOSS 2007 world, was that of running out of Hard Disk space on the servers. And the weird thing about these errors, at least to the developers, were that they never pointed out to the fact that these were caused due to in-sufficient HDD space. Admins could, well look into the Event logs and see some of them, talking about DB spaces getting full. But yes, as I said, in most cases, it wasn’t obvious looking at the so called “generic error page” ------ don’t remember now ---- was it something like ------ “An error has occurred in your application….”. Not sure, but it was the most hated error page for the MOSS developers :)

So here are the alerts to the rescue. But what next !!!

What should you do when you see these kind of alerts.

Okay, check the message. In high probability, it may tell you these:

- Error message

- its severity

- a short description

- maybe, a remedy

- and the server names.

So now you know what server needs your kind attention.

If the message is about the drives running out of free spaces, then probably here are what your options could be.

#1. Provide a new Logging Directory path in IIS for that server.

#2. Also update the “Trace Log” in the Unified Logging System. You may find this in Central Admin –> Monitoring –> Reporting –> Configure Diagnostic Logging.

Modify the Path “%CommonProgramFiles%\Microsoft Shared\Web Server Extensions\14\LOGS\”

to your new locations for the log files, say, “X:\SP2010LogFiles\”.

Also, do not forget to move all the existing log files that pre-existed on the old locations and were the actual cause of disk full.

Thats it, now the new logs would be created at this new location.

Thursday, August 12, 2010

Code Samples from Microsoft for SP 2010 development in VS 2010

These sample codes have been very helpful for me in learning the new SP 2010 dev environment from within the VS 2010 IDE.

These samples demonstrate the SP 2010 development projects, tools, and techniques using VS 2010.

Has a good list of projects:

- Sequential Workflows

- BDC Samples

- Custom Action

- Event Receivers

- Web Parts and Silverlight web part.

You can download them for the Microsoft site available here.

Highlight the GridView row when you mouse-hover

Recently while working on a ASP.Net MVC project, I needed to display data in a GridView of one of the Views. I also wanted to highlight each row, as I moved my mouse over the rows of the grid.

I got to the rescue with JQuery. This is awesome, it lets you do these kind of stuffs fairly easy. Think like javascript, but needs a little different syntax. This is how it got to working:

<script type="text/javascript">
  $(document).ready(function () {
    $("tr").filter(function () {
         return $('td', this).length && ! $('table', this).length
    }).css({background: "#FFFFFF" }).hover(
     function () { $(this).css({ background: "#EEFF77" }); },
                function () { $(this).css({ background: "#FFFFFF" }); }
                );
        });
    </script>


Thursday, August 5, 2010

LightSwitch Beta 1 for VS 2010 – Now available

LightSwitch Beta 1 was made available to the MSDN Subscribers on Wed Aug 18, 2010.  Also, it was announced that it will be available for Public download starting today Aug 23, 2010.

I hope to get this on my machine sometime this week and am pretty sure would be lots of fun.

Monday, July 26, 2010

Silverlight controls with a 3D appearance

Wow !!!

Yes, this is what many have reacted to this new behavior of the Silverlight Apps recently.

Codeplex has a new project on Silverlight 3D Controls for transforming the look-n-feel of the controls in your app, or more precisely, the app itself with an effect of adding more depth to its dimension.

This is achieved using a control from this library, DepthCanvas.

You have the option to set its ZDepth as well as set the Speed for the animation.

You can find this on Codeplex here.

Friday, July 16, 2010

How to install the Blend 4 Item Templates into VS 2010

Recently I was trying to create a Behavior in VS 2010. I thought I already had everything needed for this. This is what I had installed on my machine:

- Microsoft Silverlight 4 (ver 4.0.50524.0)

- Microsoft Silverlight 4 SDK (ver 4.0.50401.0)

- Microsoft Silverlight 4 Toolkit April 2010 (ver 4.0.40413.2020)

- Microsoft Silverlight 4 Tools for Visual Studio 2010 (ver 10.0.30319.156)

- Microsoft Expression Blend 4 (version 4.0.20421.0)

- Microsoft Expression Blend SDK for .NET 4 (version 2.0.20525.0)

- Microsoft Expression Blend SDK for Silverlight 4 (version 2.0.20525.0)

But, when I tried to create a “New Item” in my Silverlight project in VS 2010, I couldn’t find any installed templates for this.

But I knew, that I had been doing this in Blend and VS 2008 recently. So I jumped onto the band-wagon to look back for the installedtemplates option in the “devenv” command for Visual Studio.

So, this is what I did:

#1. Locate the “Expression Blend” folder in this location: “C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp” (your loc may be different based upon your installation). There is a sub-folder “1033”, which contains Action.zip, Behavior.zip and Trigger.zip.

#2. Copy-Paste this folder to this location “C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp” (again your loc may be different based upon your installation.)

#3. Close all the VS running environments.

#4. Start the VS command prompt from the VS Tools menu.

#5. Run the command [devenv /installtemplates]

#6 Start VS 2010, open any new/existing project, try creating/adding a new item now. You will find a new folder under “Visual C#” namely, “Expression Blend”. (Under C# because we copied from the CSharp location in step #1 above.) This will list the three installed items.

Now go ahead and start creating those Actions, Behaviors and Triggers.

Wednesday, June 23, 2010

Process explorer

I wanted something more than what today’s Windows Task Manager had to offer. Looking over the internet found this free download which seems pretty much offering something extra. So I got this.

Hope you find this as useful.

Technorati Tags: ,,,,

Wednesday, January 27, 2010

Visual Web Part in SharePoint 2010

There is a new web part template in Visual Studio 2010, that actually lets you develop and deploy user controls by packaging all in a single unit.  Lets take a look at what this single unit contains when you create the project:

image

- The “properties” and “Reference” are the similar standard folders created for all projects.

- The “Features” is a new folder that contains the configuration information for the feature.

- The “Package” is also a new folder that contains the configuration information for the WSP.

- The last folder (in my case “VisualWebPart1”, this is the name that you provide during the project creation) is the standard user control project template folder structure. It hosts a number of files: a .cs file, a .webpart file, an elements.xml file, and a .ascx file. 

So actually it creates an ASP.NET user control and the VS 2010 lets you open the designer for the user control already wrapped within the web part.

This is not very different than what we used to do earlier by creating to separate projects in a single solution – one for the web part and the other for the user control. Then the web part would actually embed the user control by issuing the command Page.LoadControl() method pointing at the user control. 

So VS 2010 has done a great job to hide a lot of tasks from the developers by bringing many pieces together at one place and packaging as a single unit for an easy deployment to SharePoint.