Monday, December 12, 2011

Monday 12/12/11

Imports System.Reflection

Module Module1

Sub Main()
Console.WriteLine("***** Welcome to MyTypeViewer *****")
Dim typeName As String = String.Empty

Do
Console.WriteLine()
Console.WriteLine("Enter a type name to evaluate")
Console.Write("or enter Q to quit: ")

'get name of type
typeName = Console.ReadLine()
'does user want to quit?
If typeName.ToUpper() = "Q" Then
Exit Do
End If

'try to display type
Try
Dim t As Type = Type.GetType(typeName)
Console.WriteLine()
ListVariousStats(t)
ListFields(t)
ListProps(t)
ListMethods(t)
ListInterfaces(t)
Catch ex As Exception
Console.WriteLine("Sorry, cant' find {0}.", typeName)
End Try
Loop
End Sub


Public Sub ListMethods(ByVal t As Type)
Console.WriteLine("***** Methods *****")
Dim mi As MethodInfo() = t.GetMethods()
For Each m As MethodInfo In mi
Dim retVal As String = m.ReturnType.FullName()
Dim paramInfo As String = "( "
For Each pu As ParameterInfo In m.GetParameters()
paramInfo &= String.Format("{0} {1} ", pu.ParameterType, pu.Name)
Next
paramInfo &= " )"
Console.WriteLine("->{0} {1} {2}", retVal, m.Name, paramInfo)
Next
Console.WriteLine()

End Sub
'display field names of type
Public Sub ListFields(ByVal t As Type)
Console.WriteLine("***** Field *****")
Dim fi As FieldInfo() = t.GetFields()
For Each field As FieldInfo In fi
Console.WriteLine("->{0}", field.Name)
Next
Console.WriteLine()
End Sub

'display property names of type
Public Sub ListProps(ByVal t As Type)
Console.WriteLine("***** Properties *****")
Dim pi As PropertyInfo() = t.GetProperties()
For Each prop As PropertyInfo In pi
Console.WriteLine("->{0}", prop.Name)
Next
Console.WriteLine()
End Sub

'print out th enames of any interfaces
'supported on the incoming type
Public Sub ListInterfaces(ByVal t As Type)
Console.WriteLine("***** Interfaces *****")
Dim ifaces As Type() = t.GetInterfaces()
For Each i As Type In ifaces
Console.WriteLine("->{0}", i.Name)
Next
Console.WriteLine()
End Sub

Public Sub ListVariousStats(ByVal t As Type)
Console.WriteLine("***** Various Statistics *****")
Console.WriteLine("Base class is: {0}", t.BaseType)
Console.WriteLine("Is type abstract? {0}", t.IsAbstract)
Console.WriteLine("Is type sealed? {0}", t.IsSealed)
Console.WriteLine("Is type generic? {0}", t.IsGenericTypeDefinition)
Console.WriteLine("Is type a class type? {0}", t.IsClass)
Console.WriteLine()
End Sub
End Module

Tuesday, November 8, 2011

Tuesday 11.08.11

Module Module1

Sub Main()
'show banner
DisplayBanner()
'Get user's name and say howdy
GreetUser()
End Sub

Sub DisplayBanner()
'Get the current color of the console text
Dim currColor As ConsoleColor = Console.ForegroundColor

'set text color to yellow
Console.ForegroundColor = ConsoleColor.Yellow
Console.WriteLine("***** Welcome to FunWithModules *****")
Console.WriteLine("This simple program illustrates the role")
Console.WriteLine("of the Module type.")
Console.WriteLine("**********************************")

'reset the previous color of your console text.
Console.ForegroundColor = currColor
Console.WriteLine()
End Sub

Sub GreetUser()
Dim userName As String
Console.Write("Please enter your name: ")
userName = Console.ReadLine()
Console.WriteLine("Hello there {0}. Nice to meet ya.", userName)
End Sub
End Module


Public Class Program

Shared Function Main(ByVal args As String()) As Integer
'OS running this app?
Console.WriteLine("Current OS: {0}", Environment.OSVersion)

'List the drives on this machine.
Dim drives As String() = Environment.GetLogicalDrives()
Dim d As String
For Each d In drives
Console.WriteLine("You have a drive named {0}.", d)
Next


'Which version of the .NET platform is running this app?
Console.WriteLine("Executing version of .NET: {0}", Environment.Version)
Return 0

End Function

End Class


Module Module1

Sub Main()
Console.WriteLine("***** Fun With Data Types *****")
Console.WriteLine("Max of Integer: {0}", Integer.MaxValue)
Console.WriteLine("Min of Integer: {0}", Integer.MinValue)
Console.WriteLine("Max of Double: {0}", Double.MaxValue)
Console.WriteLine("Min of Double: {0}", Double.MinValue)
Console.WriteLine("Boolean.FalseString: {0}", Boolean.FalseString)
Console.WriteLine("Boolean.TrueString: {0}", Boolean.TrueString)
Console.ReadLine()

End Sub

End Module

Friday, November 4, 2011

Friday 11.4.11

Imports System.Web.Configuration
Imports System.Data.SqlClient
Imports System.Data

Partial Class listDataBinding
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Not Page.IsPostBack) Then
'create the Command and the Connection
Dim connectionString As String = WebConfigurationManager.ConnectionStrings("Northwind").ConnectionString
Dim sql As String = "SELECT EmployeeID, TitleOfCourtesy + ' ' + FirstName + ' ' + LastName As FullName FROM Employees"
Dim con As New SqlConnection(connectionString)
Dim cmd As New SqlCommand(sql, con)

Try
'Open the connection and get the data reader
con.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
'Bind the DataReader to the list
lstNames.DataSource = reader
lstNames.DataBind()
reader.Close()

Catch ex As Exception
Response.Write(ex.ToString())
Finally
'close the connection
con.Close()
End Try
End If
End Sub

Protected Sub cmdGetSelection_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdGetSelection.Click
Result.Text &= "Selected Employees:"
For Each li As ListItem In lstNames.Items
If li.Selected Then
Result.Text &= String.Format("
  • ({0}) {1}
  • ", li.Value, li.Text)
    End If
    Next
    End Sub
    End Class

    Friday, October 14, 2011

    Friday 10.14.11

    using System;
    using System.Data;
    using System.Data.SqlClient;

    namespace RetrieveSingleValueFromQuery
    {
    class Program
    {
    //the ExecuteScalar() method of the Command object returns a single value from the dta soruce rather than a collection of records as
    //a table or data stream
    static void Main(string[] args)
    {
    string sqlConnectString = @"Data Source=Alfred-PC\SQLExpress; Integrated security=SSPI; Initial Catalog=AdventureWorks;";

    string sqlSelect = "SELECT COUNT(*) FROM Person.Contact";

    SqlConnection connection = new SqlConnection(sqlConnectString);

    //create the scalar command and open the connection
    SqlCommand command = new SqlCommand(sqlSelect, connection);
    connection.Open();

    //execute the scalar SQl statement and store resuts.
    int count = Convert.ToInt32(command.ExecuteScalar());
    connection.Close();

    Console.WriteLine("Record count in Person.Contact = {0}", count);

    Console.WriteLine();
    Console.ReadLine();
    }
    }
    }