+ All Categories
Home > Documents > Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008...

Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008...

Date post: 31-Aug-2018
Category:
Upload: dinhthu
View: 214 times
Download: 0 times
Share this document with a friend
37
VB.NET Class Assignments Lecture No.20 Assignment No. 1 Objective: Create a WPF Application and create Login Form to check username & password Commands Used: 1. TextBox 2. PasswordBox 3. Label Output:
Transcript
Page 1: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments

Lecture No.20 Assignment No. 1

Objective: Create a WPF Application and create Login Form to check username & password

Commands Used:

1. TextBox 2. PasswordBox 3. Label

Output:

Page 2: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments Program: Window1.xaml <Window x:Class="Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> <Grid> <Button Margin="124,0,83,88" Name="Button1" Height="21" VerticalAlignment="Bottom"> Login </Button> <TextBox Margin="106,83,52,0" Name="txtUsername" Height="23" VerticalAlignment="Top" /> <Label HorizontalAlignment="Left" Margin="25,83,0,0" Name="Label1" Width="75" Height="23" VerticalAlignment="Top" HorizontalContentAlignment="Right">Username</Label> <Label HorizontalAlignment="Left" HorizontalContentAlignment="Right" Margin="25,112,0,127" Name="Label2" Width="75">Password</Label> <PasswordBox Margin="106,112,52,127" Name="txtPassword" /> </Grid> </Window> Window1.xaml.vb Partial Public Class Window1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click If txtUsername.Text = "student" And txtPassword.Password = "student" Then MsgBox("Login Successful!") Else MsgBox("Login Failed") End If End Sub End Class

Page 3: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments

Lecture No.20 Assignment No. 2

Objective: Create a WPF form having buttons aligned as shown in the figure using Grid

Commands Used:

1. Grid

Output:

Program: GridExample.xaml <Window x:Class="GridExample" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="GridExample" Height="300" Width="300"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="51*" /> <RowDefinition Height="56*" /> <RowDefinition Height="51*" /> <RowDefinition Height="57*" /> <RowDefinition Height="47*" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="72*" />

Page 4: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments <ColumnDefinition Width="76*" /> <ColumnDefinition Width="66*" /> <ColumnDefinition Width="64*" /> </Grid.ColumnDefinitions> <Button Margin="-3,0,0,0" Name="Button1">Button</Button> <Button Name="Button2" Grid.Row="2">Button</Button> <Button Name="Button3" Grid.Column="2" Grid.Row="2">Button</Button> <Button Name="Button4" Grid.Column="3" Grid.Row="3">Button</Button> <Button Name="Button5" Grid.Column="1" Grid.Row="1">Button</Button> <Button Name="Button6" Grid.Column="2">Button</Button> <Button Margin="0,1,0,0" Name="Button7" Grid.Column="3" Grid.Row="1">Button</Button> <Button Name="Button8" Grid.Column="1" Grid.Row="3">Button</Button> <Button Margin="-2,0,0,0" Name="Button9" Grid.Row="4">Button</Button> <Button Name="Button10" Grid.Column="2" Grid.Row="4">Button</Button> </Grid> </Window>

Page 5: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments

Lecture No.20 Assignment No. 3

Objective: Create a WPF form having buttons aligned as shown in the figure using Grid

Commands Used:

1. Grid

Output:

Program: DrawingShapes.xaml <Window x:Class="DrawingShapes" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="DrawingShapes" Height="300" Width="300"> <Grid> <Canvas Background="CadetBlue"> <Ellipse Canvas.Left="86" Canvas.Top="41" Height="90" Name="ellipse1" Stroke="Black" Width="96"> <Ellipse.Fill> <RadialGradientBrush Center="0.5,0.4" RadiusX="0.5" RadiusY="0.5"> <GradientStop Color="Azure" Offset="0.1" /> <GradientStop Color="CornflowerBlue" Offset="1.1" /> </RadialGradientBrush> </Ellipse.Fill> </Ellipse>

Page 6: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments <Ellipse Canvas.Left="105" Canvas.Top="62" Height="13" Name="ellipse2" Stroke="Black" Width="14" Fill="Cornsilk" /> <Ellipse Canvas.Left="144" Canvas.Top="62" Height="13" Name="ellipse3" Stroke="Black" Width="14" Fill="Cornsilk" /> <Ellipse Canvas.Left="112" Canvas.Top="68" Height="5" Name="ellipse4" Stroke="Black" Width="5" Fill="Black" /> <Ellipse Canvas.Left="151" Canvas.Top="67" Height="5" Name="ellipse5" Stroke="Black" Width="5" Fill="Black" /> <Rectangle Canvas.Left="105" Canvas.Top="129" Height="111" Name="rectangle1" Stroke="Black" Width="59" Fill="HotPink" /> <Ellipse Canvas.Left="127" Canvas.Top="128" Height="10" Name="ellipse6" Stroke="Black" Width="9" Fill="Blue" /> <Ellipse Canvas.Left="127" Canvas.Top="148" Height="10" Name="ellipse7" Stroke="Black" Width="9" Fill="Blue" /> <Ellipse Canvas.Left="127" Canvas.Top="168" Height="10" Name="ellipse8" Stroke="Black" Width="9" Fill="Blue" /> <Ellipse Canvas.Left="127" Canvas.Top="188" Height="10" Name="ellipse9" Stroke="Black" Width="9" Fill="Blue" /> <Ellipse Canvas.Left="127" Canvas.Top="208" Height="10" Name="ellipse10" Stroke="Black" Width="9" Fill="Blue" /> <Ellipse Canvas.Left="127" Canvas.Top="228" Height="10" Name="ellipse11" Stroke="Black" Width="9" Fill="Blue" /> <Line X1="12" X2="400" Y1="145" Y2="115" Stroke="Black" StrokeThickness="5" Height="158" Canvas.Left="131" Canvas.Top="114" Width="42" /> <Path Name="path1" Stroke="Black" Data="M 60,113 Q 90,146 111,111" StrokeThickness="5" Height="134" Canvas.Left="48" Canvas.Top="-14" Width="145" /> <Line X1="124" X2="145" Y1="145" Y2="166" Stroke="Black" StrokeThickness="5" Height="168" Canvas.Left="40" Canvas.Top="-7" Width="147" /> <Line X1="145" X2="600" Y1="149" Y2="166" Stroke="Black" StrokeThickness="5" Height="168" Canvas.Left="37" Canvas.Top="8" Width="178" /> <Line X1="124" X2="170" Y1="145" Y2="16" Stroke="Black" StrokeThickness="5" Height="115" Canvas.Left="-42" Canvas.Top="65" Width="147" /> <Line X1="115" X2="145" Y1="145" Y2="166" Stroke="Black" StrokeThickness="5" Height="168" Canvas.Left="-50" Canvas.Top="11" Width="147" /> <Polygon Points="10,110 60,10 110,110" Fill="Blue" Height="55" Canvas.Left="72" Canvas.Top="-7" Width="132" /> <Line X1="11" X2="145" Y1="145" Y2="166" Stroke="Black" StrokeThickness="5" Height="163" Canvas.Left="83" Canvas.Top="110" Width="41" /> <Line X1="145" X2="145" Y1="145" Y2="166" Stroke="Black" StrokeThickness="5" Height="168" Canvas.Left="-23" Canvas.Top="94" Width="151" /> <Line X1="145" X2="145" Y1="145" Y2="166" Stroke="Black" StrokeThickness="5" Height="168" Canvas.Left="-1" Canvas.Top="94" Width="147" /> <Line X1="11" X2="10" Y1="145" Y2="115" Stroke="Black" StrokeThickness="5" Height="158" Canvas.Left="121" Canvas.Top="-52" Width="42" />

