Visual Basic Q&A

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries Tagged ‘value’

BUG: The Text property of a data-bound ComboBox returns an incorrect value when you set the value in code

Symptoms
If you programmatically set the Text property for a data-bound member of the ComboBox control, the value typed in the text box portion of the ComboBox is displayed as expected. However, the Text property and the SelectedIndex property incorrectly return the value of the last item that was selected in the list box instead of returning the value typed in the text box.
When you type in the ComboBox at runtime, Text property returns the typed value, and SelectedIndex returns a value of -1.
Resolution
To resolve this problem, set the SelectedIndex property to -1 before you set the Text property for a data-bound member of the ComboBox, as in the following examples. Visual Basic .NET

ComboBox1.SelectedIndex = -1ComboBox1.Text = “My Text” Visual C# .NET

ComboBox1.SelectedIndex = -1;ComboBox1.Text = “My Text”; NOTE: Do not use the Text property to select a data-bound member of the ComboBox. You must locate the item in the list that you want to show, and then set the SelectedIndex to the index of the item. You do not have to set the Text property.
For example, if you bind the DisplayMember property and the ValueMember property of the ComboBox to a list that contains the numbers 1 through 10, and you want the ComboBox to display the number 5 in the text box and show 5 as selected in the list, you must set the SelectedIndex property to 5.

BUG: Error “Row Cannot Be Located for Updating” If You Change Numeric Field in ADODC Recordset

Symptoms
If an ActiveX Data Objects data control (ADODC) is bound to a Microsoft Access table that specifies a default value for the numeric field, when you take the following actions:Add a new record to the ADO recordset, which the ADODC exposes, without entering a value for that numeric field.Update the recordset.Type a value into the numeric field (either directly into the Recordset field or into the corresponding DataGrid cell) in that same newly-added record.Update the recordset again.the following run-time error is raised with error number -2147217864 (80040e38) or 6153:

Row cannot be located for updating. Some values may have changed since it was last read.If a DataGrid control is bound to the ADODC, the same error message usually appears a second time without an error number in a dialog box that is entitled “Microsoft DataGrid Control.”
Resolution
To resolve this problem, remove the default value that is specified for the numeric field in the Access database table.
Alternately, you can run an UPDATE statement on a separate ADO Connection object to update the numeric field in the newly-added record directly in the database and then refresh the ADODC.

PRB: Error When You Update or Delete New Rows in Access 97 Table

Symptoms
When you use ADO to edit and then update or delete newly added records in a Microsoft Access 97 table, you may receive the following error message when you call the Update method:

Run-time error ‘-2147217864 (80040e38)’:
Row cannot be located for updating. Some values may have been changed since it was last read.This error message may also occur when you edit or delete newly added records in a DataGrid control. The DataGrid control issues an Update behind the scenes when you move to another row.
NOTE: This error does not occur when you edit or delete existing records.
Resolution
The Access 97 table contains an AutoNumber field, and the auto-incremented values of newly added records may not be available for the client recordset. For example, if the AutoNumber field is 5 for the new record in the table, the value of this field in the recordset is always 0. ADO uses this value to locate the record when ADO builds an Update Action Query and sends the value to the Microsoft Jet engine to perform the Update on the specified record. However, the Jet engine cannot locate the record based on that value; thus, you receive the above-mentioned error message.

Output parameters are not returned when you run an ADO.NET command in Visual Basic

Symptoms
Output parameters do not appear to be initialized or return a wrong value when executing an ADO.NET command.
Resolution
This problem can occur for the following reasons: Output parameters are returned at the end of the data stream when using a DataReader object.The Direction property of the parameter is not set properly.

How To Use the Registry API to Save and Retrieve Setting

