C#
Posted By Mike Bender / 13th February 2012
I recently ran into an issue with when using the SqlFunctions.StringConvert function (as required by LINQ to cast scalars to a string). What I didn’t realize at first is that the StringConvert function pads the string that it casts to. As it turns out this causes some issues with defining a selected item in the list.
To best describe this issue it is probably best to quickly walk through an example of how I ran into this and resolved it.
The Problem
The editor is to provide a drop down list of available options for a given property on the Entity being edited. In my case this was a POCO being used with the Entity Framework. Straight forward enough. I started with the following in my view model.
public class foo
{
public MyObject MyObject { get; private set; }
public List MyObjects{ get; private set; }
public foo(int id){
this.MyObject = MyObjectRepository.GetById(id);
this.MyObjects = (from c in MyObjectRepository.All
where c.Id != id
select new SelectListItem {
Text = c.Name,
Value = SqlFunctions.StringConvert((double)c.Id)
})
.ToList());
}
}
In my view I had the following code.
@Html.DropDownListFor(m=>m.MyObject.ParentID, Model.MyObjects)
MyObject has a property that references its parent, if it has one. This is the trigger for our selected item.
Upon running this code I quickly noticed that the parent was not being selected. I looked and the LINQ statement was getting the correct ID’s and the ParentID was correct. So what was happening?
Well as it turns out the StringConvert functions casts the Id to a padding string and adding a call to the Trim function fixed everything. This is definitely a bit clunky in my opinion. Why did the LINQ team not let us simply cast our scalars using the ToString function?
Well that is it, below is the final LINQ statement that worked for me.
this.MyObjects = (from c in MyObjectRepository.All
where c.Id != id
select new SelectListItem {
Text = c.Name,
Value = SqlFunctions.StringConvert((double)c.Id).Trim()
})
.ToList());
Related Posts:
Posted By Mike Bender / 10th February 2012
I recently worked on a project that used the Entity Framework and T4 POCO generation. I ran into a little snag during the implementation of the Repository pattern that took a few minutes for me to figure so I thought that I would save someone else the time and share here.
First let’s start off with the Interface.
public interface IRepository : IDisposable
{
IQueryable All { get; }
T GetById(int id);
void Add(T model);
void Update(T model);
void Delete(T model);
void Save();
}
Next we’ll look at my first try at the concrete implementation of the Update method. The DataContext is a private property containing a reference to the object context object.
public class MyRepository : IRepository
{
public IQueryable All { get{...} }
public MyEntity GetById(int id) {...}
public void Add(MyEntity model) {...}
public void Update(MyEntity model)
{
DataContext.Users.Attach(model);
if (model.Id == null )
{
// no UserId denotes new object to the database.
DataContext.ObjectStateManager.ChangeObjectState(model, System.Data.EntityState.Added);
}
else
{
// object is not new. We need to get the original object and apply original values.
// Applying current or original values, changes the state
// of the attached object to Modified.
var originalObject = GetById(model.UserId);
DataContext.ApplyCurrentValues("MyEntities", originalObject);
}
Save();
}
public void Delete(MyEntity model) {...}
public void Save() {...}
}
Everything looked good to me so I ran a quick test set. To my surprise the Update method was failing. Why would this be? Well as it turns out since I was generating my POCO’s I needed to set the EntityState to Modified manually. So added the line below after the ApplyCurrentValues call.
DataContext.ObjectStateManager.ChangeObjectState(model, System.Data.EntityState.Modified);
I fired up the test cases once again and was happy to see all tests passed.
Well that is it, I hope that this post helps you and others in the future.
Related Posts:
Posted By Mike Bender / 30th January 2012
I was recently presented with an issue of casting a Bitmap Image (System.Drawing.Image) to a BitmapImage (System.Windows.Media.Imaging). Unfortunately WPF has no converter for this. To solve this issue I created a helper function that utilizes the BitmapImage StreamSource property to write a System.Drawing.Image to the object. I’ve included the helper function in full below.
You are free to use this code in any form you wish.
using System.IO;
using System.Windows.Media.Imaging;
namespace System.Drawing
{
public static class ImageExtensions
{
///
/// Returns a BitmapImage from the passed bitmap.
///
/// Bitmap to convert.
///
public static BitmapImage ToBitmapImage(this Image image)
{
MemoryStream ms = new MemoryStream();
image.Save(ms, image.RawFormat);
ms.Seek(0, SeekOrigin.Begin);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();
return bi;
}
}
}
Hopefully this helper makes your day a bit better!
Update:
When working with EDSDK see my post Canon EDSDK Liveview and Bitmap for details on working with the evfImg memory stream.
Related Posts:
Posted By Mike Bender / 24th June 2011
I recently began work on a RESTful web service using WCF 4. One of our requirements was to implement basic HTTP authentication (http://en.wikipedia.org/wiki/Basic_access_authentication). This wasn’t quite as straight forward as I initially thought it might be. After some digging around I came across a wonderful extension library WcfRestContrib (https://github.com/mikeobrien/WcfRestContrib) .
Proceeding after finding this library and use of their documentation I basic auth integrated quickly into the service. At this point I ran into another issue, Visual Studio 2010′s development web server (Cassini) does not support HTTPS. After some quick searching I stumbled across a post on Scott Hanselman‘s blog Working with SSL at Development Time is easier with IISExpress. I must say, what a life saver! Scott, as usual outlined the process wonderfully and it worked perfectly.
If you are in need of SSL while developing your ASP.NET or WCF web service this is the first and last place that you need look.
Some quick links:
Related Posts:
Posted By Mike Bender / 22nd October 2010
UPDATED: A newer version has been released visit this POST
Earlier this week I received an email informing me of the release of the EDSDK 2.9 library. I must say that this has been a quick release cycle for Canon and their SDK. Upon reviewing the change notes for this release there really only appears to be 2 changes.
First, Canon added support for their latest EOS camera the Canon EOS 60D. Second they removed support for the antiquated Windows™ 2000 operating system.
That being said I did look through the headers and C files for differences and have provided my findings below.
Read Full Post »
Related Posts:
Posted By Mike Bender / 10th March 2010
It’s been a loooong time between posts and I apologize for that. The resent months have been quite busy and a somewhat recent change in direction on my software development has led to me no longer diving into WIA.
In this post I am providing a functional class that provides a means to enumerate the devices properties, set a device property and take a picture. What this class does not include is a function for downloading the device image. This class is meant to be a simple basis from which you can build upon. The code is offered as is.
Read Full Post »
Related Posts:
Posted By Mike Bender / 16th October 2009
So I’ve been working on a tether program for my Nikon D40x. My current development platform is 32-bit Vista, but my target machine is a 64-bit Vista machine. So today I’ve finally got everything working on my development machine and migrate it to my target machine. Everything seems fine under normal test cases until I go to exercise a couple of key events used to detect if the camera is available or not. Well they are not firing as they do under 32-bit Vista.
Has anyone ran into this themselves, and if so have you found a workaround yet? Any input would be greatly appreciated.
Related Posts:
Posted By Mike Bender / 28th August 2009
Welcome, in this article I am going to how to get your project setup to use WIA, how successfully connect to an image device and finally how to enumerate all the properties and events the device supports. While this article will only touch the surface of what all you can do with WIA subsequent articles will continue to delve deeper into WIA’s full capabilities.
Read Full Post »
Related Posts: