Bc0053 - Vb.net and XML de Set-1

7
Vinod H Bachelor of Computer Application Semester - 5 BC0053 – VB.Net & XML Fall 2012 Assignment Set – 1 Roll No: 521110833 Center Code: 03011

Transcript of Bc0053 - Vb.net and XML de Set-1

Vinod HBachelor of Computer Application

Semester - 5BC0053 – VB.Net & XML

Fall 2012 Assignment Set – 1Roll No: 521110833Center Code: 03011

2BC0053 – VB.Net & XML Set-1

1. Describe the process of compiling and running a visual basic application.

Figure 1 shows how an application is compiled and run when using Visual Basic .NET. To start, you use Visual Studio .NET to create a project, which is made of one or more source files that contain Visual Basic statements. Most simple projects consist of just one source file, but more complicated projects can have more than one source file. A project may also contain other types of files, such as sound files, image files, or simple text files. As the figure shows, a solution is a container for projects.

You use the visual basic compiler, which is built into visual studio, to compile your visual basic source code into Microsoft Intermediate Language (or MSIL). For short, this can be referred to as Intermediate Language (or IL).

At this point, the Intermediate Language is stored on disk in a file that’s called an assembly. In addition to the IL, the assembly includes references to the classes that the application requires. The assembly can then be run on any PC that has the Common Language Runtime installed on it. When the assembly is run, the CLR converts the Intermediate Language to native code that can be run by the Windows operation system.

CLR is available for UNIX system also, i.e. Mono. It is possible that the CLR will eventually be available for other operating systems as well. In other words, the Common Language Runtime makes platform independence possible visual Basic applications will be able to run on those operating systems as well as Windows/UNIX operating systems. Whether this will happen and how well it will work remains to be seen.

3BC0053 – VB.Net & XML Set-1

2. Explain various basic data types and variables in Visual.

There are two kinds of data types in VB.Net

a) Value type (implicit data types, structure and Enumeration)b) Reference type (objects, delegates)

Value types are passed to methods by passing an exact copy while reference types are passed to methods by passing only their reference (handle). Implicit data types are defined in the language core by the language vender, while explicit data types are types that are made by using or composing implicit data types.

Implicit data types in .net compliant languages are mapped to types in Common Type System (CTS) and CLS (Common Language Specification). Hence, each implicit data type in VB.Net has its corresponding .net type. The implicit data types in vb.net are:

VB.Net type Corresponding.Net Type

Size in bytes Description

Boolean Boolean 1 Contains either True or False

Char Char 2Contains any single Unicode character enclosed in double quotation marks followed by a c, for example “x” c

Integral TypesByte Byte 1 May contain integer from 0-255Short Int16 2 Ranges from -32,768 to 32, 768

Integer (default) Int32 4 Ranges from -2, 147, 483, 648 to 2, 147, 483, 647

Long Int64 8 Ranges from – 9, 223, 372, 036, 854, 775,808 to 9, 223, 372, 036, 854, 775, 807

Floating point types

Single Single 4 Ranges from ±1.5 x 10-45 to ±3.4 x 1038 with 7 digits precision. Requires the suffix ‘f’ or ‘F’

Double(default) Double 8 Ranges from ±1.5 x 10-324 to ±3.4 x 10308 with 15-16 digits precision.

Decimal Decimal 12 Ranges from 1.0 x 10-28 to 7.9 x 1028 with 28-29 digits precision. Requires the suffix ‘m’ or ‘M’

Implicit data types are represented in language using ‘keywords’; so each of above is a keyword in VB.Net (Keywords are the words defined by the language and cannot be used as identifies). It is worth-nothing that string is also an implicit data type in VB.Net, so String is a keyword in VB.Net. Last point about implicit data types is that they are value types and thus stored at the stack, while user defined types or referenced types are stored at heap. Stack is a data structure that store items in last in first out (LIFO) fashion. It is an area of memory supported by the processor and its size is determined at the compile time. Heap is the total memory available at run time. Reference types are allocated at heap dynamically (during the execution of program). Garbage collector searches for non-referenced data in heap during the execution of program and returns that space to Operating System.

4BC0053 – VB.Net & XML Set-1

3. With the help of suitable example, describe the development of single document and multi-document interface.

Figure 2 shows two versions of the financial calculations application that will be used as examples. These applications let the user calculate an investment or depreciation using forms. In addition, each application includes a third form that provides a way for the user to access the other forms.

The first version of this application uses a single-document interface, or SDI. In an SDI application, each form runs in its own application window, and this window is usually shown in the windows taskbar. Then, you can click on the buttons in the taskbar to switch between the open forms. When you use this interface, each form can have its own menus and toolbars. In addition, a main form called a startup form typically provides access to the other forms of the application. In this figure, for example, the startup form includes buttons that the user can click on to display the other forms.

The second version of this application uses a multiple-document interface, or MDI. In an MDI application, a container form called a parent form contains one or more child forms. Then, the menus and toolbars on the parent form contain the commands that let you open and view forms, when you close the parent form of an MDI application, all of the child forms are closed and the application ends.

