Skip to main content

Posts

Showing posts from 2009

Image to Byte Array

In Some ways to convert a byte array to Bitmap we see how to convert a Bitmap to and from a ByteArray. As an alternative I use the following extension method to convert an image to a ByteArray: [csharp] using System.Drawing; namespace DC.Core.Extension { public static class ImageExtension { private static ImageConverter _converter; public static byte[] ToByteArray(this Image image) { if (_converter==null) _converter = new ImageConverter(); return (byte[])_converter.ConvertTo(image, typeof(byte[])); } } } [/csharp]

F# and Units of Measure

On Channel 9 they have an interview with Andrew Kennedy about the new Unit of Measure feature in F#. This is a really cool feature and I have plans to use it, however there is one problem I have with it: The UOM information is baked into the compiler and can't be persisted. An example that Andrew uses it the NASA's Mars Climate Orbiter that crashed due to an issue with Units of Measure (long story short the program thought it was using Newtons when it was actually using Pounds Force). This is a good example of how when you are doing calculations it would be nice if something other than the brain of the programmer was checking the units. But there is another time that UOM is important: When inputting the data. I recently worked on an application where the units for a given value could be in English or SI (metric) units and we had an elaborate system to deal with them. The end user could even come up with their own unit system if they wanted to and the application had to

RESTful Architecture

Found the following in the article ASP.NET MVC: Using RESTful Architecture and thought it worth noting. It describes what the MVC command actions should be, no more than this is needed: The endpoint needs to be something meaningful, and Rails uses a nice convention that divides the endpoints into 7 main bits: Index - the main “landing” page. This is also the default endpoint. List - a list of whatever “thing” you’re showing them - like a list of Products. Show - a particular item of whatever “thing” you’re showing them (like a Product) Edit - an edit page for the “thing” New - a create page for the “thing” Create - creates a new “thing” (and saves it if you’re using a DB) Update - updates the “thing” Delete - deletes the “thing” Normally the last 3 are “action only ” and don’t have a view associated with them. So if you “create” a Product (from the New view, using Create as the action on the form), you’d just redirect then to the List or Edit views. Li

Json for jqGrid from ASP.Net MVC

jqGrid takes a specific format for its json (taken from jqGrid documentation): [js]{ total: "xxx",page: "yyy", records: "zzz", rows : [ {id:"1", cell:["cell11", "cell12", "cell13"]}, {id:"2", cell:["cell21", "cell22", "cell23"]}, ... ]}[/js] The tags mean the following: total - Total number of Pages. page - Current page Index. records - Total number of records in the rows group. rows - An array with the data plus an identifier. id - The unique row identifier, needs to be an int from what I have found. cell - An array of the data for the grid. The ASP.Net MVC framework has the JsonResult response type which we can use to populate the jqGrid. As an example I created a Person model and a method to return some data: [csharp] public class Person { public int ID { get; set; } public string Name { get; set; } public DateTime Birthday { get; set; } } public I

LINQ to SQL Roundtrips: SQL Trace

I was looking into reducing the number of database round trips that LINQ to SQL took and found an article by David Hayden that fit the bill. I wanted to see what was actually happening so I slapped together a simple demo. Using the Pubs database I created a console app, added the LINQ to SQL classes then created a simple repository class: public class AuthorRepository { public author GetAuthorWithTitles( string authorId) { var db = new PubsDataClassesDataContext (); return db.authors.FirstOrDefault(a => a.au_id == authorId); } public author GetAuthorWithTitlesWithUsing( string authorId) { using ( var db = new PubsDataClassesDataContext ()) return db.authors.FirstOrDefault(a => a.au_id == authorId); } public author GetAuthorWithTitlesPrefecth( string authorId) { using ( var db = new PubsDataClassesDataContext ()) { var options = new DataLoadOptions (); op

DataTable: Finding Differences in Column Values

I have two tables in a typed DataSet and I want to compare one column in each table to see if TableA has values that are not in TableB. IEnumerable < string > valuesInA = typedDataSet.TableA.AsEnumerable().Select(row => row.AField); IEnumerable < string > valuesInB = typedDataSet.TableB.AsEnumerable().Select(row => row.BField);   foreach ( var notInB in valuesInA.Except(valuesInB))     Debug .WriteLine( string .Format( "Value not in TableB.BField: {0}" , notInB)); This is assuming that both AField and BField are the same type.

DataSet: More reasons to not like it

So I am still living in the world of DataSets, when will I ever learn? The task was simple, take the contents of DataSetA and merge it into DataSetB. Sure, just use: DataSetA.Merge(DataSetB, true) and life will be good. But wait! Why is it when I try to save the merged data from DataSetB to the database it doesn't show up? Because the RowState of all the rows in all the tables are set to Unchanged! And a merge operation does not change the RowState. So if I want all the data from B to be saved to A I have to change all the row states of all the tables to Added. Sure, I should be able to loop thorough it all and set the row's state as such: row.RowState = DataRowState.Added; Not so fast grasshopper! row.RowState does not have a setter! Isn't that Asinine! You have to use: row.SetAdded(); What ever, I say setter, you say method. Long story short I created an extension method that works. public static DataSet MergerAllDataAsNew(this DataSet target, DataSet source) {

Using IDataErrorInfo for Validation

I read an article on CodeProject titled Total View Validation where the author complains that IDataErrorInfo is inadequate for WPF validation.  The assumption he makes is that all the validation code needs to go into the IDataErrorInfo.this[string] property as such: [csharp] public string this[string name] { get { string result = null; if (name == "Age") { if (this.age < 0 || this.age > 150) { result = "Age must not be less than 0 or greater than 150."; } } return result; } } [/csharp] This should not be the way that IDataErrorInfo is used as it puts business logic in the model, as the author points out. His solution though was to create another mechanism to notifiy the user of validation errors and to not use IDataErrorInfo at all. A solution I would propose though would be to follow how DataTables and DataTables use IDataErrorInfo by implementing methods

Y2K38

Eight seconds past 3:14 a.m., on January 19, 2038, most computers in the world will think it’s actually a quarter to 9 p.m. on December 13, 1901. Oh Crap. UPDATE: At 11:31:30 pm UTC on Feb 13, 2009, Unix time will reach 1,234,567,890.