Page 7: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments <Polyline Name="Base" Points="10,110 6,10 110,110" Stroke="Black" StrokeThickness="4" Height="52" Canvas.Left="209" Canvas.Top="210" Width="50"/> <Ellipse Canvas.Left="172" Canvas.Top="118" Height="100" Name="Ball" Stroke="Black" Width="87"> <Ellipse.BitmapEffect> <OuterGlowBitmapEffect GlowColor="DarkViolet" /> </Ellipse.BitmapEffect> </Ellipse> </Canvas> </Grid> </Window>

Page 8: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments

Lecture No.21 Assignment No. 1

Objective: Create a WPF form having Combo Box bound with product table from database, on the click of the button display the selected value in the combo box

Commands Used:

1. Data Binding

Output:

Program: DatabindingExample.xaml <Window x:Class="DataBindingExample" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Data Binding Example" Height="300" Width="300" Loaded="Window_Loaded"> <Grid> <ComboBox Margin="20,109,29,126" Name="comboBox1" /> <Button Height="23" Margin="20,0,0,89" Name="button1" VerticalAlignment="Bottom" Click="button1_Click" HorizontalAlignment="Left" Width="75">Show</Button> <Label Height="20" Margin="20,83,138,0" Name="label1" VerticalAlignment="Top">Select Product</Label> </Grid>

Page 9: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments </Window> DatabindingExample.xaml.vb Imports System.Data Imports System.Data.SqlClient Partial Public Class DataBindingExample Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) 'Note: For DataBinding use the Typed DataSet i.e. DataSet xsd file Dim con As New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True;") Dim da As New SqlDataAdapter("select ProductID,ProductName from products", con) Dim objDS As New NWD() con.Open() da.Fill(objDS.Product) con.Close() comboBox1.ItemsSource = objDS.Product comboBox1.DisplayMemberPath = "ProductName" comboBox1.SelectedValuePath = "ProductID" End Sub Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) MsgBox(comboBox1.Text) End Sub End Class

Page 10: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments

Lecture No.21 Assignment No. 2

Objective: Create a WPF form to play video clip and add button for play & stop to it.

Commands Used:

1. Data Binding

Output:

Program: DatabindingExample.xaml <Window x:Class="DataBindingExample" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Data Binding Example" Height="300" Width="300" Loaded="Window_Loaded"> <Grid> <ComboBox Margin="20,109,29,126" Name="comboBox1" /> <Button Height="23" Margin="20,0,0,89" Name="button1" VerticalAlignment="Bottom" Click="button1_Click" HorizontalAlignment="Left" Width="75">Show</Button> <Label Height="20" Margin="20,83,138,0" Name="label1" VerticalAlignment="Top">Select Product</Label> </Grid> </Window>

Page 11: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments DatabindingExample.xaml.vb Imports System.Data Imports System.Data.SqlClient Partial Public Class DataBindingExample Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) 'Note: For DataBinding use the Typed DataSet i.e. DataSet xsd file Dim con As New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True;") Dim da As New SqlDataAdapter("select ProductID,ProductName from products", con) Dim objDS As New NWD() con.Open() da.Fill(objDS.Product) con.Close() comboBox1.ItemsSource = objDS.Product comboBox1.DisplayMemberPath = "ProductName" comboBox1.SelectedValuePath = "ProductID" End Sub Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) MsgBox(comboBox1.Text) End Sub End Class

Page 12: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments

Lecture No.22 Assignment No. 1

Objective: Following assignment is LINQ query examples on filtering, order by, group, projection, joins, quantifier, and aggregate operators

Commands Used:

• LINQ Operators

Output:

Program: Public Class LINQ_Examples #Region "Filtering" ''' <summary> ''' This returns all the numbers in an array that are less than 5. ''' </summary>

Page 13: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments ''' <remarks></remarks> Public Sub Linq1() 'Creates an array of numbers Dim numbers = New Integer() {5, 4, 1, 3, 9, 8, 6, 7, 2, 0} 'This query will give us all numbers in the array which are less than 5 'The variable num is called the "range variable" - it will take turns 'representing each value in the array. Dim lowNums = From num In numbers _ Where num < 5 _ Select num PrintOutput("Numbers < 5:") 'lowNums now contains only the numbers that are less than 5 'We can use a loop to print out each of the values For Each lowNumber In lowNums PrintOutput(lowNumber) Next End Sub ''' <summary> ''' This sample uses a Where clause to find all products that are out of stock. ''' </summary> ''' <remarks></remarks> Public Sub Linq2() Dim products = GetProductList() 'We can leave off the Select statement when we just want the range variable Dim soldOutProducts = From prod In products _ Where prod.UnitsInStock = 0 _ Select prod PrintOutput("Sold out products: ") For Each prod In soldOutProducts PrintOutput(prod.ProductName & " is sold out!") Next End Sub ''' <summary> ''' This sample uses a Where clause to find all products that are in stock and cost more than 3.00 per unit. ''' </summary> ''' <remarks></remarks> Public Sub Linq3() Dim products = GetProductList() Dim expensiveProducts = From prod In products _ Where prod.UnitsInStock > 0 AndAlso prod.UnitPrice > 3.0 PrintOutput("In-stock products that cost more than 3.00:")

Page 14: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments For Each prod In expensiveProducts PrintOutput(prod.ProductName & "is in stock and costs more than 3.00.") Next End Sub #End Region #Region "Order by" ''' <summary> ''' This sample uses Order By to sort a list of words alphabetically. ''' </summary> ''' <remarks></remarks> Public Sub Linq4() Dim words() As String = {"cherry", "apple", "blueberry"} Dim sortedWords = From word In words _ Order By word PrintOutput("The sorted list of words:") For Each w In sortedWords PrintOutput(w) Next End Sub ''' <summary> ''' This sample uses Order By to sort a list of words by length. ''' </summary> ''' <remarks></remarks> Public Sub Linq5() Dim words = New String() {"cherry", "apple", "blueberry"} Dim sortedWords = From word In words _ Order By word.Length PrintOutput("The sorted list of words (by length):") For Each w In sortedWords PrintOutput(w) Next End Sub ''' <summary> ''' This sample uses Order By and Descending to sort a list of doubles from highest to lowest. ''' </summary> ''' <remarks></remarks> Public Sub Linq6() Dim doubles = New Double() {1.7, 2.3, 1.9, 4.1, 2.9} Dim sortedDoubles = From dbl In doubles _ Order By dbl Descending PrintOutput("The doubles from highest to lowest:")

Page 15: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments For Each d In sortedDoubles PrintOutput(d) Next End Sub ''' <summary> ''' This sample uses Reverse to create a list of all digits in the array whose second letter is 'i' that is reversed from the order in the original array. ''' </summary> ''' <remarks></remarks> Public Sub Linq7() Dim digits() As String = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"} Dim IDigits = From dig In digits _ Where dig(1) = "i" Dim reversedIDigits = IDigits.Reverse() PrintOutput("A backwards list of the digits with a second character of 'i':") For Each d In reversedIDigits PrintOutput(d) Next End Sub #End Region #Region "Grouping" ''' <summary> ''' This sample uses Group By to partition a list of numbers by their remainder when divided by 5. ''' </summary> ''' <remarks></remarks> Public Sub Linq8() Dim numbers() As Integer = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0} Dim numberGroups = From num In numbers _ Group num By Remainder = num Mod 5 Into NumberGroup = Group _ Select Remainder, NumberGroup For Each g In numberGroups PrintOutput("Numbers with a remainder of " & g.Remainder & "when divided by 5:") For Each n In g.NumberGroup PrintOutput(n) Next Next End Sub ''' <summary>

Page 16: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments ''' This sample uses Group By to partition a list of words by their first letter. ''' </summary> ''' <remarks></remarks> Public Sub Linq9() Dim words = New String() {"blueberry", "chimpanzee", "abacus", "banana", "apple", "cheese"} Dim wordGroups = From word In words _ Group word By Key = word(0) Into Group _ Select FirstLetter = Key, WordGroup = Group For Each g In wordGroups PrintOutput("Words that start with the letter '" & g.FirstLetter & "':") For Each w In g.WordGroup PrintOutput(w) Next Next End Sub #End Region #Region "Partion" ''' <summary> ''' This sample uses Take to get only the first 3 elements of the array. ''' </summary> ''' <remarks></remarks> Public Sub Linq10() Dim numbers() As Integer = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0} Dim first3Numbers = From num In numbers Take 3 PrintOutput("First 3 numbers:") For Each n In first3Numbers PrintOutput(n) Next End Sub ''' <summary> ''' This sample uses Skip to get all but the first 4 elements of the array. ''' </summary> ''' <remarks></remarks> Public Sub Linq11() Dim numbers() As Integer = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0} Dim allButFirst4Numbers = From num In numbers Skip 4 PrintOutput("All but first 4 numbers:") For Each n In allButFirst4Numbers PrintOutput(n) Next

Page 17: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments End Sub ''' <summary> ''' This sample uses Take While to return elements starting from the beginning of the array until a number is hit that is not less than 6. ''' </summary> ''' <remarks></remarks> Public Sub Linq12() Dim numbers() As Integer = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0} Dim firstNumbersLessThan6 = From num In numbers Take While num < 6 'Alternate syntax 'Dim firstNumbersLessThan6 = numbers.TakeWhile(Function(n) n < 6) PrintOutput("First numbers less than 6:") For Each n In firstNumbersLessThan6 PrintOutput(n) Next End Sub #End Region #Region "Projection" ''' <summary> ''' This sample uses Select to produce a sequence of Integers one higher than those in an existing array of Integers. ''' </summary> ''' <remarks></remarks> Public Sub Linq13() Dim numbers = New Integer() {5, 4, 1, 3, 9, 8, 6, 7, 2, 0} Dim numsPlusOne = From num In numbers _ Select num + 1 PrintOutput("Numbers + 1:") For Each i In numsPlusOne PrintOutput(i) Next End Sub ''' <summary> ''' This sample uses Select to produce a sequence of strings representing the text version of a sequence of Integers. ''' </summary> ''' <remarks></remarks> Public Sub Linq14() Dim numbers = New Integer() {5, 4, 1, 3, 9, 8, 6, 7, 2, 0} Dim stringNames = New String() {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"} Dim textNums = From num In numbers _

Page 18: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments Select stringNames(num) PrintOutput("Number strings:") For Each s In textNums PrintOutput(s) Next End Sub ''' <summary> ''' This sample combines Select and Where to make a simple query that returns the text form of each digit less than 5. ''' </summary> ''' <remarks></remarks> Public Sub Linq15() Dim numbers As Integer() = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0} Dim digits As String() = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"} Dim lowNums = From num In numbers _ Where num < 5 _ Select digits(num) PrintOutput("Numbers < 5:") For Each num In lowNums PrintOutput(num) Next End Sub ''' <summary> ''' This sample uses a compound From clause to make a query that returns all pairs of numbers from both arrays such that the number from numbersA is less than the number from numbersB. ''' </summary> ''' <remarks></remarks> Public Sub Linq16() Dim numbersA() As Integer = {0, 2, 4, 5, 6, 8, 9} Dim numbersB() As Integer = {1, 3, 5, 7, 8} Dim pairs = From a In numbersA, b In numbersB _ Where a < b _ Select a, b PrintOutput("Pairs where a < b:") For Each pair In pairs PrintOutput(pair.a & " is less than " & pair.b) Next End Sub #End Region #Region "Aggregate Operators" ''' <summary> ''' This sample uses Count to get the number of unique factors of 300. ''' </summary>

