You are on page 1of 11

Bachelor of Computer Application (BCA) Semester 5 BC0053 VB.

Net & XML 4 Credits


Assignment Set 1 (40 Marks)
Ques1. Describe the following: i) Code Editor Window ii) Solution Explorer Ans. (i) Code Editor Window: The Code Editor Window, Shown in Figure create and edit visual Basic source code. After you have designed the user interface for your project by placing controls on the form, youll turn to the Code Editor to develop the Visual Basic statements that make the controls functional. The easiest way to call up the Code Editor is to double-click a control. Then, you can begin typing the Visual Basic statements that will be executed when the user performs the most common action on that control. If you double-click on a button, for example, you can enter the statements that will be executed when the user clicks on that button. The Code Editor works much like other text editors youve worked with. However, the Code Editor has a number of special features that simplify the task of editing Visual Basic code. For example, color is used to distinguish Visual Basic Keywords from variables, comments, and other language elements. And many types of coding errors are automatically highlighted as you type so you can correct them. Description The Code Editor window is where you create and edit the Visual Basic code that your application requires. The Code Editor works much like any other text editor you have used, so you shouldnt have much trouble learning how to use it. You can display the Code Editor by double-clicking the form or one of the controls in the Form Designer window. Or, you can click the view Code Button in the Solution Explorer. Once youve opened the Code Editor, you can return to the Form Designer by clicking the (Design) tab at the top of the Code Editor or the View Designer button in the solution Explorer (to the right of the View Code button). You can also move among these windows by pressing Ctrl+Tab or Shift+Ctrl+Tab. Its important to realize that the Form Designer and the Code Editor do not represent two different files, instead they provide you with two views of the same Visual Basic source file. The Form Designer gives you a visual representation of the form that is implemented by your Visual Basic code. The Code Editor lets you edit the code for the form. Ques2. Describe the operators and constants, symbols in Visual Basic.Net Ans. There are various types of operators in Visual Basic, and I'll go over them all here. Here are the Arithmetic operators (for example, the expression 5 + 4 yields a value of 9):

^ Exponentiation Multiplication / Division \ Integer division Mod Modulus

+ Addition -Subtraction

These are the Assignment operators (for example, temperature = 72 stores the value 72 in the variable temperature): = Assignment ^= Exponentiation followed by assignment *= Multiplication followed by assignment /= Division followed by assignment \= Integer division followed by assignment += Addition followed by assignment -= Subtraction followed by assignment &= Concatenation followed by assignment Here are the Comparison operators (these values yield true or false valuesfor example, 5 > 4 yields a value of True):

< (Less than)True if operand1 is less than operand2 <= (Less than or equal to)True if operand1 is less than or equal to operand2 > (Greater than)True if operand1 is greater than operand2 >= (Greater than or equal to)True if operand1 is greater than or equal to operand2 = (Equal to)True if operand1 equals operand2 < > (Not equal to)True if operand1 is not equal to operand2 IsTrue if two object references refer to the same object LikePerforms string pattern matching

These are the String Concatenation operators (for example, "Hi "& " there " yields the string "Hi there".): & String concatenation + String concatenation These are the Logical/Bitwise operators, where bitwise means working bit by bit with numerical values. These types of operators can work on logical values (for example, if blnValue1 is set to True and blnValue2 is set to False, then blnValue1 Or blnValue2 returns a value of True) or numbers for bitwise operations, which work on their operands bit by bit (for example, if intValue1 is set to 2 and intValue2 is set to 1, then intValue1 Or intValue2 yields 3):

And Performs an And operation (for logical operations: True if both operands are True, False otherwise; the same for bit-by-bit operations where you treat 0 as False and 1 as True). Not Reverses the logical value of its operand, from True to False and False to True, for bitwise operations, turns 0 into 1 and 1 into 0. Or Operator performs an Or operation (for logical operations: True if either operand is True, False otherwise; the same for bit-by-bit operations where you treat 0 as False and 1 as True). Xor Operator performs an exclusive-Or operation (for logical operations: True if either operand, but not both, is True, and False otherwise; the same for bit-by-bit operations where you treat 0 as False and 1 as True). AndAlso Operator A "short circuited" And operator; if the first operand is False, the second operand is not tested.

