dotnet web sql business etc

26 серпня, 2011

Scripts to enable SQL Service Broker

Just recently a newcomer in our team could not get his windows service to work properly, and get messages from sql service broker queue. In the end it turned out that in the main database that was created from scratch, service broker appeared to be disabled. Here are some scripts to enable it:

alter database [mydatabase] set single_user with rollback immediate
alter database [mydatabase] set enable_broker
alter database [mydatabase] set multi_user with no_wait

Мітки: ,

12 серпня, 2011

The provider did not return a ProviderManifestToken string

I just have been going through ASP.NET MVC tutorial, step 5 (http://www.asp.net/mvc/tutorials/getting-started-with-mvc3-part5-cs), when I have ran into the "The provider did not return a ProviderManifestToken string" error when interacting with Entity Framework. When I tried to create a Controller specifying Model class  (Movie (MvcMovie.Model)) and Data Context Class (MovieDBContext (MvcMovie.Model)), Visual Studio gave an error "The provider did not return a ProviderManifestToken string". My connection string seemed fine:
 add name="MovieDBContext" connectionString="Server=.;database=movies;Integrated Security=SSPI;User Instance=true" providerName="System.Data.SqlClient"/>
<

Changing User Instance from true to false, solved the issue :). Why? I don't know, have not had time to investigate.

Мітки: ,

10 серпня, 2011

Install, uninsttall and kill a Windows Service

After having set up new build environment I decided to note some tips on working with Windows services, for myself to remember, and for others to easily find needed steps. So, off we go.

To install a Windows service (in a Windows 7 environment):
1) Copy service files (including the .exe) to the folder C:\MyService
2) Open command prompt as Administrator
3) cd to C:\MyService
4) Install Windows service from commant prompt using installutil.exe:
C:\MyService>C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\installutil.exe /serviceName="My Service" MyService.exe

To uninstall a Windows service:
1)  Open command prompt as Administrator
2)  cd to C:\MyService
3)  c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe /u  /serviceName="My Service" MyService.exe

Sometimes, it can happen that Windows Service can not be stopped. It remains in status Stopping, and never stops actually. Then you would have to kill it:
1)  Open command prompt as Administrator
2)  sc queryex MyService. You will receive the following information:

SERVICE_NAME: MyService
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 4  RUNNING
                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0
        PID                : 4084
        FLAGS              :


3)  taskkill /F /PID 4084

Мітки: ,

22 квітня, 2011

Implementation of a simple algorithm to generate a random alpha-numeric string of a needed size

Below is an implementation of a simple algorithm to obtain an alpha-numeric id, I found on the web, neat and easy:

public static string GenerateUniqueCode(int maxSize)
        {
            var allowedChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            var random = new Random();
            var result = new string(
                Enumerable.Repeat(allowedChars, maxSize)
                          .Select(s => s[random.Next(s.Length)])
                          .ToArray());
            return result;
        }

Мітки: , ,

Validating several controls with one customvalidator: using ValidatorHookupControl javascript function

Recently I have run into a seemingly simple problem: I had to make a control for picking a birth date.

First, I thought that using a MaskEdit control from AjaxControls toolkit would be the best fast and simple solution. However, then it turned out that MaskEdit is quite limited, in terms of validating date type input.

Afterwards, my colleagues recommended using dropdowns for days, months and years. That is easy, first we populate the dropdowns with the needed options (days 1-31, months Jan - Dec, years 1900-2011). Yet, there are some invalid combinations of values that can be entered by the user. Thus, input has to be validated, when either day, month, or year value changes, all three dropdowns have to be validated as a whole. This problem lead me to find out a solution on how to link several input controls to one custom validator by using ValidatorHookupControl.

First, we should define the following javascript function for our file:

function HookupControl(curObj, customValidatorClientID) {
         var customValidator = document.getElementById(customValidatorClientID);
         customValidator.controltovalidate = curObj.id;
         customValidator.clientvalidationfunction = 
"ValidateDate";

         ValidatorHookupControl(curObj, customValidator); }

this function links the input control (curObj) to your custom validator control (customValidator) at runtime.

Afterward, we should have HookupControl function run everytime user changes the value of either field (day, month, year). To do that, we have the define the following code for each of the dropdowns in the Page_Load event of the control (in addition to populating control with possible values):

ddlDay.Attributes.Add("onfocus", "HookUpControl(this,'" + customValidator.ClientID + "')");


Nice solution. Works as a charm!

Мітки: , ,

19 квітня, 2011

Rhino Mocks: how to clear/reset expectations when using same test method several times

Today I faced a seemingly simple problem of running one unit test method with different parameters stored in arrays. When looping through the arrays of data, I was setting expectations for different set of arguments. Well, in the third loop, I have set the different expection results for the set of arguments equvalent to the one in loop number two. Althrough I have "overriden" the expectation, the test method was still using the first expectation set. The solution was to add Repeat.Once() call to the end of the expectation set line to make sure it is used only once.

Мітки: ,

07 квітня, 2011

Client validations with custom validators in .NET: why it might not work

I have just spent about an hour trying to find out why client validation with clientvalidationfunction did not work. Well, unfortunately for myself I have used args.isValid parameter instead of args.IsValid (capital I) in my javascript function. Daaaah... :)

Мітки: , ,