Sunday, 11 January 2009

Unable to build in VS2008

I’m sitting and trying to resolve an error in my application. It is the handling of images from the database, to the WPF gui. I have written a post about this in Swedish which I will translate when I have resolved the problem I have now. But this post is not about that, this post is about the annoying  build error
Unable to copy file "obj\Debug\exe-file" to "bin\Debug\exe-file.exe". The process cannot access the file 'bin\Debug\exe-file.exe' because it is being used by another process.

I’ve tried several ways to get around this, but it seems to be very hard, especially when GDI+ is involved. But one thing I’ve found that helps is to first kill the process vshost.exe for the current project. (It will be directly restarted), then I choose Clean Solution under the Build menu in VS2008, then I can build the solution again. I think the trick lies in killing the vshost.exe process.

Labels: , ,

kick it on DotNetKicks.com

Friday, 9 January 2009

Localization in Asp.Net

Since I changed to English for this blog I realised that I had to change the language of the single webpage of www.invitationaltour.com.

So I took the chance of developing a “proof-of-concept” for localization, which actually was harder than I thought when you are using master pages in asp.net. Before I started I was sure it was only a matter of setting the currentuiculture of the current thread, since the page class has a property named Culture which can be set to change the culture for a page, but it was more to it than that. I did a simplified version of localization, and in the future I will make a more appropriate solution and will write a post about that solution. For now I just wanted to localize a single page, which are nested in a master page. The correct language should be determined by the browsers language settings. So, here it is.
First, I use the Localization control. You can use a literal control, they are exact the same, but the localisation control allows you to edit the text in design mode.

<h1>
    <asp:Localize 
    ID="Header1" 
    meta:resourceKey="Header1" 
    runat="server" 
    Text="Välkommen!"/>
</h1>

Välkommen is Swedish for Welcome, and Swedish is the default language for the site. This is the first word on the page at www.invitationaltour.com

Next I created a folder under my solution and named it App_LocalResources. Resource files under App_LocalResources are page specific, and resources in the folder App_GlobalResources  are application specific, and should be the place to store words that are used through out the application.

Next, I added a resource file to the App_LocalResources and named the file to Default.aspx.resx. Double click on that file and an editor i shown where you can add and edit localised words. This file has no culture info in it, so the words in this file will be the default language for the page.

Add a new item by typing Header1.Text in the name column, and the value for the text in the Value column. As you can see in my asp.net code, I’ve named both the ID and the meta:resourceKey to “Header1”. This is the way I want to do it, to keep the ID and resourceKey the same, but it’s up to you. The important thing is that the value for Name in the resource file corresponds to the value of meta:resourceKey. In the resource file you also typed .Text after Header1. This is the way to tell Asp.Net which property that should display the text. You can do it the same for buttons or other controls in the same way.

After you’re down, save the file. Now we will create a second language. Create a copy of the newly created resource file in the same directory, and rename it to Default.Aspx.en.resx. This is now the resource file for the English language. Double click the file to open it in the editor and change “Välkommen” to “Welcome”. Save the file.

Now, to let asp.net automatically  determine the language of the calling browser, add the following to your web.config file.

<system.web>
    <globalization culture="auto"  uiCulture="auto"/>

This will tell the asp.net runtime to set the culture for you, and if no resource file is found for the current culture, it will fallback to the words in the file Default.aspx.resx, which is Swedish. So If someone from France browses to this page, they will see the page in Swedish.
Normally, a French person wont understand Swedish, so I decided to add two language buttons in the upper right corner, one for Swedish and one for English. Now, it was here I thought it was just a matter of setting the currentUICulture on the current thread, but it’s more complicated than that. In the click event for each button, I have the following code in the master page.

protected void imgBtSwedish_Click(
object sender, ImageClickEventArgs e)
        {
            this.Response.Redirect("Default.aspx?lang=se");
        }

        protected void imgBtEnglish_Click(
object sender, ImageClickEventArgs e)
        {
            this.Response.Redirect("Default.aspx?lang=en");
        }

The above code actually just redirect the user the default page, with a query string specifying the chosen language.

I know that this is not the correct way of doing this, but it is good enough for just one page. When the web page gets more pages, this will not do it, then I will use the Profile class to set the languages, but more on this in a future post.

This is all the code that are needed in the master page, but in the default.aspx we have to put some code also, to actually set the language.

protected override void InitializeCulture()
{
            if (this.Request["lang"] != null)
            {
                //Swedish is the fallback language
                CultureInfo newCultureInfo = 
new CultureInfo("sv-SE"); switch (this.Request["lang"].ToLower()) { case "en": { newCultureInfo =
new CultureInfo("en-GB"); break; } } Thread.CurrentThread.CurrentCulture =
newCultureInfo; Thread.CurrentThread.CurrentUICulture =
newCultureInfo; } else base.InitializeCulture(); }

The above code overrides the InitializeCulture and checks if there are any language specified, and if so, sets is. Remember to call base.InitializeCulture() if you don't set the language your self, and NOT to call base.InitializeCulture if you do. The base class doesn’t do nothing extra than setting the culture as in this code.

There is much more to Localization than I have written about here, this post jus describes a very simple way to localize a single page with in a master page, in my case enough to display a localized “under construction” page. In the future I will user the Profile class and store language settings for a user in a database. The resource files are XML files, I am also on the search for some great editors. Because the way it is now in Visual Studio, the workflow does not encourage agile development, since I don’t want to open each resource file one at a time, remember the meta:ResourceKey and type the localized word. I want an editor where I can edit all languages at the same time! For now I make all in the default language, and the make copies of the resource file and edit them in the XML-editor, since the built in editor does not handle line breaks very well. To make copies of the resource files works well if you develop on page from top to bottom, and get done with it, but I don’t want to work that way.

Labels: , , ,

kick it on DotNetKicks.com

Friday, 19 December 2008

Linq To Sql eller Entity Framework

Jaha, en till ändring. När jag började med detta projekt i somras för den n:te gången så beslutade jag mig för att använda Linq To Sql i datalagret. Mycket av mitt beslut grundades på denna artikel. Speciellt som jag vet med säkerhet att system kommer att använda MS Sql Server som databas. Nåväl, idag läser jag följande artikel. Linq To Sql kommer alltså inte att vidareutvecklas och Linq To Entities saknar ju vissa saker som Linq To Sql har, tex. stöd för Stored Procedures. Så nu är ju frågan vad jag ska göra. Jag kommer först och främst läsa igenom denna blogg. Ett av syftena med detta projekt är att lära mig ny teknik, men som jag skrev tidigare har jag satt ner foten till viss del för att någon gång komma i mål. Men jag får väl lyfta foten igen, jag vill ju inte välja ett spår som Microsoft inte ska vidareutveckla. Jag vill inte börja om igen när VS2010 och .Net 4.0 lanseras.

Labels: , , ,

kick it on DotNetKicks.com

Wednesday, 11 June 2008

Välkommen!

Denna blogg kommer främst att handla om systemutveckling med .Net som är relaterat till det system jag skapar för det frustrerande spelet golf. Till vardags arbetar jag som IT-konsult och denna blogg har inget med min anställning att göra, men självklart kommer det att finnas beröringspunkter. Tänk det som brukar stå längst ned på filmer. "Detta är personliga åsikter som jag uttrycker på min fritid, och eventuella likheter är rena tillfälligheter, bla bla bla, jagkan inte hållas ansvarig..." Nåväl, tillbaka till innehållet. .Net och golf....? Well, jag har utvecklat ett golfstatistik program som hobbyprojekt sedan 1994. (Då med Visual Basic 4/16 bit, lite osäker på året men det var Win3.11 och VB4 och även lite Amiga innan det). Anledningen till detta är främst för att ha ett projekt med ett "komplett scope" som täcker de flesta aspekter av systemarkitektur. Enstaka användare, många användare, desktop, distribuerat, webb, etc. Ett golfsystem är skalbart avseende funktioner, bara fantasin sätter gränser för vilka funktioner och tjänster som som kan vara nyttiga och roliga. En annan stor anledning är också att jag gillar både golf och statistik. Eftersom mitt golfspel ofta suger så är det ju väldigt bra att det finns lögn, förbannad lögn och statistik. Visst, jag kanske torskade på Kyssinge i helgen, men jag var grym på att träffa fairway på Par4 hålen! Under alla dessa år har endast en version lanserats publikt, då skapat med VB6. Jag registrerade webbplatsen på sökmotorerna, jag gjorde ingen annan marknadsföring. Efter lite drygt två år hade jag en hel de registrerade användare varav drygt 50% använde programmet aktivt. Gränssnittet var på Svenska, så det finns helt klart ett behov av ett proffsigt program för Svenska golfspelare. Tyvärr var jag tvungen att dra in programmet eftersom det namn jag döpte programmet till, och även registrerat som domän, varumärkesregistreades av en bangolfare med samma tanke för bangolfare. (Vid vår mailkonversation råkade jag skriva minigolf, det var ine helt ok.) Man får väl lära sig av misstagen... I samband med detta började Microsoft .Net att bubbla. Jag tänkte att jag tar tillfället i akt och börjar om från början när .Net kommer. (Vilket jag även gjorde när VB5 och VB6 lanserades.) Och det har varit den "strategi" jag har haft, när Microsoft släpper något nytt så börjar jag om. Självklart återanvänder jag domän och data modeller, men koden skriver jag från scratch. Jag började på nytt när Visual Studio för .Net 1.0 beta kom, när skarp version släpptes. Likaså med VS2003, 2005 och 2008. Men nu har jag tröttnat, det blir ju omöjligt med alla tillägg som kommer till ramverken för .Net! Jag vet att det är en ny VS på gång, men nu ska jag bli klar med innevarande version och fokuserar istället på .Net2.0/3.5 Till saken hör att jag med varje version har en ny betaversion som jag själv använder för den kompis turnering jag anordnar, vilken nu är inne på sitt sjätte år. Igen, tillbaka till innehållet. Jag kommer att skriva om tips och trix, saker jag upptäcker, etc. medans jag utvecklar programmet. När jag stöter på problem som inte fungerar som det är tänkt,utan viss trixning, så använder jag bloggen som en tracker över problem jag stöter på under vägen. Teknikerna det handlar om är C#, Linq to Sql, WPF, WC, Asp.Net, Windows Mobile, Sql Server Express/2005/Compact Edition Detta första inlägg blev långt, kanske dags att refaktorera det en smula?

Labels: , ,

kick it on DotNetKicks.com