OrElse Operator A "short circuited" Or operator, if the first operand is True, the second is not tested. Constant or Symbols: Constants values once defined cannot be changed in the program. Constants are declared using Const keyword, like: Dim Const PI As Double = 3.142 Constants must be initialized as they are declared. DimVonst MARKS As Integer It is a notation convention to use capital letters while naming constants. Ques3. Write a program to print the day of the week taking the input of numbers 1 to 7 as input using Select Case statement. Ans. Sub Main() dim n as integer = System.console.readline() select case n case 1 Console.writeline("Sunday") case 2 Console.writeline("Monday") case 3: Console.writeline("Tuesday") case 4: console.writeline("Wednesday") case 5: console.writeline("Thursday") case 6: console.writeline("Friday") case 7: console.writeline("Saturday") case else Console.writeline("Week has only 7 days") end select end sub

Ques4. Design a form based applications using labels, text boxes, and buttons to perform basic arithmetic operations on integers. Ans.

Set The Properties:

Control Label1 Label2 Label3 Textbox1 Textbox2 Textbox3 Command1 Command2 Command3 Command4 Command5

Property Caption: First No. Caption: Second No. Caption: Result. Text: (empty) Text: (empty) Text: (empty) Caption: Add Name: Cmdadd Caption: SUB Name: Cmdsub Caption: MUL Name: Cmdmul Caption: DIV Name: Cmddiv Caption: EXIT Name: CmdEX

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub Cmdadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cmdadd.Click TextBox3.Text = Val(TextBox1.Text) + Val(TextBox2.Text) End Sub Private Sub Cmdsub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cmdsub.Click TextBox3.Text = Val(TextBox1.Text) - Val(TextBox2.Text) End Sub Private Sub Cmdmul_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cmdmul.Click TextBox3.Text = Val(TextBox1.Text) * Val(TextBox2.Text) End Sub Private Sub Cmddiv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cmddiv.Click TextBox3.Text = Val(TextBox1.Text) / Val(TextBox2.Text) End Sub Private Sub Cmdex_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cmdex.Click End End Sub End Class

Ques5. Describe the concept of Exceptions in .Net environment Ans. .NET implements a systemwide, comprehensive approach to exception handling. As noted in the chapter introduction, 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 youll 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: Property HelpLink InnerException Message Source StackTrace Description A string indicating the link to help for this exception Returns the exception object reference to an inner (nested) exception A string that contains a description of the error, suitable for displaying to users A string containing the name of an object that generated the error 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. A read-only string property that holds the method that threw the exception

TargetSite

The two most important methods of the Exception class are as follows: Method GetBaseException ToString Description Returns the first exception in the chain Returns the error string, which might include as much information as the error message, the inner exceptions, and the stack trace, depending on the error

Bachelor of Computer Application (BCA) Semester 5 BC0053 VB . Net & XML 4 Credits

Assignment Set 2 (40 Marks)

Ques1. Describe the process of adding and updating records with an example in VB .net ? Ans. In the last section, you learned how to move through the records in your DataSet, and how to display the records in Textboxes on your form. In this lesson, well see how to add new records, how to delete them and how to Update a records. Before we start the coding for these new buttons, its important to understand that the DataSet is disconnected from the database. What this means is that if youre adding a new record, youre not adding it to the database: youre adding it to the DataSet! Similarly, if youre updating or Deleting, you are doing it to the DataSet, and NOT to the database. After you have made all of your changes, you THEN commit these changes to the database. You do this by issuing a separate command. But well see how it all works. Youll need to add a few more buttons to your form - five of them. Change the Name properties of the new Buttons to the following: btnAddNew btnCommit btnUpdate btnDelete btnClear Change the Text properties of the buttons to "Add New Record ", "Commit Changes", "Update Record ", "Delete Record", and "Clear/Cancel". Your form might look something like this:

(i) Updating a Record To reference a particular column (item) in a row of the DataSet, the code is this: ds.Tables("AddressBook").Rows(2).Item(1) That will return whatever is at Item 1 on Row 2. As well as returning a value, you can also set a value. You do it like this: ds.Tables("AddressBook").Rows(2).Item(1) = "Jane" Now Item 1 Row 2 will contain the text "Jane". This wont, however, affect the database! The changes will just get made to the DataSet. To illustrate this, add the following code to your btnUpdate: ds.Tables("AddressBook").Rows(inc).Item(1) = txtFirstName.Text ds.Tables("AddressBook").Rows(inc).Item(2) = txtSurname.Text MsgBox("Data updated")

Run your program, and click the Next Record button to move to the first record. "John" should be displayed in your first textbox, and "Smith" in the second textbox. Click inside the textboxes and change "John" to "Joan" and "Smith" to "Smithy". (Without the quotes). Now click your Update Record button. Move to the next record by clicking your Next Record button, and then move back to the first record. You should see that the first record is now "Joan Smithy". Close down your program, then run it again. Click the Next Record button to move to the first record. It will still be "John Smith". The data you updated has been lost! So here, again, is why: "Changes are made to the DataSet, and NOT to the Database" Try it out. Run your program, and change one of the records. Click the Update button. Then close the program down, and load it up again. You should see your new changes displayed in the textboxes. (ii) Add a New Record Adding a new record is slightly more complex. First, you have to add a new Row to the DataSet, then commit the new Row to the Database. But the Add New Record button on our form is quite simple. The only thing it does is to switch off other buttons, and clear the textboxes, ready for a new entry. Heres the code for your Add New Record button: btnCommit.Enabled = True btnAddNew.Enabled = False btnUpdate.Enabled = False btnDelete.Enabled = False txtFirstName.Clear() txtSurname.Clear() So three buttons are switched off when the Add New Record button is clicked, and one is switched on. The button that gets switched on is the Commit Changes button. The Enabled property of btnCommit gets set to True. But, for this to work, you need to set it to False when the form loads. So return to your Form. Click btnCommit to select it. Then locate the Enabled Property in the Properties box. Set it to False. When the Form starts up, the button will be switched off. The Clear/Cancel button can be used to switch it back on again. So add this code to your btnClear: btnCommit.Enabled = False btnAddNew.Enabled = True btnUpdate.Enabled = True btnDelete.Enabled = True inc = 0 NavigateRecords() Were switching the Commit Changes button off, and the other three back on. The other two lines just make sure that we display the first record again, after the Cancel button is clicked. Otherwise the textboxes will all be blank. To add a new record to the database, well use the Commit Changes button. So double click your btnCommit to access its code. Add the following:

If inc <> -1 Then Dim cb As New OleDb.OleDbCommandBuilder(da) Dim dsNewRow As DataRow dsNewRow = ds.Tables("AddressBook").NewRow() dsNewRow.Item("FirstName") = txtFirstName.Text dsNewRow.Item("Surname") = txtSurname.Text

ds.Tables("AddressBook").Rows.Add(dsNewRow) da.Update(ds, "AddressBook") MsgBox("New Record added to the Database") btnCommit.Enabled = False btnAddNew.Enabled = True btnUpdate.Enabled = True btnDelete.Enabled = True End If The code is somewhat longer than usual, but well go through it. The first line is an If Statement. Were just checking that there is a valid record to add. If theres not, the inc variable will be on minus 1. Inside of the If Statement, we first set up a Command Builder, as before. The next line is this: Dim dsNewRow As DataRow If you want to add a new row to your DataSet, you need a DataRow object. This line just sets up a variable called dsNewRow. The type of variable is a DataRow. To create the new DataRow object, this line comes next: dsNewRow = ds.Tables("AddressBook").NewRow() Were just saying, "Create a New Row object in the AddressBook DataSet, and store this in the variable called dsNewRow." As you can see, NewRow() is a method of ds.Tables. Use this method to add rows to your DataSet. The actual values we want to store in the rows are coming from the textboxes. So we have these two lines: dsNewRow.Item("FirstName") = txtFirstName.Text dsNewRow.Item("Surname") = txtSurname.Text The dsNewRow object we created has a Property called Item. This is like the Item property you used earlier. It represents a column in your DataSet. We could have said this instead: dsNewRow.Item(1) = txtFirstName.Text dsNewRow.Item(2) = txtSurname.Text The Item property is now using the index number of the DataSet columns, rather than the names. The result is the same, though: to store new values in these properties. Were storing the text from the textboxes to our new Row. We now only need to call the Method that actually adds the Row to the DataSet: ds.Tables("AddressBook").Rows.Add(dsNewRow) To add the Row, you use the Add method of the Rows property of the DataSet. In between the round brackets, you need the name of your DataRow (the variable dsNewRow, in our case). You should know what the rest of the code does. Heres the next line: da.Update(ds, "AddressBook") Ques2. Write a simple XML file for student details (sname, sno, sbranch, saddr) ? Ans. college.xml <college> <student>

