Thursday, May 20, 2010

Converting One Kind of Collection to Another

Getting an ObservableCollection from an IEnumerable (this is the type of loadOperation.Entities

myCollection = new ObservableCollection<myEntityType>(loadOperation.Entities)

// DON’T do this. It returns a null value

//myCollection = (ObservableCollection<myEntityType>)
//loadOperation.Entities;

Converting an iQueryable returned by EntityFramework into a List.  Note that this is an extension method, which means you must be “Using System.Linq” in order have this function available to the iEnumerable.

var myQueryable = (from x in myContext.table select x).ToList()

Friday, April 9, 2010

Using SQL to Generate XML

To create this XML block:

<EventArguments>
<OccurrenceId>300031738</OccurrenceId>
<HoursInProcess>123</HoursInProcess>
<ContactName>McCusker &amp; Ogborne &lt;Other&gt;</ContactName>
<AccidentFormId>795</AccidentFormId>
<BusinessUnitNumber>*</BusinessUnitNumber>
</EventArguments>

You would do this:

SELECT '300031738' AS OccurrenceId,
123 AS HoursInProcess,
'McCusker & Ogborne <Other>' AS ContactName,
795 AS AccidentFormId,
'*' AS BusinessUnitNumber,
FOR XML RAW('EventArguments'), ELEMENTS

-

For more sophisticated XML, you can do sub queries. This may not make sense to include this particular table, but it shows how it would work.

SELECT '300031738' AS OccurrenceId,
123 AS HoursInProcess,
'McCusker & Ogborne <Other>' AS ContactName,
795 AS AccidentFormId,
'*' AS BusinessUnitNumber,
(SELECT TOP 5 ProcessedMessageLogId FROM ProcessedMessageLog FOR XML RAW(''), ELEMENTS, TYPE) AS Children
FOR XML RAW('EventArguments'), ELEMENTS

- Which creates the following XML:

<EventArguments>
<OccurrenceId>300031738</OccurrenceId>
<HoursInProcess>123</HoursInProcess>
<ContactName>McCusker &amp; Ogborne &lt;Other&gt;</ContactName>
<AccidentFormId>795</AccidentFormId>
<BusinessUnitNumber>*</BusinessUnitNumber>
<Children>
<ProcessedMessageLogId>3281</ProcessedMessageLogId>
<ProcessedMessageLogId>3282</ProcessedMessageLogId>
<ProcessedMessageLogId>3283</ProcessedMessageLogId>
<ProcessedMessageLogId>3284</ProcessedMessageLogId>
<ProcessedMessageLogId>3285</ProcessedMessageLogId>
</Children>
</EventArguments>

Thursday, April 8, 2010

Confirming Delete in a GridView

I tried a bunch of ways but this was the only way to get this to work reliably. The problem is that the ButtonFields in the GridView don’t have an OnClientClick event the way that every other button does. To remedy this, you have to attach the ClientClick handler when the grid is being generated.

So in your Columns collection of the GridView, you will have a button field like this:

<asp:ButtonField ButtonType="Link" CommandName="Clear" Text="Clear" />

Then in the code behind you need to do this:

protected void grdCourses_RowDataBound(object sender , GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow) return;

int lastCellIndex = e.Row.Cells.Count - 1;
LinkButton clearButton = (LinkButton)e.Row.Cells[lastCellIndex].Controls[0];
clearButton.OnClientClick =
"if (!window.confirm('Are you sure you want to clear this score?')) return false;";
}

Wednesday, February 24, 2010

Concatenating Strings Across Records In SQL Server

Occasionally it is necessary to concatenate strings from a number of different records and return them as a single value in another query.   For example, in the classic invoice/detail scenario, maybe you want a list of invoices, and with each invoice, a comma delimited list of all the descriptions of each detail record.  But this detail information has to be returned as a single text field as part of the invoice.  
 
To do this you need to create a function:
 
IF EXISTS (SELECT * FROM sysobjects WHERE name = 'DetailList')
BEGIN
  DROP  FUNCTION DetailList
END
GO
CREATE FUNCTION DetailList@InvoiceId INT) RETURNS varchar(max)
BEGIN
DECLARE @Build varchar(max)
SELECT @Build=''
SELECT @Build = @Build + Description +  ', '
  FROM Detail
WHERE Detail.InvoiceId = @InvoiceId
RETURN @Build
END
GO
 
 
When calling this function you return it as if it were a field in the invoice table.   Don't forget to prefix it with "dbo."
select Amount, dbo.
 
 
 
 

Wednesday, February 10, 2010

Silverlight Non-Descript Errors When Deploying Using WCF Services

You deliver a Silverlight module that uses WCF Services to a production server.  It works great on your machine, but when you run it in production, you get an error every time you call out to a WCF service.  
 
The issue may be in the ServiceReference.Config file.  In the <System.serviceModel> section, in the <client> section, you will see endpoints for every service.  Change the 'address' of these to the endpoints that you use in production, recompile, and deliver the module.  
 

Monday, August 17, 2009

Creating “In” or “Exists” clauses in EntityFramework

To create the equivalent of a SQL “Exists” clause in EntityFramework, do the following:

ddlRecipe.DataSource = from r in _global.db.Recipe
              where (from    u in _global.db.RecipeUser
                     where u.UserId == _global.CurrentUser.Id
                        && u.RecipeId == r.ID
                     select u.RecipeId).Any()
                || (r.HTIRecipe && _global.CurrentUser.HTIEmployee)
                orderby r.Name
              select new { r.ID, r.Name };

In this case the query is returning all Recipes where there exists a RecipeUser in the other collection.

Monday, August 3, 2009

Installing new modules in DNN

To install the feedback module in DotNetNuke, I had to go into permissions and grant full control to ‘Everyone’ to get it to install successfully.  After the install, I removed the permissions again.  Not sure why, but nothing else worked.  Without doing this it kept complaining about File access permissions to \bin\DotNetNuke.Modules.Feedback.dll.