Friday, November 24, 2006

Using the registry from a VB.Net application

Creating the demo project

If you want to build this demo yourself, start by creating a user interface looking like the image you see here. Don't forget that a complete demo is available for download at the end of this article.

At the top of the code in your form, you need to add the line to imports the namespace:

Imports Microsoft.Win32

Adding values to the registry

The code needed to create a key into the registry and place some values in it is very simple (this code can be placed into the Click event of a button):

Dim oReg As RegistryKey
oReg = Registry.LocalMachine.OpenSubKey("Software", True)
oReg = oReg.CreateSubKey("UTMagDemo")
oReg.SetValue(txtName.Text, txtValue.Text)
oReg.Close()

The code starts by opening the Software key of the HKEY_LOCAL_MACHINE registry hive. Notice that all the hives are available (LocalMachine and CurrentUser are probably the ones you will most often use). The second parameter means that you are opening this key and that you will later write in it.

Once the Software key is opened, the UTMagDemo sub key is created in it. If this key already exists, nothing happens. No exceptions are raised. The key content will remain untouched. Notice that you can create a complete hierarchy in a single call (ie oReg.CreateSubKey("UTMagDemo\\Level1\\Level2\\Level3") is correct). Don't ask me why all the backslashes are doubled. Even if you put only one, it will be working. Even a mix of single and double backslashes is working (according to my own tests). It may have something to do with C# since the backslash is an escape character in C# (but has no impact in VB.Net). If you find out why, drop me a line!

The next line adds a new value to the sub key just created. If you let the Name parameter empty (or you set it to Nothing), the value will be saved in the (Default) key name. If you provide a value for a name that exists, the value is simply replaced without warnings.

The last line is closing the key we opened.

Many exceptions may be triggered while trying to write to the registry. The most encountered are:

  • The user does not have permissions to create registry keys.
  • The key name cannot exceeds 255 characters
  • The key is closed.
  • The key is read-only.

Reading the value back

Many of the lines needed to read from the registry are the same as the ones we used to write:

Dim oReg As RegistryKey
Dim strValue As String
oReg = Registry.LocalMachine.OpenSubKey("Software\\UTMagDemo")
strValue = oReg.GetValue(txtName.Text, "DEFAULT-VALUE").ToString
MessageBox.Show(strValue, _
"Stored Registry Value", _
MessageBoxButtons.OK, _MessageBoxIcon.Information)
oReg.Close()

The code starts by opening the sub key we created in the previous sample. One thing you really need to check (and it is not done here – but it is done into the downloadable demo) is if the oReg is Nothing after the call of the OpenSubKey method. This indicates that the key you passed to the method does not exist. No exceptions are raised when this occurs.

The following line uses GetValue to retrieve the value with the Name parameter. Again, if you let the Name parameter empty (or you set it to Nothing), the value in the (Default) key name will be retrieved. If the Name does not exist, the default value (if you provide one) will be returned.

The last line is closing the key we opened.

any exceptions may be triggered while trying to write to the registry. The most encountered are:

  • The user does not have permissions to create registry keys.
  • The key name cannot exceeds 255 characters
  • The key is closed.
  • The key is read-only.

Wiping out our stuff

The Delete method has 3 flavors:

  • DeleteValue deletes a single value (using the Name parameter to specify which one to delete).
  • DeleteSubKey deletes a complete key and all the values it contains in a single shot (but raises an exception if it contains sub keys).
  • DeleteSubKeyTree deletes a complete key and all the values it contains as well as all sub keys in a single shot. The code I use for this demo is the following:
Dim oReg As RegistryKey
oReg = Registry.LocalMachine.OpenSubKey("Software", True)
oReg.DeleteSubKey("UTMagDemo")
oReg.Close()

Retrieving file type of a file extension

This other example shows how to retrieve the file type from a file extension.