Page 19: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments ''' <remarks></remarks> Public Sub Linq17() Dim factorsOf300 = New Integer() {2, 2, 3, 5, 5} Dim uniqueFactors = factorsOf300.Distinct().Count() PrintOutput("There are " & uniqueFactors & " unique factors of 300.") End Sub ''' <summary> ''' This sample uses Count to get the number of odd ints in the array. ''' </summary> ''' <remarks></remarks> Public Sub Linq18() Dim numbers() As Integer = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0} Dim oddNumbers = Aggregate num In numbers _ Into Count(num Mod 2 = 1) PrintOutput("There are " & oddNumbers & " odd numbers in the list.") End Sub ''' <summary> ''' This sample uses Sum to get the total of the numbers in an array. ''' </summary> ''' <remarks></remarks> Public Sub Linq19() Dim numbers() As Integer = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0} Dim numSum = numbers.Sum() PrintOutput("The sum of the numbers is " & numSum) End Sub ''' <summary> ''' This sample uses Sum to get the total number of characters of all words in the array. ''' </summary> ''' <remarks></remarks> Public Sub Linq20() Dim words = New String() {"cherry", "apple", "blueberry"} Dim totalChars = Aggregate word In words _ Into Sum(word.Length) PrintOutput("There are a total of " & totalChars & " characters in these words.") End Sub ''' <summary> ''' This sample uses Min to get the lowest number in an array. ''' </summary> ''' <remarks></remarks> Public Sub Linq21() Dim numbers() As Integer = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}

Page 20: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments Dim minNum = numbers.Min() PrintOutput("The minimum number is " & minNum & ".") End Sub ''' <summary> ''' This sample uses Min to get the length of the shortest word in an array. ''' </summary> ''' <remarks></remarks> Public Sub Linq22() Dim words = New String() {"cherry", "apple", "blueberry"} Dim shortestWord = Aggregate word In words _ Into Min(word.Length) PrintOutput("The shortest word is " & shortestWord & " characters long.") End Sub ''' <summary> ''' This sample uses Max to get the highest number in an array. ''' </summary> ''' <remarks></remarks> Public Sub Linq23() Dim numbers() As Integer = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0} Dim maxNum = numbers.Max() PrintOutput("The maximum number is " & maxNum & ".") End Sub ''' <summary> ''' This sample uses Max to get the length of the longest word in an array. ''' </summary> ''' <remarks></remarks> Public Sub Linq24() Dim words = New String() {"cherry", "apple", "blueberry"} Dim longestLength = Aggregate word In words _ Into Max(word.Length) PrintOutput("The longest word is " & longestLength & " characters long.") End Sub ''' <summary> ''' This sample uses Average to get the average of all numbers in an array. ''' </summary> ''' <remarks></remarks> Public Sub Linq25() Dim numbers() As Integer = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0} Dim averageNum = numbers.Average()

Page 21: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments PrintOutput("The average number is " & averageNum & ".") End Sub ''' <summary> ''' This sample uses Average to get the average length of the words in the array. ''' </summary> ''' <remarks></remarks> Public Sub Linq26() Dim words() As String = {"cherry", "apple", "blueberry"} Dim averageLength = words.Average(Function(w) w.Length) PrintOutput("The average word length is " & averageLength & " characters.") End Sub ''' <summary> ''' This sample uses Aggregate to create a running product on the array that calculates the total product of all elements. ''' </summary> ''' <remarks></remarks> Public Sub Linq27() Dim doubles = New Double() {1.7, 2.3, 1.9, 4.1, 2.9} Dim product = doubles.Aggregate(Function(runningProduct, nextFactor) runningProduct * nextFactor) PrintOutput("Total product of all numbers: " & product) End Sub #End Region #Region "Quantifier" ''' <summary> ''' This sample uses Any to determine if any of the words in the array contain the substring 'ei'. ''' </summary> ''' <remarks></remarks> Public Sub Linq28() Dim words() As String = {"believe", "relief", "receipt", "field"} Dim iAfterE = Aggregate word In words _ Into Any(word.Contains("ei")) 'Alternative Syntax 'Dim iAfterE = words.Any(Function(w) w.Contains("ei")) PrintOutput("There is a word that contains in the list that contains 'ei': " & iAfterE) End Sub ''' <summary>

Page 22: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments ''' This sample uses All to determine whether an array contains only odd numbers. ''' </summary> ''' <remarks></remarks> ''' Public Sub Linq29() Dim numbers() As Integer = {1, 11, 3, 19, 41, 65, 19} Dim onlyOdd = Aggregate num In numbers _ Into All(num Mod 2 = 1) PrintOutput("The list contains only odd numbers: " & onlyOdd) End Sub #End Region #Region "Joins" ''' <summary> ''' Cross Join: This sample shows how to efficiently join elements of two sequences based on equality between key expressions over the two. ''' </summary> ''' <remarks></remarks> Public Sub Linq30() Dim categories() As String = {"Beverages", "Condiments", "Vegetables", "Dairy Products", "Seafood"} Dim products = GetProductList() Dim categorizedProducts = From cat In categories _ Join prod In products On cat Equals prod.Category _ Select Category = cat, prod.ProductName For Each v In categorizedProducts PrintOutput(v.ProductName & ": " & v.Category) Next End Sub ''' <summary> ''' Using a group join you can get all the products that match a given category bundled as a sequence. ''' </summary> ''' <remarks></remarks> Public Sub Linq31() Dim categories As String() = {"Beverages", "Condiments", "Vegetables", "Dairy Products", "Seafood"} Dim productList = GetProductList() Dim categorizedProducts = From cat In categories _ Group Join prod In productList On cat Equals prod.Category _ Into Products = Group _

Page 23: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments Select Category = cat, Products For Each v In categorizedProducts PrintOutput(v.Category & ":") For Each p In v.Products PrintOutput(" " & p.ProductName) Next Next End Sub ''' <summary> ''' A so-called outer join can be expressed with a group join. A left outer join is like a cross join, except that all the left hand side elements get included at least once, even if they don't match any right hand side elements. Note how Vegetables shows up in the output even though it has no matching products. ''' </summary> ''' <remarks></remarks> Public Sub Linq32() Dim categories() As String = {"Beverages", "Condiments", "Vegetables", "Dairy Products", "Seafood"} Dim productList = GetProductList() Dim joinResults = From cat In categories _ Group Join prod In productList On cat Equals prod.Category Into Group _ From prod2 In Group.DefaultIfEmpty() _ Select Category = cat, _ ProductName = If(prod2 Is Nothing, "(None)", prod2.ProductName) For Each v In joinResults PrintOutput(v.Category & ": " & v.ProductName) Next End Sub #End Region #Region "Internal Classes" ''' <summary> ''' This contains the classes used in the LINQ Example ''' </summary> ''' <remarks></remarks> ''' Public Class Product Public ProductID As Integer Public ProductName As String Public Category As String Public UnitPrice As Decimal

Page 24: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments Public UnitsInStock As Integer End Class Private productList As List(Of Product) ''' <summary> ''' Initializes the Product objects ''' </summary> ''' <returns></returns> ''' <remarks></remarks> Public Function GetProductList() As List(Of Product) If productList Is Nothing Then CreateLists() End If Return productList End Function Private Sub CreateLists() ' Product data created in-memory. ' We could also use the MakeList method above to do this all in one line productList = New List(Of Product) productList.Add(New Product With {.ProductID = 1, .ProductName = "Chai", .Category = "Beverages", .UnitPrice = 18D, .UnitsInStock = 39}) productList.Add(New Product With {.ProductID = 2, .ProductName = "Chang", .Category = "Beverages", .UnitPrice = 19D, .UnitsInStock = 17}) productList.Add(New Product With {.ProductID = 3, .ProductName = "Aniseed Syrup", .Category = "Condiments", .UnitPrice = 10D, .UnitsInStock = 13}) productList.Add(New Product With {.ProductID = 4, .ProductName = "Chef Anton's Cajun Seasoning", .Category = "Condiments", .UnitPrice = 22D, .UnitsInStock = 53}) productList.Add(New Product With {.ProductID = 5, .ProductName = "Chef Anton's Gumbo Mix", .Category = "Condiments", .UnitPrice = 21.35D, .UnitsInStock = 0}) productList.Add(New Product With {.ProductID = 6, .ProductName = "Grandma's Boysenberry Spread", .Category = "Condiments", .UnitPrice = 25D, .UnitsInStock = 120}) productList.Add(New Product With {.ProductID = 7, .ProductName = "Uncle Bob's Organic Dried Pears", .Category = "Produce", .UnitPrice = 30D, .UnitsInStock = 15}) productList.Add(New Product With {.ProductID = 8, .ProductName = "Northwoods Cranberry Sauce", .Category = "Condiments", .UnitPrice = 40D, .UnitsInStock = 6}) productList.Add(New Product With {.ProductID = 9, .ProductName = "Mishi Kobe Niku", .Category = "Meat/Poultry", .UnitPrice = 97D, .UnitsInStock = 29}) productList.Add(New Product With {.ProductID = 10, .ProductName = "Ikura", .Category = "Seafood", .UnitPrice = 31D, .UnitsInStock = 31}) productList.Add(New Product With {.ProductID = 11, .ProductName = "Queso Cabrales", .Category = "Dairy Products", .UnitPrice = 21D, .UnitsInStock = 22})

Page 25: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments productList.Add(New Product With {.ProductID = 12, .ProductName = "Queso Manchego La Pastora", .Category = "Dairy Products", .UnitPrice = 38D, .UnitsInStock = 86}) productList.Add(New Product With {.ProductID = 13, .ProductName = "Konbu", .Category = "Seafood", .UnitPrice = 6D, .UnitsInStock = 24}) productList.Add(New Product With {.ProductID = 14, .ProductName = "Tofu", .Category = "Produce", .UnitPrice = 23.25D, .UnitsInStock = 35}) productList.Add(New Product With {.ProductID = 15, .ProductName = "Genen Shouyu", .Category = "Condiments", .UnitPrice = 15.5D, .UnitsInStock = 39}) productList.Add(New Product With {.ProductID = 16, .ProductName = "Pavlova", .Category = "Confections", .UnitPrice = 17.45D, .UnitsInStock = 29}) productList.Add(New Product With {.ProductID = 17, .ProductName = "Alice Mutton", .Category = "Meat/Poultry", .UnitPrice = 39D, .UnitsInStock = 0}) productList.Add(New Product With {.ProductID = 18, .ProductName = "Carnarvon Tigers", .Category = "Seafood", .UnitPrice = 62.5D, .UnitsInStock = 42}) productList.Add(New Product With {.ProductID = 19, .ProductName = "Teatime Chocolate Biscuits", .Category = "Confections", .UnitPrice = 9.2D, .UnitsInStock = 25}) productList.Add(New Product With {.ProductID = 20, .ProductName = "Sir Rodney's Marmalade", .Category = "Confections", .UnitPrice = 81D, .UnitsInStock = 40}) productList.Add(New Product With {.ProductID = 21, .ProductName = "Sir Rodney's Scones", .Category = "Confections", .UnitPrice = 10D, .UnitsInStock = 3}) productList.Add(New Product With {.ProductID = 22, .ProductName =

"Gustaf's Knckebr d", .Category = "Grains/Cereals", .UnitPrice = 21D,

.UnitsInStock = 104}) productList.Add(New Product With {.ProductID = 23, .ProductName =

"Tunnbr d", .Category = "Grains/Cereals", .UnitPrice = 9D, .UnitsInStock =

61}) productList.Add(New Product With {.ProductID = 24, .ProductName =

"Guaran Fant stica", .Category = "Beverages", .UnitPrice = 4.5D,

.UnitsInStock = 20}) productList.Add(New Product With {.ProductID = 25, .ProductName =

"NuNuCa Nu -Nougat-Creme", .Category = "Confections", .UnitPrice = 14D,

.UnitsInStock = 76}) productList.Add(New Product With {.ProductID = 26, .ProductName =

"Gumb r Gummib rchen", .Category = "Confections", .UnitPrice = 31.23D,

.UnitsInStock = 15}) productList.Add(New Product With {.ProductID = 27, .ProductName = "Schoggi Schokolade", .Category = "Confections", .UnitPrice = 43.9D, .UnitsInStock = 49}) productList.Add(New Product With {.ProductID = 28, .ProductName =

"R ssle Sauerkraut", .Category = "Produce", .UnitPrice = 45.6D, .UnitsInStock

= 26})

Page 26: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments productList.Add(New Product With {.ProductID = 29, .ProductName =

"Th ringer Rostbratwurst", .Category = "Meat/Poultry", .UnitPrice = 123.79D,

.UnitsInStock = 0}) productList.Add(New Product With {.ProductID = 30, .ProductName = "Nord-Ost Matjeshering", .Category = "Seafood", .UnitPrice = 25.89D, .UnitsInStock = 10}) productList.Add(New Product With {.ProductID = 31, .ProductName = "Gorgonzola Telino", .Category = "Dairy Products", .UnitPrice = 12.5D, .UnitsInStock = 0}) productList.Add(New Product With {.ProductID = 32, .ProductName = "Mascarpone Fabioli", .Category = "Dairy Products", .UnitPrice = 32D, .UnitsInStock = 9}) productList.Add(New Product With {.ProductID = 33, .ProductName = "Geitost", .Category = "Dairy Products", .UnitPrice = 2.5D, .UnitsInStock = 112}) productList.Add(New Product With {.ProductID = 34, .ProductName = "Sasquatch Ale", .Category = "Beverages", .UnitPrice = 14D, .UnitsInStock = 111}) productList.Add(New Product With {.ProductID = 35, .ProductName = "Steeleye Stout", .Category = "Beverages", .UnitPrice = 18D, .UnitsInStock = 20}) productList.Add(New Product With {.ProductID = 36, .ProductName = "Inlagd Sill", .Category = "Seafood", .UnitPrice = 19D, .UnitsInStock = 112}) productList.Add(New Product With {.ProductID = 37, .ProductName = "Gravad lax", .Category = "Seafood", .UnitPrice = 26D, .UnitsInStock = 11}) productList.Add(New Product With {.ProductID = 38, .ProductName =

"C te de Blaye", .Category = "Beverages", .UnitPrice = 263.5D, .UnitsInStock

= 17}) productList.Add(New Product With {.ProductID = 39, .ProductName = "Chartreuse verte", .Category = "Beverages", .UnitPrice = 18D, .UnitsInStock = 69}) productList.Add(New Product With {.ProductID = 40, .ProductName = "Boston Crab Meat", .Category = "Seafood", .UnitPrice = 18.4D, .UnitsInStock = 123}) productList.Add(New Product With {.ProductID = 41, .ProductName = "Jack's New England Clam Chowder", .Category = "Seafood", .UnitPrice = 9.65D, .UnitsInStock = 85}) productList.Add(New Product With {.ProductID = 42, .ProductName = "Singaporean Hokkien Fried Mee", .Category = "Grains/Cereals", .UnitPrice = 14D, .UnitsInStock = 26}) productList.Add(New Product With {.ProductID = 43, .ProductName = "Ipoh Coffee", .Category = "Beverages", .UnitPrice = 46D, .UnitsInStock = 17}) productList.Add(New Product With {.ProductID = 44, .ProductName = "Gula Malacca", .Category = "Condiments", .UnitPrice = 19.45D, .UnitsInStock = 27}) productList.Add(New Product With {.ProductID = 45, .ProductName = "Rogede sild", .Category = "Seafood", .UnitPrice = 9.5D, .UnitsInStock = 5}) productList.Add(New Product With {.ProductID = 46, .ProductName = "Spegesild", .Category = "Seafood", .UnitPrice = 12D, .UnitsInStock = 95}) productList.Add(New Product With {.ProductID = 47, .ProductName = "Zaanse koeken", .Category = "Confections", .UnitPrice = 9.5D, .UnitsInStock = 36})

Page 27: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments productList.Add(New Product With {.ProductID = 48, .ProductName = "Chocolade", .Category = "Confections", .UnitPrice = 12.75D, .UnitsInStock = 15}) productList.Add(New Product With {.ProductID = 49, .ProductName = "Maxilaku", .Category = "Confections", .UnitPrice = 20D, .UnitsInStock = 10}) productList.Add(New Product With {.ProductID = 50, .ProductName = "Valkoinen suklaa", .Category = "Confections", .UnitPrice = 16.25D, .UnitsInStock = 65}) productList.Add(New Product With {.ProductID = 51, .ProductName = "Manjimup Dried Apples", .Category = "Produce", .UnitPrice = 53D, .UnitsInStock = 20}) productList.Add(New Product With {.ProductID = 52, .ProductName = "Filo Mix", .Category = "Grains/Cereals", .UnitPrice = 7D, .UnitsInStock = 38}) productList.Add(New Product With {.ProductID = 53, .ProductName = "Perth Pasties", .Category = "Meat/Poultry", .UnitPrice = 32.8D, .UnitsInStock = 0}) productList.Add(New Product With {.ProductID = 54, .ProductName =

"Tourti re", .Category = "Meat/Poultry", .UnitPrice = 7.45D, .UnitsInStock =

21}) productList.Add(New Product With {.ProductID = 55, .ProductName =

"P t chinois", .Category = "Meat/Poultry", .UnitPrice = 24D, .UnitsInStock

= 115}) productList.Add(New Product With {.ProductID = 56, .ProductName = "Gnocchi di nonna Alice", .Category = "Grains/Cereals", .UnitPrice = 38D, .UnitsInStock = 21}) productList.Add(New Product With {.ProductID = 57, .ProductName = "Ravioli Angelo", .Category = "Grains/Cereals", .UnitPrice = 19.5D, .UnitsInStock = 36}) productList.Add(New Product With {.ProductID = 58, .ProductName = "Escargots de Bourgogne", .Category = "Seafood", .UnitPrice = 13.25D, .UnitsInStock = 62}) productList.Add(New Product With {.ProductID = 59, .ProductName = "Raclette Courdavault", .Category = "Dairy Products", .UnitPrice = 55D, .UnitsInStock = 79}) productList.Add(New Product With {.ProductID = 60, .ProductName = "Camembert Pierrot", .Category = "Dairy Products", .UnitPrice = 34D, .UnitsInStock = 19}) productList.Add(New Product With {.ProductID = 61, .ProductName =

"Sirop d' rable", .Category = "Condiments", .UnitPrice = 28.5D, .UnitsInStock

= 113}) productList.Add(New Product With {.ProductID = 62, .ProductName = "Tarte au sucre", .Category = "Confections", .UnitPrice = 49.3D, .UnitsInStock = 17}) productList.Add(New Product With {.ProductID = 63, .ProductName = "Vegie-spread", .Category = "Condiments", .UnitPrice = 43.9D, .UnitsInStock = 24}) productList.Add(New Product With {.ProductID = 64, .ProductName =

"Wimmers gute Semmelkn del", .Category = "Grains/Cereals", .UnitPrice =

33.25D, .UnitsInStock = 22})

Page 28: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments productList.Add(New Product With {.ProductID = 65, .ProductName = "Louisiana Fiery Hot Pepper Sauce", .Category = "Condiments", .UnitPrice = 21.05D, .UnitsInStock = 76}) productList.Add(New Product With {.ProductID = 66, .ProductName = "Louisiana Hot Spiced Okra", .Category = "Condiments", .UnitPrice = 17D, .UnitsInStock = 4}) productList.Add(New Product With {.ProductID = 67, .ProductName = "Laughing Lumberjack Lager", .Category = "Beverages", .UnitPrice = 14D, .UnitsInStock = 52}) productList.Add(New Product With {.ProductID = 68, .ProductName = "Scottish Longbreads", .Category = "Confections", .UnitPrice = 12.5D, .UnitsInStock = 6}) productList.Add(New Product With {.ProductID = 69, .ProductName = "Gudbrandsdalsost", .Category = "Dairy Products", .UnitPrice = 36D, .UnitsInStock = 26}) productList.Add(New Product With {.ProductID = 70, .ProductName = "Outback Lager", .Category = "Beverages", .UnitPrice = 15D, .UnitsInStock = 15}) productList.Add(New Product With {.ProductID = 71, .ProductName = "Flotemysost", .Category = "Dairy Products", .UnitPrice = 21.5D, .UnitsInStock = 26}) productList.Add(New Product With {.ProductID = 72, .ProductName = "Mozzarella di Giovanni", .Category = "Dairy Products", .UnitPrice = 34.8D, .UnitsInStock = 14}) productList.Add(New Product With {.ProductID = 73, .ProductName =

"R d Kaviar", .Category = "Seafood", .UnitPrice = 15D, .UnitsInStock = 101})

productList.Add(New Product With {.ProductID = 74, .ProductName = "Longlife Tofu", .Category = "Produce", .UnitPrice = 10D, .UnitsInStock = 4}) productList.Add(New Product With {.ProductID = 75, .ProductName =

"Rh nbr u Klosterbier", .Category = "Beverages", .UnitPrice = 7.75D,

.UnitsInStock = 125}) productList.Add(New Product With {.ProductID = 76, .ProductName =

"Lakkalik ri", .Category = "Beverages", .UnitPrice = 18D, .UnitsInStock =

57}) productList.Add(New Product With {.ProductID = 77, .ProductName =

"Original Frankfurter gr ne So e", .Category = "Condiments", .UnitPrice =

13D, .UnitsInStock = 32}) End Sub #End Region Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged txtOutput.Text = "" Select Case ComboBox1.SelectedIndex Case 0 Linq1() Case 1

Page 29: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments Linq2() Case 2 Linq3() Case 3 Linq4() Case 4 Linq5() Case 5 Linq6() Case 6 Linq7() Case 7 Linq8() Case 8 Linq9() Case 9 Linq10() Case 10 Linq11() Case 11 Linq12() Case 12 Linq13() Case 13 Linq14() Case 14 Linq15() Case 15 Linq16() Case 16 Linq17() Case 17 Linq18() Case 18 Linq19() Case 19 Linq20() Case 20 Linq21() Case 21 Linq22() Case 22 Linq23() Case 23 Linq24() Case 24 Linq25() Case 25 Linq26() Case 26 Linq27() Case 27 Linq28() Case 28 Linq29()

Page 30: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments Case 29 Linq30() Case 30 Linq31() Case 31 Linq32() End Select End Sub Public Sub PrintOutput(ByVal text As String) txtOutput.Text &= vbCrLf & text End Sub End Class

Page 31: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments

Lecture No.23 Assignment No. 1

Objective: Use the Northwind database and make LINQ to SQL and write the queries for following and display the result in a grid

• This displays the sum, avg of unitprice and no of products by categories • Display the products with selected columns sorted by UnitPrice • Display the products with selected columns sorted by UnitPrice • Display the products in category "Beaverages" • Displays all products

Commands Used:

1. LINQ to SQL

Output:

Output: Partial Public Class LINQtoSQLExamples Inherits Form Public Sub New() InitializeComponent() End Sub

Page 32: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments ''' <summary> ''' This displays the sum, avg of unitprice and no of products by categories ''' </summary> Private Sub Example5() Dim objNwdDataContext As New NorthwindDataContext() Dim result = From c In objNwdDataContext.Products _ Group c By c.Category.CategoryName Into g = Group, Sum(c.UnitPrice), Average(c.UnitPrice), Count() _ Select CategoryName, Sum, Average, Count dataGridView1.DataSource = result End Sub ''' <summary> ''' Display the products with selected columns sorted by UnitPrice ''' </summary> Private Sub Example4() Dim objNwdDataContext As New NorthwindDataContext() Dim result = From c In objNwdDataContext.Products _ Order By c.UnitPrice Descending _ Select c.ProductID, c.ProductName, c.Category.CategoryName, c.UnitPrice, c.Discontinued dataGridView1.DataSource = result End Sub ''' <summary> ''' Display the products in category name contains word "con" in sorted by UnitPrice ''' </summary> Private Sub Example3() Dim objNwdDataContext As New NorthwindDataContext() Dim result = From c In objNwdDataContext.Products _ Where c.Category.CategoryName.Contains("con") _ Select c dataGridView1.DataSource = result End Sub ''' <summary> ''' Display the products in category "Beaverages" ''' </summary> Private Sub Example2() Dim objNwdDataContext As New NorthwindDataContext() Dim result = From c In objNwdDataContext.Products _ Where c.Category.CategoryName = "Beverages" _ Select c dataGridView1.DataSource = result End Sub ''' <summary> ''' Displays all products ''' </summary> Private Sub Example1() Dim objNwdDataContext As New NorthwindDataContext() Dim result = From c In objNwdDataContext.Products _ Select c dataGridView1.DataSource = result