<student branch=CSE> <sname firstname=h>Rama</sname> <sno>1</sno> <saddr>Hyderabad</saddr> </student> <student> <student branch=ECE> <sname firstname=r>sita</sname> <sno>1</sno> <saddr>Delhi</saddr> </student> <student> <student branch=EEE> <sname firstname=p>Rani</sname> <sno>1</sno> <saddr>Puna</saddr> </student> </college> The root element in the example is <college>. All <student> elements in the document are contained within <college>. The <student> element has 4 children: <sname>, < sno>, <sbranch>, <saddr>. Ques3. What is meant by DTD? What are the building blocks of DTD ? Ans. DTD is one of the specifications included in to XML. DTD has its own small set of constructs and grammar rules that helps us to prepare our markup language specifications. That is the constructs of DTD are understood by the XML parsers that can help our markup language implementation application. DTD file contains the building blocks (i.e. tags) of an XML file. The building blocks of XML document are: 1. Elements (Tags) 2. Attributes 3. Entities 4. PCDATA 5. CDATA

Ques4. Explain XML Namespaces with an example. Ans. Namespace allows us to define the elements and attributes with a unique identification. This is similar to packages concept we have in java This allows defining the elements in different XSD documents and references them uniquely. Example1: <staff> <name>hai</name> <dept>

<name>hello</name> </dept> <room>35536</room> </staff> In this example we have two elements called name but they each represent different things and have different meanings. Applications confuse these two items, thus rendering the whole XML document useless. In a small document that is not a problem as you can simply invent a new name for one of the elements. For large organizations there are more number of XML Schemas, for this purpose we use the concept called namespaces. Example2: Name Conflicts In XML, element names are defined by the developer. This often results in a conflict when trying to mix XML documents from different XML applications. This XML carries information about a company (emp details): <company> <eno>1</eno> <ename>Hello</ename> </company> This XML carries information about a company (dept details): <company> <dno>12345</dno> <dname>computers</dname> </company> If these XML fragments were added together, there would be a name conflict. Both contain a <company> element, but the elements have different content and meaning. Solving the Name Conflict Using a Prefix Name conflicts in XML can easily be avoided using a name prefix. This XML carries information about an emp and dept: <e:company> <e:eno>1</e:eno> <e:ename>Hello</e:ename> </e:company> <d:company> <d:dno>12345</d:dno> <d:dname>computers</d:dname> </d:company> In the example above, there will be no conflict because the two <company> elements have different names. XML Namespaces - The xmlns Attribute When using prefixes in XML, a so-called namespace for the prefix must be defined. The namespace is defined by the xmlns attribute in the start tag of an element. Ques5. Write a program that uses the nodeValue property to change the text node of the first <sname> element in "college.xml.

Ans.

<html>

10

<head> <script type="text/javascript" src="loadxmldoc.js"> </script> </head> <body> <script type="text/javascript"> xmlDoc=loadXMLDoc("college.xml"); x=xmlDoc.getElementsByTagName("sname")[0]; document. write (xmlDoc.documentElement.nodeName); document. write ("<br />"); document.write(xmlDoc.documentElement.nodeType); </script> </body> </html> Output : College.xml 1

You might also like