Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
42 views17 pages

VB Dot Net

Uploaded by

aspirantssc892
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views17 pages

VB Dot Net

Uploaded by

aspirantssc892
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

VB.

NET
Experiments
1. Program to display the addition, subtraction, multiplication and division of two
number using console application.
Ans :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyExample1
{
class Program
{
static void Main(string[] args)
{
int a, b, sum, mult, sub;
float div;
Console.WriteLine("Enter Value of a and b\n");
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());

sum = a + b;
Console.WriteLine("\nThe Sum of {0} and {1} = {2}\n", a, b, sum);

mult = a * b;
Console.WriteLine("The Multiplication of {0} and {1} = {2}\n", a, b, mult);

div = (float)a / b;
Console.WriteLine("The Division of {0} and {1} = {2}\n", a, b, div);

sub = a - b;
Console.WriteLine("The Substraction of {0} and {1} = {2}\n", a, b, sub);

Console.ReadLine();
}
}
}
The output of console application is :
2. Program to display the first 10 natural numbers and their sum using console
application.
Ans :

Private Sub Command1_Click()


Dim i, s As Integer
Cls
For i = 1 To 10
s=s+i
Next i
Print "Sum of ten Serial Number = "; s
End Sub

Output :-
3. Program to display the addition using the windows application.
Ans :

Double click on command1 button and write code in code window.


Private Sub command1_click()
Dim a, b, x As Integer
a = Val(Text1.Text)
b = Val(Text2.Text)
x=a+b
Label3 = "Sum of Two Number = " & x
End Sub
Close the code window and run the form to display output.

4. Write a program to convert input string from lower to upper and upper to lower case.
Ans :

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button1.Click
TextBox2.Text = UCase(TextBox1.Text)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button2.Click
TextBox2.Text = LCase(TextBox1.Text)
End Sub

5. Write a program to simple calculator using windows application.


Ans :
Module Module1
Sub Main()
Dim choice As Integer

Dim num1 As Integer = 0


Dim num2 As Integer = 0
Dim result As Integer = 0

Console.WriteLine("############################")
Console.WriteLine(" 1: Addition")
Console.WriteLine(" 2: Subtraction")
Console.WriteLine(" 3: Multiplication")
Console.WriteLine(" 4: Division")
Console.WriteLine("############################")

Console.Write("Enter choice: ")


choice = Integer.Parse(Console.ReadLine())

Console.Write("Enter number1: ")


num1 = Integer.Parse(Console.ReadLine())

Console.Write("Enter number2: ")


num2 = Integer.Parse(Console.ReadLine())

Select Case choice


Case 1
result = num1 + num2
Case 2
result = num1 - num2
Case 3
result = num1 * num2
Case 3
result = num1 / num2
Case Else
Console.WriteLine("Invalid choice")
End Select

Console.WriteLine("Result: {0}", result)


End Sub

End Module
Output:
############################
1: Addition
2: Subtraction
3: Multiplication
4: Division
############################
Enter choice: 3
Enter number1: 3
Enter number2: 6
Result: 18
Press any key to continue . . .

6. Write a program working with Page using ASP.Net.


Ans :
Let us create a new web site with a web form, which will show the current date and time, when a
user clicks a button. Take the following steps −
 Select File → New → Web Site. The New Web Site Dialog Box appears.

 Select the ASP.Net Empty Web Site templates. Type a name for the web site and select a
location for saving the files.
 You need to add a Default page to the site. Right click the web site name in the Solution
Explorer and select Add New Item option from the context menu. The Add New Item
dialog box is displayed −
 Select Web Form option and provide a name for the default page. We have kept it as
Default.aspx. Click the Add button.
 The Default page is shown in Source view
 Set the title for the Default web page by adding a value to the <Title> tag of the page, in
the Source view −
 To add controls on the web page, go to the design view. Add three labels, a text box and a
button on the form.
 Double-click the button and add the following code to the Click event of the button −
Protected Sub Button1_Click(sender As Object, e As EventArgs) _
Handles Button1.Click
Label2.Visible = True
Label2.Text = "Welcome to Tutorials Point: " + TextBox1.Text
Label3.Text = "You visited us at: " + DateTime.Now.ToString()
End Sub
When the above code is executed and run using Start button available at the Microsoft Visual
Studio tool bar, the following page opens in the browser −