Symptoms
Although Visual Basic includes the SaveSetting and GetSetting functionsto save and retrieve information from the registry, these functions onlyoperate on a specific section of the registry, the Visual Basic and VBAProgram Settings of the HKEY_CURRENT_USER root key.
This article outlines the use of 32-bit Windows API functions, which can beused to set and retrieve values from anywhere in the registry. The topicsand function references in this article can be generalized to program the16-bit registry.
The 32-bit API functions also include support for security, although anoverview of security is outside the scope of this article.
NOTE: The SaveSetting and GetSetting functions are not part of the VBAfunction library. However, the sample code below still applies to 32-bitapplications that implement VBA.
Resolution
General Registry InformationThe registry is used by applications and Windows to store configurationdata. It is a replacement for the large numbers of INI files thatproliferated on Windows 3.x machines and is also used heavily by OLE.
The registry is organized using a hierarchical series of keys and valuesresembling a tree. Each key, beginning with one of the six predefined rootkeys, can have sub-keys and values associated with it. The keys areorganizational and naming units and appear in the Windows Registry Editorsas file folders. Values are data entries and appear as text entries in theright pane of the Registry Editor window. Keys need not have any associatedvalues, but may have many. Each value has an associated data type. The twomost commonly used registry data types are REG_SZ, a null-terminatedstring; and REG_DWORD, a 32-bit number.
The basic process used to write or read from a location in the registry isthe same. To reference any given key or value, you must have a handle tothe key. Once this handle is obtained, values and sub-keys of the key thatthis handle refers to can be read, set, or listed (enumerated).
Given a location in the registry, to obtain a handle to that key, you mustbegin with one of the six predefined keys (HKEY_CLASSES_ROOT,HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CURRENT_CONFIG, andHKEY_DYN_DATA) and traverse the registry tree until the desired key isreached. User programs most often read and write from HKEY_CURRENT_USER andHKEY_LOCAL_MACHINE. If the keys being traversed exist already, you can usea series of calls to the RegOpenKey or RegOpenKeyEx functions. If the keysneed to be created, the RegCreateKey and RegCreateKeyEx functions do the job.
With the handle to the desired key, the functions used to list, set, andretrieve information can be called. In all cases, the functions with the Exsuffix will work only on 32-bit platforms. Functions without the suffix maywork on both 16-bit and 32-bit versions of Windows. Keep in mind that notall registry functions lacking the ‘Ex’ suffix are functions provided for16-bit compatibility. The Ex suffix was only added when the capabilities of16-bit functions were expanded. Functions that are totally new and specificto 32-bit platforms do not possess an Ex extension.
The RegSetValue and RegSetValueEx functions allow the settings of a valueto be modified, while RegQueryValue and RegQueryValueEx retrieve thecurrent setting of a value. The limitations of the non-Ex, 16-bit versionsof these APIs are very evident here. When using the 16-bit RegSetValuefunction there is no way to name a value, and because of this, RegSetValuecan’t be used to associate more than one value with each key. In addition,all values written with RegSetValue have a data type of REG_SZ. Theselimitations are inherent with the 16-bit Registry. RegSetValueEx allows thecreation of a multiple number of values with any available data type.
How to Write to a Specific Registry LocationAfter determining what functions you will need to use for your project,copy the relevant declares from the code at the end of this article to abasic module. The two Visual Basic procedures included (SetValueEx andQueryValueEx) are wrappers for the RegSetValueEx and RegQueryValueEx APIfunctions and greatly simplify their use. The notes below make use of theseVisual Basic functions; however, you are free to make calls directly to theAPI if you wish.
Creating/Modifying Keys and Values:
With the declarations and procedures available, you can create and openkeys, and add, modify, and read values. The three following sectionsexplain how to create a key, set or modify a value, and query a value.
Creating a New Key:
Creating a new key is as simple as using the following procedure.CreateNewKey takes the name of the key to create, and the constantrepresenting the predefined key to create the key under. The call toRegCreateKeyEx doesn’t take advantage of the security mechanisms allowed,but could be modified to do so. A discussion of Registry security isoutside the scope of this article.

Private Sub CreateNewKey (sNewKeyName As String, lPredefinedKey As Long)Dim hNewKey As Long’handle to the new keyDim lRetVal As Long’result of the RegCreateKeyEx functionlRetVal = RegCreateKeyEx(lPredefinedKey, sNewKeyName, 0&, _vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, _0&, hNewKey, lRetVal)RegCloseKey (hNewKey)End Sub
With this procedure a call of:

CreateNewKey “TestKey”, HKEY_LOCAL_MACHINE
will create a key called TestKey immediately under HKEY_LOCAL_MACHINE.
Calling CreateNewKey like this:

CreateNewKey “TestKey\SubKey1\SubKey2″, HKEY_LOCAL_MACHINE
will create three-nested keys beginning with TestKey immediately underHKEY_CURRENT_USER, SubKey1 subordinate to TestKey, and SubKey3 underSubKey2.
Setting/Modifying a Value:
Creating and setting a value of a specified key can be accomplished withthe following short procedure. SetKeyValue takes the key that the valuewill be associated with, the name of the value, the setting of the value,and the type of the value (the SetValueEx function only supports REG_SZ andREG_DWORD, but this can be modified if necessary). Specifying a new valuefor an existing sValueName will modify the current setting of that value.