The main advantage of a multiple-document interface is that the parent form manages multiple instances of child forms for you. In contrast, if you create multiple instances of a form in an SDI application, you have to manage them yourself. As you can imagine, that can get unwieldy.

The parent form in the MDI application in this figure also includes a status bar. A bar like this can be set up to display a number of information items. Although you can add a status bar to any form in either an SDI or MDI application, it makes the most sense to use one on the parent form in an MDI application.

Incidentally, you can also develop an explorer-style interface with Visual Basic .Net. in this type of interface, a single window is split into two panes just as it is in the windows explorer. Then, you can use the left pan to navigate between different parts of the application, and you can use the right pane to work with the application.

5BC0053 – VB.Net & XML Set-1

4. Describe the concept of Exceptions in .Net environment.

.Net implements a system wide, comprehensive approach to exception handling. Instead of an error number, there is an exception object. This object contains information relevant to the error, exposed as properties of the object. Later you’ll see a summary of the properties and the information they expose in a table.

Such an object is an instance of a class that derives from a class named System.Exception. As shown later, a variety of subclasses of System.Exception are used for different circumstances.

Important Properties and Methods of an exception:

The Exception class has properties that contain useful information about the exception, as shown in the following table:

Properties DescriptionHelpLink A string indicating the link to help for this exceptionInnerException Returns the exception object reference to an inner (nested) exceptionMessage A string that contains a description of the error, suitable for displaying to usersSource A string containing the name of an object that generated the errorStackTrace A read-only property that holds the stack trace as a text string. The stack trace is a

list of the pending method calls at the point at which the exception was detected. That is, if MethodA called MethodB, and an exception occurred in MethodB, the stack trace would contain both MethodA and MethodB

TragetSite A read-only string property that holds the method that threw the exception

The 2 most important methods of the Exception class are as follows:

Method DescriptionGetBaseException Returns the first exception in the chainToString Returns the error string, which might include as much information as the error

message, the inner exception, and the stack trace, depending on the error

6BC0053 – VB.Net & XML Set-1

Because an exception contains all of the information needed about an error, structured exception handling does not use error numbers and the Err object. The exception object contains all the relevant information about the error.

However, whereas there is only one global Err object in VB6, there are many types of exception objects in VB 2005. For example, if a divide by zero is done in code, then an Overflow Exception is generated. There are several dozen types of exception classes in VB 2005, and in addition to using the ones that are available in the .Net Framework, you can inherit from a class called ApplicationException and then create your own exception classes.

In .Net, all exceptions inherit from System.Exception. Special-purpose exception classes can be found in many namespaces. The following table lists four representative examples of the classes that extend Exception:

Namespace Class DescriptionSystem InvalidOperationException Generated when a call to an object method is

inappropriate because of the object’s stateSystem OutOfMemoryException Results when there is not enough memory to carry

out an operationSystem.XML XmlException Often caused by an attempt to read invalid XMLSystem.Data DataException Represents errors in ADO.NET components

It is common for an exception class to reside in a namespace with the classes that typically generate the exception. For example, the DataException class is in System.Data, with the ADO.NET components that often generate a DataException instance.

Having many types of exceptions in VB 2005 enables different types of conditions to be trapped with different exception handlers. This is a major advance over VB6.

5. Explain the concept of setting a connection string with example.

These are properties and methods associated with the connection object. We have to start with the ConnectionString property. This can take many parameters. Fortunately, we only need a few of these.

We need to pass two things to our new Connection Object: the technology we want to use to do the connecting to our database; and where the database is. (If your database was password and user name protected, you would add these two parameters as well. Ours isn’t, so we only need the two)

The technology is called the provider; and you use “Data Source” to specify where your database is. This should be entered on the same line, and not two as it is below. So add this to your code:

Con.ConnectionString = “PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\AddressBook.mdb”

Notice the two parts, separated by a semi-colon:

1st Part: PROVIDER=Microsoft.Jet.OLEDB.4.0

2nd Part: Data Source = C:\AddressBook.mdb

7BC0053 – VB.Net & XML Set-1

The first part specifies which provider technology we want to use to do the connecting (JET). The second part, typed after a semi-colon, points to where the database is. In the above code, the database is on the C drive, in the root folder. The name of the access file we want to connect to is called AddressBook.mdb. (Note that “Data Source” is two words, and not one.)

But your coding window should now look like this:

Private Sub btnLoad_Click(byVal Sender as Object,_

ByVal e as System.EventArgs) _

Handles btnLoad.Click

Dim con as new OleDb.OleDbConnection

Con.ConnectionString = “Provider = Microsoft.Jet.OLEDB.4.0; DataSource = C:\AddressBook.mdb”

This assumes that you have copied the addressbook database over to the root folder of your C drive. If you’ve copied it to another folder, change the “Data Source” part to match. For example, if you copied it you copied it to a folder called “databases” you’d put this:

Data Source = C:\databases\addressbook.mdb

In our code, though, ConnectionString is a property of the con variable. The con variable holds our connection object. We’re passing the Connection String the name of a data provider, and a path to the database.