Enter your name and click on the Submit button −


7. Write a program working with forms using ASP.NET.
Ans :
Create an Input Form:-
<!DOCTYPE html>
<html>

<head>
<title>Customer Form</title>
</head>

<body>
<form method = "post" action = "">
<fieldset>
<legend>Add Student</legend>
<div>
<label for = "StudentName">Student Name: </label>
<input type = "text" name = "StudentName" value = "" />
</div>

<div>
<label for = "UniName">University Name:</label>
<input type = "text" name = "UniName" value = "" />
</div>

<div>
<label for = "Address">Res. Address:</label>
<input type = "text" name = "Address" value = "" />
</div>

<div>
<label> </label>
<input type = "submit" value = "Submit" class = "submit" />
</div>
</fieldset>
</form>

</body>
</html>
Now let’s run the application again and specify the following url
− http://localhost:46023/myform, then you will see the following output.
8. Write a program to connectivity with Oracle database
Ans :
First, we create a project:
1. Go to the File menu and select the Website.
2. Now select ASP.NET Empty Website.
3. Now we add a web form.
4. In this web form, we use one GridView and one button.
After doing all that we will now add the Oracle assemblies to our project as in the following:
1. Go to Solution Explorer.
2. Right-click and select Add Reference.
3. Click on Extensions.
4. Select the following assembly files and click on the OK button.
Now we find that a Bin folder is created automatically and this folder contains all the required
assembly files.

For more confirmation, we can check our Web.Config file. If we check our web.config then we
will find the following code.

If our Web.config file contains the preceding code then that means everything is fine until now.

Now we make a connection with an Oracle database and retrieve the data as in the following:
1. First, we need to add the namespace "using Oracle.DataAccess.Client;" on our aspx.cs
page.
2. Now we require a TNS entry. The TNS is the .Net configuration file that defines database
addresses for establishing a connection with an Oracle database.

The following is a sample of how to create a TNS:


"Data Source=(DESCRIPTION =" + "(ADDRESS = (PROTOCOL = TCP)(HOST = HO
ST_NAME)(PORT = 1521))" + "(CONNECT_DATA =" + "(SERVER = DEDICATED)
" + "(SERVICE_NAME = ORCL)));" + "User Id= User_ID;Password=******"
Before making a connection with the Oracle database using TNS entries we should have the
following information in the TNS.
1. HOST Provider: You can use the following command to get the Host Name:
SELECT SYS_CONTEXT ('USERENV', 'SERVER_HOST') FROM DUAL;
Example
2. Service Name: To determine your Service Name you can use the following procedure.
 Press Window + R.
 Now enter Services.msc and press Enter.
 Now search for your Oracle Service. At the last of the service, you can find your service
name.

Here my Oracle Service name is “ORCL”.


3. User Id and Password: Provide the User id and Password that you set during installation.

After providing all that information we can make our connection.

The following is the source code of the Aspx.cs file:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Oracle.DataAccess.Client;
using System.Data;

public partial class _Default: System.Web.UI.Page


{
string TNS = "Data Source=(DESCRIPTION =" +
"(ADDRESS = (PROTOCOL = TCP)(HOST = pankaj)(PORT = 1521))" +
"(CONNECT_DATA =" +
"(SERVER = DEDICATED)" +
"(SERVICE_NAME = ORCL)));" +
"User Id= hr;Password=hr";
OracleConnection Con;
protected void Page_Load(object sender, EventArgs e)
{

OracleConnection Con = new OracleConnection(TNS);


Con.Open();
Response.Write("Connected to Oracle " + Con.ServerVersion);
Con.Close();
Con.Dispose();

Response.Write(" Disconnected");
}
protected void Submit_Click(object sender, EventArgs e)
{
OracleConnection Con = new OracleConnection(TNS);
Con.Open();
DataTable tab = new DataTable();
OracleDataAdapter da = new OracleDataAdapter("select * from JOBS", Con);
da.Fill(tab);
GridView1.DataSource = tab;
GridView1.DataBind();
Con.Close();
Con.Dispose();
}
}
When we run this project we find the following message in our browser screen.

