Code Repo    |     RSS
MD's Technical Sharing



Friday, June 26, 2009

System.InvalidOperationException is thrown when attempting simultaneous calls to ExecuteNonQuery

If you have multiple threads trying to execute an SqlCommand via ExecuteNonQuery using a single SqlConnection object, you may get the following exception:

System.InvalidOperationException: ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.

The following code replicates the problem:

Dim conn As New SqlConnection("Data Source=localhost;User Id=sa; Password=; Initial Catalog=test")

Sub Main()
conn.Open()

For i As Integer = 0 To 1000
Dim th As New Threading.Thread(AddressOf TestSqlThread)
th.Start(i)
NextEnd Sub

Private Sub TestSqlThread(ByVal state As Object)
Console.WriteLine(state)
Dim cmdLocal As New SqlCommand("INSERT INTO testtable(col1, col2) VALUES('H', 'Hello2')", conn)
cmdLocal.ExecuteNonQuery()
cmdLocal.Dispose()
End Sub


What the above code does is to open a single connection and create multiple threads which call ExecuteNonQuery on the connection which was created. The multiple threads do not wait for each other - they execute concurrently. The previous call to ExecuteNonQuery may not have been finished before the next one comes in. So there may be concurrent attempts to call ExecuteNonQuery to update the database. .NET only supports up to a maximum number of concurrent calls to ExecuteNonQuery on a single SqlConnection object. When the limit is reached, the exception mentioned above is thrown.

A workaround is to use a lock to make sure you call ExecuteNonQuery one after another, and not concurrently:

Dim mylock as new object
Private Sub TestSqlThread(ByVal state As Object)
Console.WriteLine(state)
Dim cmdLocal As New SqlCommand("INSERT INTO testtable(col1, col2) VALUES('H', 'Hello2')", conn)
Synclock MyLock
cmdLocal.ExecuteNonQuery()
End Synclock
cmdLocal.Dispose()
End Sub


However, this will have a performance impact as the query will be executed one by one and slow down the application. A better alternative would be to create a separate SqlConnection object for each thread that needs to call ExecuteNonQuery.
Read More »

Monday, June 22, 2009

Hard-reset Windows Mobile devices

This guide shows you how to hard reset various different phones.


HTC Touch


1. Power on.
2. Press and hold buttons marked 1.
3. Press and release 2 with stylus.
4. Release buttons marked 1 when menu comes up.
5. Press directly down on center dpad, marked 3.

Reference: http://blog.ryjones.org/2008/01/14/htc-touch-hard-reset/

Motorola Q9h:


With the device powered off, press and hold the E and Z buttons and turn the device on by pressing the RED key (hangup button). You may need to try this a few times.


Reference: http://blogs.lanlogic.net/blogs/ted/archive/2007/12/12/how-to-hard-reset-a-motorola-q9h.aspx


Dopod 577W:


With the device powered off, press and hold both the soft keys. Without releasing the soft keys, press the on/off button for about 0.5 seconds and release it.

A message will be displayed, prompting user to press "0" (zero) key to restore factory settings. Release the soft keys and press 0 (zero) key. The phone will hard reset

Reference: http://www.ppcsg.com/index.php?showtopic=77294


Dopod 838Pro

  1. Turn the power off
  2. Hold down the 2 softkey buttons together
  3. Poke the reset button
  4. Confirm the hard reset by pressing the R button on the keyboard.
  5. Press the space button on the keyboard to reset.

Reference: http://docfiles.blogspot.com/2006/08/how-to-hard-reset-dopod-838-pro.html


Samsung Omnia


Method 1


1. Poke the soft reset hole beside your power button(Poke and Release)
2. Press and hold the Call and Cancel Button together(Do it immediately after Step 1)
3. Do not release Call and Cancel button, the Samsung Omnia Screen Appear

4. Press Call to Format or End Call to Cancel.


This hard reset does not erase the 16GB memory.


Method 2


Key in the following code on your Dial Pad *2767*3855#. Phone will be restarted and reset to defaulf factory settings straight away.


Reference: http://www.pspmyspace.com/omnia/2008/09/how-to-hard-res.html


LG KS-20


1. Turn off the phone completely by pressing and holding the power button and wait for the prompt.
2. Press and hold these three buttons–volume down, Call and power–till the LED (beside the front camera) flashes.
3. Release the buttons. That same LED should flash blue/red/green and power up.
4. The KS20 should be in a clean state.


Reference: http://freshgear.wordpress.com/2008/03/25/tips-how-to-hard-reset-lg-ks-20/


For all phones:


  • PocketPC: Start->Settings->System->Clear Storage/Hard-reset.
  • Smartphone: Start->Accessories->Clear Storage or Start->System Tools->Master Clear/Master Reset

Actual names of menu items may vary slightly depending on device manufacturer.

Reference: http://www.hardresetguide.com/

Read More »

Wednesday, June 10, 2009

Disable USB Charging on Windows Mobile devices

If your device can charge over USB, you can probably disable this feature by setting:


HKLM\Drivers\BuiltIn\usbfndrv\EnableUsbCharging = 0 (DWORD decimal)

And to re-enable:

HKLM\Drivers\BuiltIn\usbfndrv\EnableUsbCharging = 1 (DWORD decimal)
Read More »

Monday, June 8, 2009

Animated gifs in .NET Compact Framework

By default the .Net Compact framework does not support displaying GIF based animations on a Windows Form. It is possible to code up a custom animator that will essentially do the same. Here's a link to a sample class for displaying a GIF on the compact framework.

http://msdn.microsoft.com/en-us/library/aa446483.aspx

This does not work with all GIF files, however. A better approach would be to use a WebBroswer control to display the animated picture.

The full .NET framework, however, supports animated GIF natively.

Read More »

Monday, June 1, 2009

New Features in VB 9.0 and C# 3.0

The following is a list of some new language-specific features available to C# 3.0 and VB 9.0, available in Visual Studio 2008, including Express editions.

Both VB and C#:
Extension methods
Anonymous Types
Local variable type inference

Object Initializers

Automatic properties
Lambda Expressions
Partial methods in VB and C#

VB Only:
Read More »