Upgraded to BE 1.6

4. May 2010

I recommend following this guide here.  It was an uneventful update, but now I have quite a few bugs afterwards.Cry

Hopefully, I will resolve them in the coming weeks and start posting again.

Update: There was some problems with some old extensions of mine, and now everything seems to be working.  Love the recaptcha...

I have been away and working so much, that I have not had any time for posting.  Hopefully I will get back to it soon.

BlogEngine

MOD 11 Check Digit code in C# and sql

23. January 2010

 

Recently, I needed to generate a MOD 11 or Modulus 11 check digit calculation in c#.

So, I took a look at the MOD11 on wikipedia for the specification. 

  • Norwegian link here
  • English Translated by google here
  • Norwegian: Page 14 of bbs spec here

 

I searched around to see if someone had coded it and found a few examples for c#, but they all didn't seem to calculate it per the specification.  I broadened my search and found an excellant MOD11 check generator here.  The php code here was also straightforward, so I converted it to c#.  Perhaps, it could be optimized a little, but it is a small utility method, so I didn't bother.

Here is the code:

public static string CalculateMOD11(string controlNumber)
        {
            int i;
            int sum = 0;
            string checkDigit= string.Empty;
            
            //Set the weight beginning=2.
            int weightcount = 2;
            char[] tempArray = controlNumber.ToCharArray();
            //Reverse the control number
            Array.Reverse(tempArray);
            string controlNumberReversed = new string(tempArray);
            for (i = 0; i < controlNumberReversed.Length; i++)
            {
                //sets the weight count to loop from 2 up to 7.
                if (weightcount >7)
                {
                  //resets to 2 again.
                  weightcount=2;
                }
                sum += Convert.ToInt32(controlNumberReversed.Substring(i, 1),CultureInfo.InvariantCulture) * weightcount;
                weightcount++;
            }
            int remainder = sum % 11;
            switch (remainder)
            {
                case 0:
                    checkDigit = Convert.ToString(0, CultureInfo.InvariantCulture);
                    break;
                case 1:
                    checkDigit = "-";
                    break;
                default:
                    checkDigit = Convert.ToString((11 - remainder), CultureInfo.InvariantCulture);
                    break;
            }
            controlNumber = controlNumber + checkDigit;
            return controlNumber;
        }

I found out later that I needed a sql function for this also.  I took me a little time to convert this to a stored procedure as I not that good a writing code in sql.  

Below is the code, I am hopeful this will save time for you. :)

ALTER FUNCTION [dbo].[CreateMOD11]
(
	@ControlNumber Varchar(15)
)
RETURNS Varchar(16)
AS
BEGIN
	-- Declare the return variable here
		DECLARE @ReversedControlNumber Varchar(16)
        DECLARE @CharCount int
        DECLARE @Sum int
        DECLARE @TotalSum int
        DECLARE @WeightCount int
        DECLARE @Checkbit char(1)
		DECLARE @Remainder int

	-- reverse the control number
    select @ReversedControlNumber= reverse(@ControlNumber)

	select @CharCount=1
	select @TotalSum=0
	select @WeightCount=2
	WHILE @CharCount < Len(@ReversedControlNumber)+1
	BEGIN
	   --loop to 7 then repeat as per MOD11 spec. http://no.wikipedia.org/wiki/MOD11
	   IF @WeightCount > 7
	   BEGIN
		 select @WeightCount=2
	   END
	   select @Sum =Cast(Substring(@ReversedControlNumber,@CharCount,1)as int)* @WeightCount
	   select @TotalSum = @TotalSum + @Sum
	   select @CharCount= @CharCount +1
	   select @WeightCount= @WeightCount +1
	END
	select @Remainder=(@TotalSum % 11)
	Select @Checkbit= CASE @Remainder
						WHEN 0 THEN 0
						WHEN 1 THEN '-'
						ELSE CAST((11-@Remainder) as char(1))
					  END
    return @ControlNumber+@Checkbit
END

I found out that I needed mod 10, so here it is also. :)