In other words, our project has been connected with the Oracle database successfully. Now if we
press the Submit Button then it will show all the information of the JOBS table into the
GridView, such as:
9. Write a program to access data source through ADO.NET. Galaxy A13
Ans :
Set up the sample database
Create the sample database by following these steps:
1. In Visual Studio, open the Server Explorer window.
2. Right-click on Data Connections and choose Create New SQL Server Database.
3. In the Server name text box, enter (localdb)\mssqllocaldb.
4. In the New database name text box, enter Sales, then choose OK.
The empty Sales database is created and added to the Data Connections node in Server
Explorer.
5. Right-click on the Sales data connection and select New Query.
A query editor window opens.
6. Copy the Sales Transact-SQL script to your clipboard.
7. Paste the T-SQL script into the query editor, and then choose the Execute button.
Create the forms and add controls
1. Create a C# or Visual Basic project using the Windows Forms App (.NET
Framework) template, and then name it SimpleDataApp.
Visual Studio creates the project and several files, including an empty Windows form that's
named Form1.
2. Add two Windows forms to your project so that it has three forms, and then give them the
following names:
 Navigation
 NewCustomer
 FillOrCancel
3. For each form, add the text boxes, buttons, and other controls that appear in the following
illustrations. For each control, set the properties that the tables describe.
4. Navigation form

Expand table
Controls for the Navigation form Properties
Button Name = btnGoToAdd
Button Name = btnGoToFillOrCancel
Button Name = btnExit
NewCustomer form

Expand table
Controls for the NewCustomer form Properties
TextBox Name = txtCustomerName
TextBox Name = txtCustomerID
Readonly = True
Button Name = btnCreateAccount
Controls for the NewCustomer form Properties
NumericUpdown DecimalPlaces = 0
Maximum = 5000
Name = numOrderAmount
DateTimePicker Format = Short
Name = dtpOrderDate
Button Name = btnPlaceOrder
Button Name = btnAddAnotherAccount
Button Name = btnAddFinish
FillOrCancel form

Expand table
Controls for the FillOrCancel form Properties
TextBox Name = txtOrderID
Button Name = btnFindByOrderID
DateTimePicker Format = Short

Name = dtpFillDate
DataGridView Name = dgvCustomerOrders

Readonly = True

RowHeadersVisible = False
Button Name = btnCancelOrder
Button Name = btnFillOrder
Button Name = btnFinishUpdates

10. Write a program to manage the session.


Ans :

Step 1: Open Visual Studio.


Step 2: Click on New Project > WEB > ASP.NET Empty Web Application.
Step 3: Click on Solution Explorer.
Step 4: Right-click on Add > New Item > Web Form. Specify the name of the web form. Here
we have added two forms WebForm1 and WebForm2.
Step 5: Add the code to WebForm1.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="Session.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="formSubmit" runat="server">
<div>
User Name:-<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<br />
<br />
Password:-<asp:TextBox ID="txtpwd" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnSubmit" runat="server" OnClick="Button1_Click" Text="Submit" />
</div>
</form>
</body>
</html>
The code behind it is:
protected void Button1_Click(object sender, EventArgs e)
{
//textbox value is stored in Session
Session["UserName"] = txtUserName.Text;
Session["Pwd"] = txtpwd.Text;
Response.Redirect("WebForm2.aspx");
}
Step 6: Add the code to WebForm2.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs"
Inherits="Session.WebForm2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="formSubmit" runat="server">
<div>
User Name:-<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<br />
<br />
Password:-<asp:TextBox ID="txtpwd" runat="server"></asp:TextBox>
<br />
</div>
</form>
</body>
</html>
The code behind it is:
protected void PageLoad(object sender, EventArgs e)
{
//Session value is assign on the text box
if (Session["UserName"] != null)
{
txtUserName.Text = Session["UserName"].ToString();
}
if (Session["Pwd"] != null)
{
txtpwd.Text = Session["Pwd"].ToString();
}
}
Step 7: Now set the session in Web.config file.
<system.web>
<sessionState mode="SQLServer"
sqlConnectionString="Server=DIVS\SQLEXPRESS;Integrated Security=true"></sessionState>
<compilation debug="true" targetFramework="4.0" />
</system.web>
Output:

You might also like