Private Sub SetKeyValue (sKeyName As String, sValueName As String, _vValueSetting As Variant, lValueType As Long)Dim lRetVal As Long’result of the SetValueEx functionDim hKey As Long’handle of open key’open the specified keylRetVal = RegOpenKeyEx(HKEY_CURRENT_USER, sKeyName, 0, _KEY_SET_VALUE, hKey)lRetVal = SetValueEx(hKey, sValueName, lValueType, vValueSetting)RegCloseKey (hKey)End Sub
A call of:

SetKeyValue “TestKey\SubKey1″, “StringValue”, “Hello”, REG_SZ
will create a value of type REG_SZ called “StringValue” with the setting of”Hello.” This value will be associated with the key SubKey1 of “TestKey.”
In this case, “TestKey” is a subkey of HKEY_CURRENT_USER, but this can bemodified by changing the call to RegOpenKeyEx. This call will fail if”TestKey\SubKey1″ does not exist. To avoid this problem, use a call toRegCreateKeyEx instead of a call to RegOpenKeyEx. RegCreateKeyEx will opena specified key if it already exists.
Querying a Value:
The next procedure can be used to ascertain the setting of an existingvalue. QueryValue takes the name of the key and the name of a valueassociated with that key and displays a message box with the correspondingvalue. It uses a call to the QueryValueEx wrapper function defined below,that only supports REG_SZ and REG_DWORD types.

Private Sub QueryValue (sKeyName As String, sValueName As String)Dim lRetVal As Long’result of the API functionsDim hKey As Long’handle of opened keyDim vValue As Variant’setting of queried valuelRetVal = RegOpenKeyEx(HKEY_CURRENT_USER, sKeyName, 0, _KEY_QUERY_VALUE, hKey)lRetVal = QueryValueEx(hKey, sValueName, vValue)MsgBox vValueRegCloseKey (hKey)End Sub
With this procedure, a call of:

QueryValue “TestKey\SubKey1″, “StringValue”
will display a message box with the current setting of the “StringValue”value, and assumes that “StringValue” exists in the “TestKey\SubKey1″ key.
If the Value that you query does not exist then QueryValue will return anerror code of 2 – ‘ERROR_BADKEY’.
Additional Notes:
The above examples use the extended 32-bit versions of the registryfunctions exclusively. These functions allow more than one value to beassociated with each key. As discussed above, the 16-bit RegSetValue andRegQueryValue act on a single value associated with the current key (whichis always of the type REG_SZ). These functions appear in the 32-bitRegistry Editor with a name of <NO NAME>. To set, modify, or query thisspecial associated value, one must use the 16-bit registry functions.Reading and writing from the registry in a 16-bit environment is muchsimpler than in a 32-bit environment. The same basic procedure is followed:open a key and get a handle and then call your modification function withthat handle, but no consideration needs to be made for multiple associatedvalues or for different value data types. A 16-bit application can createand modify keys and values with the declarations of the RegCreateKey,RegOpenKey, RegQueryValue, RegSetValue, and RegCloseKey functions.
In some cases, there is no need for any values to be associated with a key.An application may only need to know if a certain key or value exists, andnot care about the nature of the key’s values. In a situation like this,the RegEnumKey, RegEnumKeyEx, and RegEnumValue functions can be used todetermine whether a certain key or value exists. For more information onthese functions refer to the API Text Viewer and/or Windows API reference.
API Function and Constant Declarations