Page 33: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments End Sub Private Sub LINQtoSQLExamples_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load Example1() End Sub Private Sub comboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles comboBox1.SelectedIndexChanged Select Case comboBox1.SelectedIndex Case 0 Example1() Exit Select Case 1 Example2() Exit Select Case 2 Example3() Exit Select Case 3 Example4() Exit Select Case 4 Example5() Exit Select End Select End Sub End Class

Page 34: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments

Lecture No.23 Assignment No. 2

Objective: Use the Northwind database and create a data form for inserting, updating and deleting the records in Product table

Commands Used:

1. LINQ to SQL

Output:

Program: Partial Public Class ProductForm Inherits Form Private products As Object Public Sub New() InitializeComponent() End Sub

Page 35: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments Private Sub ProductForm_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load DataBind() End Sub ''' <summary> ''' Binds the product table with controls, category table to ComboBox ''' </summary> Private Sub DataBind() Dim obj As New NorthwindDataContext() Dim result = From c In obj.Categories _ Select c comboBox1.DisplayMember = "CategoryName" comboBox1.ValueMember = "CategoryID" comboBox1.DataSource = result products = From c In obj.Products _ Select c 'Remove any previous binding productIDTextBox.DataBindings.Clear() productNameTextBox.DataBindings.Clear() comboBox1.DataBindings.Clear() discontinuedCheckBox.DataBindings.Clear() unitPriceTextBox.DataBindings.Clear() reorderLevelTextBox.DataBindings.Clear() supplierIDTextBox.DataBindings.Clear() unitsInStockTextBox.DataBindings.Clear() unitsOnOrderTextBox.DataBindings.Clear() quantityPerUnitTextBox.DataBindings.Clear() 'perform binding productIDTextBox.DataBindings.Add("Text", products, "ProductID") productNameTextBox.DataBindings.Add("Text", products, "ProductName") comboBox1.DataBindings.Add("SelectedValue", products, "CategoryID") discontinuedCheckBox.DataBindings.Add("Checked", products, "Discontinued") unitPriceTextBox.DataBindings.Add("Text", products, "unitPrice") reorderLevelTextBox.DataBindings.Add("Text", products, "reorderLevel") supplierIDTextBox.DataBindings.Add("Text", products, "supplierID") unitsInStockTextBox.DataBindings.Add("Text", products, "unitsInStock") unitsOnOrderTextBox.DataBindings.Add("Text", products, "unitsOnOrder") quantityPerUnitTextBox.DataBindings.Add("Text", products, "quantityPerUnit") End Sub Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button2.Click BindingContext(products).Position += 1 End Sub

Page 36: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click BindingContext(products).Position -= 1 End Sub ''' <summary> ''' Make controls blank for adding new data ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> Private Sub button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button3.Click productIDTextBox.DataBindings.Clear() productNameTextBox.DataBindings.Clear() comboBox1.DataBindings.Clear() discontinuedCheckBox.DataBindings.Clear() unitPriceTextBox.DataBindings.Clear() reorderLevelTextBox.DataBindings.Clear() supplierIDTextBox.DataBindings.Clear() unitsInStockTextBox.DataBindings.Clear() unitsOnOrderTextBox.DataBindings.Clear() quantityPerUnitTextBox.DataBindings.Clear() productIDTextBox.Text = "" productNameTextBox.Text = "" comboBox1.Text = "" discontinuedCheckBox.Checked = False unitPriceTextBox.Text = "" reorderLevelTextBox.Text = "" supplierIDTextBox.Text = "" unitsInStockTextBox.Text = "" unitsOnOrderTextBox.Text = "" quantityPerUnitTextBox.Text = "" End Sub ''' <summary> ''' Add a new product ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> Private Sub button4_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button4.Click Dim objProduct As New Product() objProduct.ProductName = productNameTextBox.Text objProduct.UnitPrice = Convert.ToDecimal(unitPriceTextBox.Text) objProduct.QuantityPerUnit = quantityPerUnitTextBox.Text objProduct.ReorderLevel = Convert.ToInt16(reorderLevelTextBox.Text) objProduct.UnitsInStock = Convert.ToInt16(unitsInStockTextBox.Text) objProduct.UnitsOnOrder = Convert.ToInt16(unitsOnOrderTextBox.Text) objProduct.SupplierID = Convert.ToInt32(supplierIDTextBox.Text) objProduct.Discontinued = discontinuedCheckBox.Checked objProduct.CategoryID = Convert.ToInt16(comboBox1.SelectedValue) Dim objDC As New NorthwindDataContext()

Page 37: Lecture No.20 Assignment No. 1 - …memberfiles.freewebs.com/14/28/86002814/documents/VB 2008 Class... · Class Assignments Lecture No.20 ... Catalog=Northwind; ... ("select ProductID,ProductName

VB.NET Class Assignments objDC.Products.InsertOnSubmit(objProduct) 'add record objDC.SubmitChanges() ' commit the changes MessageBox.Show("Record added...") DataBind() End Sub ''' <summary> ''' Update the Product ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> Private Sub button5_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button5.Click Dim objDC As New NorthwindDataContext() Dim objProduct As Product = objDC.Products.[Single](Function(c) c.ProductID = Convert.ToInt32(productIDTextBox.Text)) objProduct.ProductName = productNameTextBox.Text objProduct.UnitPrice = Convert.ToDecimal(unitPriceTextBox.Text) objProduct.QuantityPerUnit = quantityPerUnitTextBox.Text objProduct.ReorderLevel = Convert.ToInt16(reorderLevelTextBox.Text) objProduct.UnitsInStock = Convert.ToInt16(unitsInStockTextBox.Text) objProduct.UnitsOnOrder = Convert.ToInt16(unitsOnOrderTextBox.Text) objProduct.SupplierID = Convert.ToInt32(supplierIDTextBox.Text) objProduct.Discontinued = discontinuedCheckBox.Checked objProduct.CategoryID = Convert.ToInt16(comboBox1.SelectedValue) objDC.SubmitChanges() ' commit the changes MessageBox.Show("Record updated...") End Sub ''' <summary> ''' Delete the product ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> Private Sub button6_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button6.Click Dim objDC As New NorthwindDataContext() Dim objProduct As Product = objDC.Products.[Single](Function(c) c.ProductID = Convert.ToInt32(productIDTextBox.Text)) objDC.Products.DeleteOnSubmit(objProduct) 'for deleting objDC.SubmitChanges() ' commit the changes MessageBox.Show("Record deleted...") End Sub End Class


Recommended