Friday, August 5, 2011

Friday August 5th


Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_LOad(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'only executed if page is being loaded for the first time
If Not IsPostBack Then
Dim iIndex As Integer
For iIndex = 50 To 500 Step 50
ddlMonthlyInvestment.Items.Add(iIndex)
Next iIndex
End If
End Sub

Protected Sub btnCalculate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim iMonths As Integer
Dim dInterestRate, dMonthlyInvestment As Decimal
Dim dFutureValue As Decimal
If IsValid Then
iMonths = txtYears.Text * 12
dInterestRate = txtInterestRate.Text / 12 / 100
dMonthlyInvestment = ddlMonthlyInvestment.SelectedValue
dFutureValue = FutureValue(iMonths, dInterestRate, dMonthlyInvestment)
lblFutureValue.Text = FormatCurrency(dFutureValue)
End If
End Sub

Private Function FutureValue(ByVal Months As Integer, ByVal InterestRate As Decimal, ByVal MonthlyInvestment As Decimal) As Decimal
Dim iIndex As Integer
Dim dFutureValue As Decimal
For iIndex = 1 To Months
dFutureValue = (dFutureValue + MonthlyInvestment) * (1 + InterestRate)
Next iIndex
Return dFutureValue
End Function

Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
txtInterestRate.Text = ""
txtYears.Text = ""
lblFutureValue.Text = ""
End Sub
End Class


and this is some of the ASP code


<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtInterestRate" Display="Dynamic"
ErrorMessage="Interest rate is required"></asp:RequiredFieldValidator>

<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="txtInterestRate" Display="Dynamic"
ErrorMessage="Interest Rate must range from 1 to 20" MaximumValue="20" MinimumValue="1" Type="Double"></asp:RangeValidator>
<p>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtYears" Display="Dynamic"
ErrorMessage="Number of years is required"></asp:RequiredFieldValidator>

<asp:RangeValidator ID="RangeValidator2" runat="server" ControlToValidate="txtYears" Display="Dynamic"
ErrorMessage="Years must range from 1 to 45" MaximumValue="45" MinimumValue="1" Type="Integer"></asp:RangeValidator>




using System;
using System.Data;
using System.Data.SqlClient;
//It's not only necessary to use the namespace System.Configuration. You have also to add the reference to the assembly System.Configuration.dll ,
//by right-click-ing on the References tab, choose add reference and then find System.Configuration
using System.Configuration;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//enumerate connection string
Console.WriteLine("---Connection string enumeration---");
foreach (ConnectionStringSettings css in ConfigurationManager.ConnectionStrings)
{
Console.WriteLine(css.Name);
Console.WriteLine(css.ProviderName);
Console.WriteLine(css.ConnectionString);
}
//retrieve a connection string and open/close it
Console.WriteLine("\n---Using a connection string---");
Console.WriteLine("-> Retrieving connection string AdventureWorks");

//retrieve connection string and open/close it
string sqlConnectString = ConfigurationManager.ConnectionStrings["AdventureWorks"].ConnectionString;
SqlConnection connection = new SqlConnection(sqlConnectString);

Console.WriteLine("-> Opening Connection String");

connection.Open();
Console.WriteLine("Connection string state = {0}", connection.State);
connection.Close();
Console.WriteLine("-> Closing connection string.");
Console.WriteLine("Connection string state = {0}", connection.State);
Console.WriteLine("\nPres any key to continue.");
Console.ReadKey();
}
}
}




using System;
using System.Data.SqlClient;

namespace BuildConnectionString
{
class Program
{
static void Main(string[] args)
{
//create a connection string builder
SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder();
//define connection string attributes using three techniques
//also prepend the string with the @ character
csb.DataSource = @"Alfred-PC\SQLExpress";
csb.Add("Initial Catalog", "AdventureWorks");
csb["Integrated Security"] = true;

//output the connection string from the connection string builder
Console.WriteLine("Connection string:\n{0}", csb.ConnectionString);
//create a connection string from the connection string builder
SqlConnection connection = new SqlConnection(csb.ConnectionString);
//open and close the connection
connection.Open();
Console.WriteLine("\nConnectionState={0}", connection.State);
connection.Close();
Console.WriteLine("ConnectionState={0}", connection.State);
Console.WriteLine("\nPress any key to continue");
Console.ReadKey();
}
}
}

No comments: