The most stupid error translation…

My MSSQL Server was installed with polish localization. And an error occured when a XML file had been loaded to the database:

Nieprawidłowa wartość znakowa dla specyfikacji obsady

WTF? It’s in Polish. My mother language is Polish. But I completely don’t understand that gibberish.
With help of Google Translate (Polish > English) I found the original error:

Invalid character value for cast specification

and it’s simple, clear and understandable.

Hurray! An hour spent on nothing, erm… sorry, on googling…

WP8 emulator doesn’t start

Problem:

The Windows Phone Emulator wasn’t able to create a differencing disk:

The process cannot access the file ‘C:\Path\To\VHD\dd.480×800. xxx.vhd’ because it is being used by another process.

Remedy (in my silly case of course):
Exit from project’s ‘bin’ dir that was opened in Total Commander…

Position of the cursor and TextChanged event

Two additional lines are needed if we want to make cursor stay at the end of the line when editing content of TextBox in TextChanged event.

private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
    TextBox txtb = (sender as TextBox);
    // remember cursor location
    int loc = txtb.SelectionStart;
    // do something with txtb.Text;
    txtb.Text = txtb.Text.Replace('$','%');
    // set old location
    txtb.SelectionStart = loc;
}

TextBoxChanged in WP7 always firing twice per change

And it is annoying when you want to implement a custom validation for a simple control. So what is the problem and remedy for it?

Problem: Template for TextBox in WP7 contains… two TextBoxes. One for the enabled state and the other one for disabled / read only state.

Remedy: If you really know that you won’t be using other states than enabled state you can simply remove DisabledOrReadonlyBorder and all code that using this border. For a second option you can change the second TextBox with e.g. an Rectangle and fill it with some semitransparent brush.

Check if string contains any chinese or russian characters

The two really helpful extension methods for this task:

private static readonly Regex regexChinese = new Regex(@"\p{IsCJKUnifiedIdeographs}");
public static bool IsChinese(this char s)
{
     return regexChinese.IsMatch(s.ToString());
}

private static readonly Regex regexCyrlics = new Regex(@"\p{IsCyrillic}");
public static bool IsCyrylic(this char s)
{
     return regexCyrlics.IsMatch(s.ToString());
}

We put the code to a static class and then simply write:

if (sometext.Any(z=>z.IsChinese() || z.IsCyrillic()))
     DoSomething();

You can do the same thing for some other alphabets. Check MSDN.

Difference in capturing groups between Java and C#

In Java I have written string for the replacement in regex like that:
$123
and expecting result would be:
some_captured_string23

In .NET it is not working.
We have to add brackets around group number:
${1}23

Where is the plus and minus in the speakers

It is so simple that I didn’t know about it 😛
We have to take an AA battery (1.5V) and put it on speaker contacts.
•    If we put it from the plus side of speaker the membrane will lean forward.
•    If we put it from the minus side of speaker the membrane will lean backward.

WP7 and enum enumeration with Reflection

In WP7 we don’t have Enum.GetValues method. Below is one of solutions for its absence that returns array of enum initializers:

int[] x = typeof(MyEnum).GetFields().Where(z => z.IsLiteral).Select(z=>(int)z.GetValue(typeof(MyEnum))).ToArray();

Fixing broken Visual Studio’s project publishing mechanism

…  and unexpected repair the debugger.

For quite time I had problems with IE on my laptop (I have various others problems too, as effects of my manipulation with the file system and playing with registry. Reinstalling system is no option for me). IE didn’t start, it flashed white for a second and was closed. Additionally I cannot use ActiveX components. The fix that done the trick was reregister ieproxy.dll. Now IE is running properly but not ActiveX components.

But I had other problems with debugger in Visual Studio. Each time I started debugging project I saw that message:

“Attaching the  ProteusDebugEngine debugger to process ‘[xxx] MyProject.vshost.exe’ on machine ‘MyMachine’ failed. Invalid pointer”

That was really annoying and I couldn’t find how to fix it.

