Saturday, January 19, 2013

Bitten by uninitialized DateTime fields in Entity Framework

I was trying my hands with Ninject, Quartz.Net in an MVC 4 project. Everything was working fine. I added a new model to data project. After adding migration and updating database, I pressed F5.

pofff... the page which was working fine well..., stopped being fine anymore.

I was getting following error:

The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.

But I was already setting all the date fields and everything was working nicely. After fooling around, it suddenly occured to me that I was not setting a property that is coming from the base class which all the entities were inheriting from.

If the date fields are not initialized (not set), by default it is set to

01-JAN-0001
which of course falls outside the range of the dates acceptable by SQL Server.

The simplest way to get arround is to set the DateTime fields to something acceptable to SQL Server. All worked fine after I added following line:

obj.UpdatedOn = DateTime.Now;

Thursday, December 22, 2011

Displaying items horizontaly in WPF ItemsControl

I am trying my hand on Microsoft Composite Application for few days. I was trying to display list of Actions available from a moudle into a Region using following markup:

  
        
            
                
                    
                
            
            
                
                    
                        
                            
                        
                    
                
            
        
    

This displays items in vertical manner (as is default with ItemsControl and its derivatives like ListBox). I added following code to make it use StackPanel:

    
        
            
        
     

But it did not produce the desired outcome. I tried the WrapPanel also but it was still same. After some more frustration I commented GroupStyle tag:

    
        
            
                
                    
                
           
        
     

Now it worked as desired by displaying Button controls in a single row.

Wednesday, June 22, 2011

Error on Adding WCF Service Reference to ASP.Net Project

I am writing a small website using EF 4 Code First. There are some legacy tables which don't have primary key. This (not having PK) would have created complications with EF, but I needed to only read data from those table. So I decided to create a WCF service to pull the data. I defined [ServiceContract], [DataContract], [OperationContract] etc. and everything was going fine till I tried add reference to my web project. In the Add Service Reference dialog VS did find service, but when I clicked on service to fetch metadata I got following error:

There was an error downloading 
'http://localhost:8732/Design_Time_Addresses/LegacyDataService/DataService/mex'.
The request failed with HTTP status 400: Bad Request.
Metadata contains a reference that cannot be resolved: 
http://localhost:8732/Design_Time_Addresses/LegacyDataService/DataService/mex.

After some head scratching and googling about the error I found out a post on MSDN forum where somebody was putting [DataMember] attribute over an indexer. I checked my [ServiceContract] and [DataContract]s.

My [ServiceContract] read as follows:

[ServiceContract]
public interface IDataService
{
    [OperationContract]
    IList<LegacyUser> GetUsers();
    ......
    ......
}

I tried to comment the IList<LegacyUser> stuff, but the error was still there. It then occurred to me that there was an Address property which returned build user's address by concatenating other fields. After this Address property was commented the error was gone.

Moral of the story is that:
  1. you should not put [DataMember] attribute on Indexers (as mentioned in MSDN post).
  2. you should not put [DataMember] attribute on properties that use other properties with the attribute to calculate return value.

Wednesday, May 25, 2011

MySQL Error 2002

I was reading an article about MySQL replication yesterday. After reading a bit I thought it will be really easy to test it. I setup a remote MySQL server available with a friend and configured it as master. After this I fired up Ubuntu Lucid Lynx VM in VirtualBox and configured it as slave (changed my.cnf file mainly).

Now when I tried to login into MySQL I got following error:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
After sifting through many posts on internet forums, I occured to me to check error logs (how intelligent, thought this should have been first thing to do). I issued following command:

tail /var/log/mysql/error.log

110525 23:19:20 [ERROR] /usr/sbin/mysqld: unknown variable 'relay-logn-index=test_slave-bin.index'
110525 23:19:20 [ERROR] Aborting

So it was only an extra 'n' that gave me so much headache. After changing 'relay-logn-index' to 'relay-log-index' it worked fine.

Thursday, January 6, 2011

Making Selected Row Visible in UltraWinGrid

I was facing a problem with Infragistics UltraWinGrid object. There were about 15 rows in the grid. Due to size of form only 8 were visible at a time. The first rows was fixed, so it cannot be moved by scrolling.

To edit row a popup form is opened on double clicking the row and a button on the form. On successful saving of editing information in popup form, parent form's grid is refreshed from database.

Upon refreshing if the very first row (just below the fixed row) was selected or popup was opened by clicking on button when selected row was not visible, it is not scrolled to view.

Unfortunately windows grid does not contains a method like web grid which scrolls selected row to view (ScrollToView).

After some hit and trail I found the following solution:

myGrid.ActiveRowScrollRegion.FirstRow = row;//row is selected row (can get using myGrid.ActiveRow)

Wednesday, October 27, 2010

Ruby On Rails: rspec failing with [no such file to load -- spec_helper]

I have started reading www.railstutorial.org. It is wonderful tutorial for learning Ruby on Rails (ror). I am at chapter 3 currently and tried to setup the unit testing.
After creating project and generating controllers, I installed growl for windows and then autotest, autotest-growl gems. But when I issued the following command:

rspec .\spec\controllers\pages_controller_spec.rb
I got following error:

:29:in `require': no such file to load -- spec_helper (LoadError)
 from :29:in `require'
 from F:/Code/Learning/ruby/todo/spec/controllers/pages_controller_spec.rb:1:in `'
 from D:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.0.1/lib/rspec/core/configuration.rb:306:in `load'
 from D:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.0.1/lib/rspec/core/configuration.rb:306:in `block in load_spec_files'
 from D:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.0.1/lib/rspec/core/configuration.rb:306:in `map'
 from D:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.0.1/lib/rspec/core/configuration.rb:306:in `load_spec_files'
 from D:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.0.1/lib/rspec/core/command_line.rb:18:in `run'
 from D:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.0.1/lib/rspec/core/runner.rb:55:in `run_in_process'
 from D:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.0.1/lib/rspec/core/runner.rb:46:in `run'
 from D:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.0.1/lib/rspec/core/runner.rb:10:in `block in autorun'

Going through much pain of reinstalling quite a few gems and banging my head, I went back to read the tutorial page again from beginning and make list of what I have already done as given in page. I found out that I had missed to execute following command:

rails generate rspec:install
It did the folowing:
create .rspec
  exist  
 create  spec/spec_helper.rb
 create  autotest
 create  autotest/discover.rb

Now I had got spec_helper.rb in my spec folder and was able to execute the tests (happily ever after ^_* ).

Also remember to install win32console-color gem to get colorfull info with autotest.
Happy Coding!

Tuesday, February 2, 2010

A way to resolve bugs

I just found a nice blog post by Chris Missal titled "How I Approach a Defect" via a tweet by Elijah Manor.
Chris outlines the systematic approach he takes when solving programming bugs (defects, as he calls them). He underlines the fundamental notion that you should actually fix the problem not just get the right answer for a particular case.
Like Five Ws (5Ws) Chris Missal's defect solving framework tries to ascertain that proper solution has been applied to remove defect by answering following questions:

Does This Solve the Problem? 

Is This Flexible, Maintainable, Clear and Simple?

Does This Leave the Code Better Than I Found It?

Is It Covered with Tests to be Future-Proof?

Is There Any Code that was Commented Out or Stale?

Do Any Named Variables or Classes Exist that Aren't Clear?

Are All Changes Relevant to the Fix?

Please read his post for details of what these questions mean.
Happy Coding!