More ASP.Net tricks
The developer.com article had some really nice stuff thanks to Karl Moore. I have clipped parts of the article and these can be found after the jump. For the full articles you can goto:
http://www.developer.com/net/asp/article.php/1594521
Using .IsClientConnected for Long Processes
If you're performing a long process or calculation behind a Web page, it's a good idea to periodically check whether your client is still connected. You may want to do this at set intervals in your code, or just before sending your results down to the browser.
You can do this through the innovatively titled function, Response.IsClientConnected. It'll return a True if the client is still connected to the server. If it isn't, you'll want to exit your code and run a Response.End.
Unveiled: How to Create a Default 'Enter' Button!
This is one of those little code snippets you can pull your hair out trying to find. And no help file nor book I've come across actually gives reference to it. So, surely it can't be that important?
Imagine you've created an ASP.NET Web page with a search button. The user taps a phrase into a text box and presses Enter. On most regular Web pages (think: Google), the form would be submitted and the results returned. In other words, the search button is automatically "clicked" for you.
However on an ASP.NET Web page, pressing Enter resubmits the form to the server, but actually does nothing... which is pretty useless, really.
So, how do you set a default button to be clicked when the user presses Enter? Simply add the following line to your page's Load event, replacing "btnSearch" with the name of your button. It uses a hidden Page method called RegisterHiddenField and works splendidly:
Code: |
Page.RegisterHiddenField("__EVENTTARGET", "btnSearch") |
Generating GUIDs in a Flash
GUIDs are globally unique identifiers, 128-bit integers that are automatically generated based on around two zillion frequently varying factors. In brief, they're useful when you need a value you can be assured will not match any other, anywhere. Probably.
The SQL Server data type 'uniqueidentifier' stores a GUID. You can either generate this value within SQL Server, using the NEWID() function (perhaps you could specify the function as the default value for all new rows)—or you can generate the GUID outside of SQL Server and insert it manually.
If you're doing the latter, this tip can help out. Here's a function for instantly generating your own GUID in VB.NET:
Code: |
Public Function GetGUID() As String ' Returns a new GUID Return System.Guid.NewGuid.ToString End Function |
Finding the Last Identity Number Added SMR NOTE: This also works with OLEDB and Access
Everyone has this problem: you've just added a record to your SQL Server database and need to figure out what the automatically generated identity number was, so you can use the value as a foreign key in some child table.
It took me a good while to figure this one out. You simply need to issue the "select @@identity" command to SQL Server and it'll return a one-field response that contains the last identity number added during your connection.
Let's look at a commented example:
Code: |
' Variable to hold the identity value of our record Dim MyIdentityValue As Integer ' Set up sample connection and command Dim objConnection As New SqlClient.SqlConnection( _ "server=NEMEAN;database=MYDATABASE;" & _ "user ID=USERID;password=PWD") Dim objCommand As New SqlClient.SqlCommand( _ "INSERT INTO author (authorname) " & _ "VALUES('Karl Moore')") ' Open connection and execute INSERT command objConnection.Open() objCommand.Connection = objConnection ' Execute and check minimum of one record affected... If objCommand.ExecuteNonQuery > 0 = True Then ' Set up separate command to retrieve identity value Dim objIdentifier As New _ SqlClient.SqlCommand("Select @@Identity", objConnection) Try ' Return value of field MyIdentityValue = objIdentifier.ExecuteScalar Catch MyIdentityValue = 0 End Try End If ' Close connection objConnection.Close() |
|