Option ExplicitPublic Const REG_SZ As Long = 1Public Const REG_DWORD As Long = 4Public Const HKEY_CLASSES_ROOT = &H80000000Public Const HKEY_CURRENT_USER = &H80000001Public Const HKEY_LOCAL_MACHINE = &H80000002Public Const HKEY_USERS = &H80000003Public Const ERROR_NONE = 0Public Const ERROR_BADDB = 1Public Const ERROR_BADKEY = 2Public Const ERROR_CANTOPEN = 3Public Const ERROR_CANTREAD = 4Public Const ERROR_CANTWRITE = 5Public Const ERROR_OUTOFMEMORY = 6Public Const ERROR_ARENA_TRASHED = 7Public Const ERROR_ACCESS_DENIED = 8Public Const ERROR_INVALID_PARAMETERS = 87Public Const ERROR_NO_MORE_ITEMS = 259Public Const KEY_QUERY_VALUE = &H1Public Const KEY_SET_VALUE = &H2Public Const KEY_ALL_ACCESS = &H3FPublic Const REG_OPTION_NON_VOLATILE = 0Declare Function RegCloseKey Lib “advapi32.dll” _(ByVal hKey As Long) As LongDeclare Function RegCreateKeyEx Lib “advapi32.dll” Alias _”RegCreateKeyExA” (ByVal hKey As Long, ByVal lpSubKey As String, _ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions _As Long, ByVal samDesired As Long, ByVal lpSecurityAttributes _As Long, phkResult As Long, lpdwDisposition As Long) As LongDeclare Function RegOpenKeyEx Lib “advapi32.dll” Alias _”RegOpenKeyExA” (ByVal hKey As Long, ByVal lpSubKey As String, _ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As _Long) As LongDeclare Function RegQueryValueExString Lib “advapi32.dll” Alias _”RegQueryValueExA” (ByVal hKey As Long, ByVal lpValueName As _String, ByVal lpReserved As Long, lpType As Long, ByVal lpData _As String, lpcbData As Long) As LongDeclare Function RegQueryValueExLong Lib “advapi32.dll” Alias _”RegQueryValueExA” (ByVal hKey As Long, ByVal lpValueName As _String, ByVal lpReserved As Long, lpType As Long, lpData As _Long, lpcbData As Long) As LongDeclare Function RegQueryValueExNULL Lib “advapi32.dll” Alias _”RegQueryValueExA” (ByVal hKey As Long, ByVal lpValueName As _String, ByVal lpReserved As Long, lpType As Long, ByVal lpData _As Long, lpcbData As Long) As LongDeclare Function RegSetValueExString Lib “advapi32.dll” Alias _”RegSetValueExA” (ByVal hKey As Long, ByVal lpValueName As String, _ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As _String, ByVal cbData As Long) As LongDeclare Function RegSetValueExLong Lib “advapi32.dll” Alias _”RegSetValueExA” (ByVal hKey As Long, ByVal lpValueName As String, _ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, _ByVal cbData As Long) As Long
SetValueEx and QueryValueEx Wrapper Functions:

Public Function SetValueEx(ByVal hKey As Long, sValueName As String, _lType As Long, vValue As Variant) As LongDim lValue As LongDim sValue As StringSelect Case lTypeCase REG_SZsValue = vValue & Chr$(0)SetValueEx = RegSetValueExString(hKey, sValueName, 0&, _lType, sValue, Len(sValue))Case REG_DWORDlValue = vValueSetValueEx = RegSetValueExLong(hKey, sValueName, 0&, _lType, lValue, 4)End SelectEnd FunctionFunction QueryValueEx(ByVal lhKey As Long, ByVal szValueName As _String, vValue As Variant) As LongDim cch As LongDim lrc As LongDim lType As LongDim lValue As LongDim sValue As StringOn Error GoTo QueryValueExError’ Determine the size and type of data to be readlrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch)If lrc <> ERROR_NONE Then Error 5Select Case lType’ For stringsCase REG_SZ:sValue = String(cch, 0)lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, _sValue, cch)If lrc = ERROR_NONE ThenvValue = Left$(sValue, cch-1)ElsevValue = EmptyEnd If’ For DWORDSCase REG_DWORD:lrc = RegQueryValueExLong(lhKey, szValueName, 0&, lType, _lValue, cch)If lrc = ERROR_NONE Then vValue = lValueCase Else’all other data types not supportedlrc = -1End SelectQueryValueExExit:QueryValueEx = lrcExit FunctionQueryValueExError:Resume QueryValueExExitEnd Function

Description of race conditions and deadlocks

Symptoms
Visual Basic .NET or Visual Basic 2005 offers the ability to use threads in Visual Basic applications for the first time. Threads introduce debugging issues such asrace conditions and deadlocks. This article explores these two issues.
Resolution
Race ConditionsA race condition occurs when two threads access a shared variable at the same time. The first thread reads the variable, and the second thread reads the same value from the variable. Then the first thread and second thread perform their operations on the value, and they race to see which thread can write the value last to the shared variable. The value of the thread that writes its value last is preserved, because the thread is writing over the value that the previous thread wrote.Details and ExampleEach thread is allocated a predefined period of time to execute on a processor. When the time that is allocated for the thread expires, the thread’s context is saved until its next turn on the processor, and the processor begins the execution of the next thread.
How can a one-line command cause a race condition? Examine the following example to see how a race condition occurs. There are two threads, and both are updating a shared variable called total (which is represented as dword ptr ds:[031B49DCh] in the assembly code).
Visual Basic code:

‘Thread 1Total = Total + val1

‘Thread 2Total = Total – val2 Assembly code (with line numbers) from the compilation of the preceding Visual Basic code:

‘Thread 1 1.moveax,dword ptr ds:[031B49DCh]2.addeax,edi3.jno000000334.xorecx,ecx5.call7611097F6.movdword ptr ds:[031B49DCh],eax