Dim oReg As RegistryKey
Dim strIndexerKey As String
Dim strType As String
'Opens the key to retrieve the class name
oReg = Registry.ClassesRoot.OpenSubKey(txtFileExtension.Text)
If oReg Is Nothing Then
MessageBox.Show("The file extension " & _
txtFileExtension.Text & _
" is unknown.", _
"File Type", _
MessageBoxButtons.OK, _
MessageBoxIcon.Information)
Exit Sub
End If
strIndexerKey = oReg.GetValue("").ToString
'Opens the class name to retrieve the file type
oReg = Registry.ClassesRoot.OpenSubKey(strIndexerKey)
strType = oReg.GetValue("", "").ToString
MessageBox.Show("The file extension " & _
txtFileExtension.Text & _
" is a " & strType, _
"File Type", _
MessageBoxButtons.OK, _
MessageBoxIcon.Information)
'Close the registry keys.
oReg.Close()

Wednesday, November 22, 2006

XML Integration

What is XML?
What is the version information in XML?
What is ROOT element in XML?
If XML does not have closing tag will it work?
Is XML case sensitive?
What’s the difference between XML and HTML?
Is XML meant to replace HTML?
Can you explain why your project needed XML?
What is DTD (Document Type definition)?
What is well formed XML?
What is a valid XML?
What is CDATA section in XML?
What is CSS?
What is XSL?
What is Element and attributes in XML?
Can we define a column as XML?
How do we specify the XML data type as typed or untyped?
How can we create the XSD schema?
How do I insert in to a table which has XSD schema attached to it?
What is maximum size for XML datatype?
What is Xquery?
What are XML indexes?
What are secondary XML indexes?
What is FOR XML in SQL Server?
Can I use FOR XML to generate SCHEMA of a table and how?
What is the OPENXML statement in SQL Server?
I have huge XML file which we want to load in database?
How to call stored procedure using HTTP SOAP?
What is XMLA ?

Basic .NET and ASP.NET interview questions

  1. Explain the .NET architecture.
  2. How many languages .NET is supporting now? - When .NET was introduced it came with several languages. VB.NET, C#, COBOL and Perl, etc. The site DotNetLanguages.Net says 44 languages are supported.
  3. How is .NET able to support multiple languages? - a language should comply with the Common Language Runtime standard to become a .NET language. In .NET, code is compiled to Microsoft Intermediate Language (MSIL for short). This is called as Managed Code. This Managed code is run in .NET environment. So after compilation to this IL the language is not a barrier. A code can call or use a function written in another language.
  4. How ASP .NET different from ASP? - Scripting is separated from the HTML, Code is compiled as a DLL, these DLLs can be executed on the server.
  5. Resource Files: How to use the resource files, how to know which language to use?
  6. What is smart navigation? - The cursor position is maintained when the page gets refreshed due to the server side validation and the page gets refreshed.
  7. What is view state? - The web is stateless. But in ASP.NET, the state of a page is maintained in the in the page itself automatically. How? The values are encrypted and saved in hidden controls. this is done automatically by the ASP.NET. This can be switched off / on for a single control
  8. Explain the life cycle of an ASP .NET page.
  9. How do you validate the controls in an ASP .NET page? - Using special validation controls that are meant for this. We have Range Validator, Email Validator.
  10. Can the validation be done in the server side? Or this can be done only in the Client side? - Client side is done by default. Server side validation is also possible. We can switch off the client side and server side can be done.
  11. How to manage pagination in a page? - Using pagination option in DataGrid control. We have to set the number of records for a page, then it takes care of pagination by itself.
  12. What is ADO .NET and what is difference between ADO and ADO.NET? - ADO.NET is stateless mechanism. I can treat the ADO.Net as a separate in-memory database where in I can use relationships between the tables and select insert and updates to the database. I can update the actual database as a batch.

Tuesday, November 21, 2006

inviting-dotnet-people

Hai guys,I am Arunachalam. Working as developer in Gl infotech. I am doing my projects in ASP.net, C#, SQL Server2000 & MS Access. Am inviting people who want to share their ideas & their doubts in dotnet & SQL Server.Lets have passion about Dotnet.

Regards,

Arun