Monday, April 13, 2009

GetComputerName equivalent in VB.NET and C#

The Win32Api GetComputerName has a simpler equivalent in the .NET world:



Dim compName as String = Environment.MachineName.ToString

Wednesday, April 8, 2009

Getting the Version of an OleDB Provider

There may a much simpler way of determining the version of an OleDb provider (like OraOledb.Oracle.1) than I have shown below, but I have yet to find one.

The OleDbConnection object will tell you the version of the Server through its ServerVersion property:

Dim myConn As New OleDbConnection(g_gtech.GetConnectionString)
myConn.Open()
Dim versionStr as String = myConn.ServerVersion.ToString
myConn.Dispose()


But, I often want to the know the version of the Provider itself. The only way I have found to do this is to sift through the registry and read the ProductVersion off the provider DLL itself.

For example:

Dim versionStr as String = _
GetProviderVersion("OraOleDb.Oracle.1")

Here is the supporting code:


Public Function GetProviderVersion(ByVal providerName As String) As String

Try
Dim clsid As String = GetRegistryValue(Registry.LocalMachine, _
"SOFTWARE\Classes\" + providerName + "\clsid", "")

If clsid.Trim = "" Then Return "Not Installed"
Dim path As String = _
GetRegistryValue(Registry.LocalMachine, "SOFTWARE\Classes\CLSID\" + clsid + "\InprocServer32", "")

Dim Info As FileVersionInfo
Info = FileVersionInfo.GetVersionInfo(path)
Return Info.ProductVersion.ToString

Catch ex As Exception

Return "Unable to get Version"

End Try

End Function

Public Function GetRegistryValue(ByVal regKey As RegistryKey, _
ByVal subKey As String, ByVal valueName As String) As String

Dim value As String = ""
Dim registryKey As RegistryKey = regKey
Dim registrySubKey As RegistryKey
registrySubKey = registryKey.OpenSubKey(subKey)
If registrySubKey IsNot Nothing Then
Try
value = registrySubKey.GetValue(valueName).ToString
Catch ex As Exception
value = ""
End Try
registrySubKey.Close()
End If
Return value
End Function

If there is a simple or more elegant way to find the Provider version, please comment.

Friday, March 27, 2009

Setting the DateTimePicker to a Blank Value

I wanted to use the DateTimePicker control on a data entry form; however, it always has to be set to a value. I did not want to set the date value to Now or some other preset value because it is not clear to the user that the value needs to be set. I found several solutions to this problem, but the one below appears to work and is easy to use.

Initialize the DateTimePicker control to a blank value by setting the Format property to Custom and the CustomFormat property to a space (" "):

DateTimePickerConstDate.CustomFormat = " "
DateTimePickerConstDate.Format = DateTimePickerFormat.Custom


Then, when the value changes, change the Format property back to its original value (or define a usable Custom Format:

Private Sub DateTimePickerConstDate_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePickerConstDate.ValueChanged

DateTimePickerConstDate.Format = DateTimePickerFormat.Short

End Sub


For more information on this solution and other approaches, see the following:

http://social.msdn.microsoft.com/Forums/en-US/netfxcompact/thread/46b2a370-72ec-485c-9361-bfa505bb6863/

Sunday, March 22, 2009

Sorting a VarChar Attribute as a Number in a SQL Statement

This post is not particularly .NET, but I am posting it anyway.

I was needing to sort the results of a SQL statements by a Priority attribute (which has the values 1 to 10). Unfortunately, the Priority attribute is defined as VarChar, so 10 always follows 1 when you do an ORDER BY Priority.

There are many solutions to this problem, but this simplest (although not the more readily obvious, and maybe not the most efficient) is this:

SELECT * FROM Data_Table ORDER BY Len(Priority), Priority ASC

Another approach is:

SELECT * FROM Data_Table ORDER BY RIGHT('0000' + RTRIM(LTRIM(Priority)), 4)


There are other approaches in the posts where I found this solution:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81901

http://www.eggheadcafe.com/conversation.aspx?messageid=29907381&threadid=29907381

Thursday, February 12, 2009

Appending a File with StreamWriter

The StreamWriter class has an Append flag that can be set to append to the file:


Dim outfile As New StreamWriter(filename, True)


The are other overloaded versions with the append flag as well.

Wednesday, December 17, 2008

Utility: Process Explorer

This article in PC Magazine discusses a utility called the Process Explorer (free from Sysinternals):

http://www.pcmag.com/article2/0,2817,2332918,00.asp


The utility can be downloaded from:

http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx


This utility is like Task Manager's Process Viewer, only it tells much more information that can be used to diagnose problems.

Tuesday, November 18, 2008

How to determine the .NET Compact Framework Version on the Device

Here is the best posting I have found to determine the version of the .NET Compact Framework on the device:

http://www.byteswired.com/2007/09/27/how-to-determine-compactframework-version/


The summary is that you look at the registry key:

HKLM\SOFTWARE\Microsoft\.NetCompactFramework