The other problem was that I couldn’t publish my app through VS. VS always throw at me exception below:

“Unable to cast COM object of type ‘System.__ComObject’ to interface type ‘Microsoft.VisualStudio.OLE.Interop.IServiceProvider’. This operation failed because the QueryInterface call on the COM component for the interface with IID ‘{6D5140C1-7436-11CE-8034-00AA006009FA}’ failed due to the following error: No such interface supported (Exception from HRESULT: 0×80004002 (E_NOINTERFACE)). (Microsoft.VisualStudio.OLE.Interop).

After some search and fully reading this post and the comments I had done the following steps:

  1. Used script from page above that reregistered ieproxy and actxprxy [with no effect]
  2. Executed from the command prompt:
    >> sfc /scannow [with no effect after reboot]
  3. Used FixIt app, with option that fixed desktop features & opening desktop files [fixed some errors but with no effect after reboot]
  4. Reregistered again the files from  1. in another way:
    >> regsvr32 %SystemRoot%\System32\actxprxy.dll
    >> regsvr32 %ProgramFiles%\Internet Explorer\ieproxy.dll

Finally something has happened. The path in %ProgramFiles% was right but I coludn’t reregister ieproxy file.

  1.  Renamed both IE folders (x86 and x64) as ‘Internet Explorer0’ and executed:
    >> sfc /scannow [with no effect after reboot]
  2. Reregistered ieproxy:
    >> regsvr32 %ProgramFiles%\Internet Explorer\ieproxy.dll

And that was it! After reboot I could publish my project and when I tried debugging project the debugger had no complaint to me.  I take the thing with fixing debugger as side effect of my previous action, but I don’t know which exactly.

Now time to fix the bug with ActiveX component.

Converting *.mdf database to SQL Compact *.sdf file.

I created some app that store a small amount of data in the database. Database was created as *.mdf file and the requirement of installing on each machine SQL Express engine is unpleasant thing.

I wanted quickly convert my database to SQL Compact format and database had to work with Linq2sql mapping. Exception by exception the goal was achieved.

Step 1. T-SQL database script and the new database

Following that article I downloaded:

  • Export2SqlCe,
  • ExportSqlCE_Addin,
  • SqlCeCmd40

and from the command line used:

  • Export2sqlce “Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True” AW.sqlce
  • sqlcecmd40 -d “Data Source=Database.sdf” -e create
  • sqlcecmd40 -d “Data Source=Database.sdf” -i aw.sqlce > log.txt

 Step 2. New mapping.

 From VS console created manually *.dbml file:

  • sqlmetal /dbml:Database.dbml Database.mdf

The main difference between the old *.dbml file and the newly created is constructor that now can’t be parameterless.

Database dc = new Database("Data Source=|DataDirectory|\\Database\\Database.sdf");

 At this moment I had a exception, something like: “System.Data.SqlServerCe.3.5′ not installed.”.

With some search, I edited app.config and machine.config. At first added support for 3.5, but finally  I stayed with CE 4.0 and the line below:

<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SqlServerCe.4.0" />
<add name="Microsoft SQL Server Compact Data Provider 4.0"
invariant="System.Data.SqlServerCe.4.0"
description=".NET Framework Data Provider for Microsoft SQL Server Compact"
type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
</DbProviderFactories>
</system.data>

3. Create database one more time

That was time for another exception:

“Incompatible Database Version. If this was a compatible file, run repair. For other cases refer to documentation. [ Db version = 4000000,Requested version = 3505053,File name = \\\\?\\D:\\Calc\\Calc\\bin\\Debug\\Database\\Database.sdf ]”

So the simplest solution was create database one more time in the right version, with help of SQL Management Studio  and previously created script.

But I was faced with one more problem (really strange, ne?)

4. Edit *.dbml file

“The table name is not valid. [ Token line number (if known) = 2,Token line offset (if known) = 14,Table name = CalcConst ]”

Solution: Remove from *.dbml file all occurences of “dbo.” phrase.

That’s all. LINQ works with SQL Server CE.


Categories