‘Thread 2 1.moveax,dword ptr ds:[031B49DCh]2.subeax,edi3.jno000000334.xorecx,ecx5.call76110BE76.movdword ptr ds:[031B49DCh],eax By looking at the assembly code, you can see how many operations the processor is performing at the lower level to execute a simple addition calculation. A thread may be able to execute all or part of its assembly code during its time on the processor. Now look at how a race condition occurs from this code.
Total is 100, val1 is 50, and val2 is 15. Thread 1 gets an opportunity to execute but only completes steps 1 through 3. This means that Thread 1 read the variable and completed the addition. Thread 1 is now just waiting to write out its new value of 150. After Thread 1 is stopped, Thread 2 gets to execute completely. This means that it has written the value that it calculated (85) out to the variable Total. Finally, Thread 1 regains control and finishes execution. It writes out its value (150). Therefore, when Thread 1 is finished, the value of Total is now 150 instead of 85.
You can see how this might be a major problem. If this were a banking program, the customer would have money in their account that should not be present.
This error is random, because it is possible for Thread 1 to complete its execution before its time on the processor expires, and then Thread 2 canbegin its execution. If these events occur, the problem does not occur.Thread execution is nondeterministic, therefore you cannot control the time or order of execution. Also note that the threads may execute differently in runtime versus debug mode. Also, you can see that if youexecute each thread in series, the error does not occur. This randomness makes these errors much harder to track down and debug.
To prevent the race conditions from occurring, you can lock shared variables, so that only one thread at a time has access to the shared variable. Do this sparingly, because if a variable is locked in Thread 1 and Thread 2 also needs the variable, Thread 2’s execution stops while Thread 2 waits for Thread 1 to release the variable. (For more information, see “SyncLock” in the “References” section of this article.)SymptomsThe most common symptom of a race condition is unpredictable values of variables that are shared between multiple threads. This results from the unpredictability of the order in which the threads execute. Sometime one thread wins, and sometime the other thread wins. At other times, execution works correctly. Also, if each thread is executed separately, the variable value behaves correctly.
DeadlocksA deadlock occurs when two threads each lock a different variable at the same time and then try to lock the variable that the other thread already locked. As a result, each thread stops executing and waits for the other thread to release the variable. Because each thread is holding the variable that the other thread wants, nothing occurs, and the threads remain deadlocked.Details and ExampleThe following code has two objects, LeftVal and RightVal:

‘Thread 1SyncLock LeftVal SyncLock RightVal’Perform operations on LeftVal and RightVal that require read and write. End SyncLockEnd SyncLock

‘Thread 2SyncLock RightVal SyncLock LeftVal’Perform operations on RightVal and LeftVal that require read and write. End SyncLockEnd SyncLock A deadlock occurs when Thread 1 is permitted to lock LeftVal. The processor stops Thread 1’s execution and begins the execution of Thread 2. Thread 2 locks RightVal and then tries to lock LeftVal. Because LeftVal is locked, Thread 2 stops and waits for LeftVal to be released. Because Thread 2 is stopped, Thread 1 is permitted to continue executing. Thread 1 tries to lock RightVal but cannot, because Thread 2 has locked it. As a result, Thread 1 starts to wait until RightVal becomes available. Each threadwaits for the other thread, because each thread has locked the variable that the other thread is waiting on, and neither thread is unlocking the variable that it is holding.
A deadlock does not always occur. If Thread 1 executes both locks before the processor stops it, Thread 1 can perform its operations and then unlock the shared variable. After Thread 1 unlocks the variable, Thread 2 can proceed with its execution, as expected.
This error seems obvious when these snippets of code are placed side by side, but in practice, the code may appear in separate modules or areas of your code. This a very hard error to track down because, from this same code, both correct execution and incorrect execution can occur.SymptomsA common symptom of deadlock is that the program or group of threads stops responding. This is also known as a hang. At least two threads are each waiting for a variable that the other thread locked. The threads do not proceed, because neither thread will release its variable until it gets the other variable. The whole program can hang if the program is waiting on one or both of those threads to complete execution.
What Is a Thread?Processes are used to separate the different applications that are executing at a specified time on a single computer. The operating system does not execute processes, but threads do. A thread is a unit of execution. The operating system allocates processor time to a thread for the execution of the thread’s tasks. A single process can contain multiple threads of execution. Each thread maintains its own exception handlers, scheduling priorities, and a set of structures that the operating system uses to save the thread’s context if the thread cannot complete its execution during the time that it was assigned to the processor. The context is held until the next time that the thread receives processor time. The context includes all the information that the thread requires to seamlessly continue its execution. This information includes the thread’s set of processor registers and the call stack inside the address space of the host process.