(ALTER FUNCTION [dbo].[CreateMOD10]
(
	@ControlNumber Varchar(15)
)
RETURNS Varchar(16)
AS
BEGIN
	-- Declare the return variable here
	--Check digit count
	-- Declare the return variable here
	DECLARE @LengthInvoiceOID INT
	DECLARE @NumberOfExtraZeros INT
        DECLARE @NumZerosToInvoiceOID INT
	DECLARE @NewInvoiceOID Varchar(6)
	DECLARE @ControlNumber Varchar(16)
        DECLARE @CharCount int
        DECLARE @SplitSum int
	DECLARE @StringSum char(2)
        DECLARE @Sum int
        DECLARE @TotalSum int
        DECLARE @WeightCount int
        DECLARE @Checkbit char(1)
	DECLARE @Remainder int
	--initialize the variables
	select @CharCount=1
	select @TotalSum=0
	select @WeightCount=1
	WHILE @CharCount < Len(@ControlNumber)+1
	BEGIN
	   --loop to 2 then repeat as per MOD10 spec. http://no.wikipedia.org/wiki/MOD10
	   IF @WeightCount > 2
	   BEGIN
		 select @WeightCount=1
	   END
	   select @Sum =Cast(Substring(@ControlNumber,@CharCount,1)as int)* @WeightCount
       IF Len(@Sum) =2
        BEGIN
           select @StringSum=CAST(@SUM as char(2))
           select @SplitSum=CAST(SUBSTRING(@StringSum,1,1)as int)+CAST(SUBSTRING(@StringSum,2,1)as int)
		   select @TotalSum = @TotalSum + @SplitSum
        END
       ELSE
        BEGIN
			select @TotalSum = @TotalSum + @Sum
        END
	   select @CharCount= @CharCount +1
	   select @WeightCount= @WeightCount +1
	END
	select @Remainder=(@TotalSum % 10)
	Select @Checkbit= CASE @Remainder
						WHEN 0 THEN 0
						ELSE CAST((10-@Remainder) as char(1))
					  END
    return @ControlNumber+@Checkbit
END

Upgraded to BE 1.5

3. December 2009

It was a nice and easy upgrade!  So pretty happy, now I got to do some more blogging.

I have quite a bit of new stuff to write up.

No PosersFirst and foremost, I have made it madatory that I review all posts here now..Since I keep having to delete comments..You know who you are.

So, if your comment doesnt get added, then you know why!  Also, If you are posting with Bots dont bother as they will be blocked.

This site is dedicated to helping people who are passionate about software development.

BlogEngine, General ,

BlogEngine 1.45 and posting comments

11. October 2009

upgradeWell it looks like I will be moving to BlogEngine 1.5, because comment postings are not working in my 1.45 version.  

I dont want to recompile and fix code as suggested here for the fix.  

A little side note...I think BlogEngine has done a great job fixing things as they move forward.  I have been pretty happy with it so far and well it does help if I want to get dirty and into the code I can, but I really dont think it is worth the time for me right now.  Perhaps in the future.

Best Code formatter for Blogengine(Really only one that works right)

6. October 2009

Tonight I have spent way to many hours trying to get the default code formater to work in version 1.4.5.  

I came to many people that had the same problem.  I was fed up and going to post to the code engine site, but I decided I would check and see if anyone else had the same problem.  

Well I was in luck, taliesins has created a addin extroidinaire within the texteditor and it works exactly as I want it to:

If !(working) 
{
  CodeItYourSelf();
}

Check out his post and link to code here.

Of course I haven't checked out Blogengine 1.5, so this could already be in the latest release.  I will wait for 1.6 and then move to 1.5... :)

 

BlogEngine

FxCop rule CA2227 and setter error using linq to sql

6. October 2009

I was having a problem with this fxcop rule today CA2227.  This error was being thrown out on all the  Associations when using the Linq to sql templates here: http://l2st4.codeplex.com/

Well of course we cannot have that as this violates checkin policy, so what I did was add this line here to the template:

 [GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")]

I googled this problem and landed on Andre Loker's blog here.  I understood the problem as he prensented it, but umm Microsoft is doing this..Yes.

So I dug deeper..and came here:  

Yes, it is a problem, but the workaround is against our policy, so onward...bound to here

Great justification and it fit my needs.  Yes there is some evil when using code generations, but I like having more control and the linq to sql T4 templates give me that.

Hope this helps someone else.

.Net , C#

LINQ to SQL templates for T4-Adding Interfaces for C#

6. October 2009

I was working in these templates and wanted to add interfaces.  

I found a link in the discussions here, from Jwooley stating he had done this: http://www.thinqlinq.com/Post.aspx/Title/Generating-Interfaces-for-LINQ-to-SQL-Entities

Well he did it, but in Visual basic and I needed C#, so I changed up the script.   I tried to add this script to his site, but the submission didnt allow it so I will post it here and add a link to his site.

Hope this saves someone a little time:

(Follow the instructions on the think link site and replace this part :

namespace <#=data.EntityNamespace#>	
{
<#		}
#><#if (options.CreateInterfaces) { #>
	/// <summary>
    /// <#=class1.Name#> interface
    /// </summary>
	<#=code.Format(class1.TypeAttributes)#>interface I<#=class1.Name#>
	{
	<#			foreach(Column column in class1.Columns) {#>
	<# if (column.IsReadOnly) {#>ReadOnly <#}#> <#=code.Format(column.Type)#> <#=column.Member#>{ get; set; }
	<# } 
	#>
	}
<# } 
#>
<#		if (data.Serialization && class1.IsSerializable) {

.Net , C#, Programming

Jing and Jing pro

20. September 2009

I was recently beginning to learn T4 and started here with Oleg Sych's great blog posts on T4:

Then I came across these videos on T4 from Kevin Dietz here: 

 

What I thought was cool though was Kevin is using Jing.  I haven't seen Jing before but it is free and is a very slick presentation software from TechSmith.  

TechSmith makes Camtasia studio and Snagit, which some of you probably heard of or use.  What is great with Jing is that it is free, but also it seems to be a lot better than many other of these types of free version.  What I think is better though is  for 15 dollars a year you can buy the pro version and get rid of all the advertising for Jing. This seems to me like a steal as it does everything most bloggers and someone who wants to have a easy demo.  Take a look here at Jing, if you are interested: http://www.jingproject.com/pro/

Overall Jing looks like a very high quality product and definately something I will use in the future for some code showings here at Beginnersoft.

Programming, Software

Welcome to Beginnersoft...back after a long hiatus

12. September 2009

It has has been quite some time since I have posted to this website.

Beginnersoft is back now.  Using BlogEngine.net which seems to do everything I need.  (I see a lot of BlogEngine blogs that don't change the favorite icon located here: pics\blogengine.ico, it is as simple as overwriting this file with your own ico file, makes your blog look unique..)

Now I am back and going to be filling this blog up with interesting information on ASP.NET MVC, .Net, C#, 

I hope you find something interesting here that helps along your journey.

General

Contextual help

1. January 2008
I will be building a contextual help for my company soon, so I did some research and here it is:

.Net , C#, Programming