Archiv der Kategorie: English

Article in english

Slippery When Wet #2: SCOPE_IDENTITY and SqlDataReader

I proudly present to you the second in a infinite number of posts of “Slippery When Wet.” In these posts I show you a little bastard I stubled on.

Imagine you have a database table with a cloumn id of the data type integer that has set the IDENTITY. You use a stored procedure to insert a new entry into this table. Inside this stored procedure you use the SCOPE_IDENTITY() function to get the identifier created for this row. With RETURN SCOPE_IDENTITY() you give the identity to the caller.

You call this stored procedure from a SqlComand with ExecuteReader() that returns you an SqlDataReader object.

From this SqlDataReader you read now the identifier with GetInt32().

Wrong !

This will give you an InvalidCastException. SCOPE_IDENTITY()’s return type is numeric, although your identifier column is an integer. SqlDataReader’s GetXY functions do not convert the data and throw the exception when the data is not already of the right type.

The first solution

You can read the value with GetDecimal() and cast the value to an int:

int identifier = (int)reader.GetDecimal(0);

The second solution

You cast the identifier inside the stored procedure und return it already as an integer:

SELECT CAST(SCOPE_IDENTITY() AS INT)

Whatever solution you choose, take care that you use always the same inside your application.

Slippery When Wet #1: Javascript’s GetMonth()

I proudly present to you the first in a infinite number of posts of „Slippery When Wet.“ In these posts I show you a little bastard I stubled on.

I had to reformat a date in Javascript. There were all this little nice functions of a date object like

getFullYear(), getHours(), getMinutes()

and also

getMonth()

. Every of these functions returned the value as it would also be shown, but not getMonth(). getMonth() returns a value from 0 to 11, so 0 is January, 1 is February and so on untill 11, that is December.

I see a possible reason for this: With values from 0 to 11 you can use the value directly as the index for an array with the month names inside, like

alert(months[myDate.getMonth()]);

no update for database row if parameter is null

Sometimes you want write a stored procedure to update a database entry, but you want be able to leave some columns unchanged. If you don’t need to set them back to null you can use the following code:

UPDATE TABLE
  SET Column1 = ISNULL(@parameter1, Column1), Column2 = ISNULL(@parameter2, Column2)
  WHERE PrimaryKey = @KEY

The command ISNULL uses the first parameter, if it is not null, or the second parameter if the first is null. When you pass a value, this value is used, but when you pass null the current value of this column is used, so it overwrites it with the same value.

Exception after sending data async from a thread

If you have a situation like this:

void MyThreadFunc()
{
// do some work

socket.BeginSend(buffer, 0, buffer.Length, 0, new AsyncCallback(MyCallback), s);
}

void MyCallback(IAsyncResult ar)
{
Socket s = (Socket) ar.AsyncState;
s.EndSend(ar);
}

And you send a big buffer over a slow connection it can happen that calling EndSend() will throw a SocketException ‚The I/O operation has been aborted because of either a thread exit or an application request‘.

This happens because the Thread that started the sending is no more available when the sending ends. The only solution I know is to make sure the thread is still available or to use syncronous sending if possible.

WordPress aktualisiert / Worpress updated

Heute habe ich endlich WordPress auf Version 2.3 aktualisiert. Zudem habe ich das Plugin Ultimate Google Analytics installiert um Statistiken über mein Blog mittels Google Analytics zu erhalten.
Today i’ve updated WordPress to the Version 2.3. I’ve also installed the PlugIn Ultimate Google Analytics to gather Statistics for my blog with Google Analytics.

Exception „No web service found at:“ with /js or /jsdebug

With „ASP.NET 2.0 AJAX Extensions 1.0“ (I love this name with two version numbers inside…) you can request a JavaScript Proxy for a web service with appending /js or /jsdebug to the web service path, like „http://my-server/my-service.asmx/js“.

When I deployed my application I’ve got the InvalidOperationException „No web service found at:“. When I called the web service without /js or /jsdebug the web service worked (at least the generated forms in the Browser where shown).

I’ve spent a lot of time ‚til I recognized the File my-service.asmx was not included in the deploy package. So without the /js appended the my-service.asmx file was not needed, the code from the compiled dll was executed directly, but when I added /js it needed the file.

Conclusion:
Don’t forget to add the .asmx files in the deployment of your ASP.NET AJAX Project!