Tuesday, May 25, 2010

Login Control

Default.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:Login ID="Login1" runat="server" BackColor="#F7F6F3" BorderColor="#E6E2D8"
BorderPadding="4" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana"
Font-Size="0.8em" ForeColor="#333333">
<TextBoxStyle Font-Size="0.8em" />
<LoginButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid"
BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284775" />
<InstructionTextStyle Font-Italic="True" ForeColor="Black" />
<TitleTextStyle BackColor="#5D7B9D" Font-Bold="True" Font-Size="0.9em"
ForeColor="White" />
</asp:Login>

</div>
</form>
</body>
</html>

Default.aspx.vb

Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page


Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate
'Read the xml file
'Check to see if anything in the file matches the username
'and password in the login
'if it does authenticate and redirect
'if not make them reenter
e.Authenticated = False

Dim ds As New DataSet
ds.ReadXml(MapPath("login.xml"))

For Each row As DataRow In ds.Tables(0).Rows
If Login1.UserName = row("username").ToString AndAlso Login1.Password = row("password") Then
e.Authenticated = True
Session("username") = Login1.UserName
Response.Redirect("Default2.Aspx")
End If
Next

End Sub
End Class

Default2.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Welcome</h1>

</div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>

Default2.aspx.vb


Partial Class Default2
Inherits System.Web.UI.Page


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Session("username") = Nothing Then
Response.Redirect("Default.aspx")
End If

Label1.Text = Session("userName")
End Sub
End Class

login.xml


<?xml version="1.0" encoding="utf-8" ?>

<calogins>

<calogin>
<username>jones</username>
<password>pa$$w0rd</password>
</calogin>

<calogin>
<username>Smith</username>
<password>pa$$w0rd</password>
</calogin>

</calogins>

Thursday, May 20, 2010

BirthDay Calculator

Default.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>How many days old are you</h1>
<asp:Label ID="Label1" runat="server" Text="Enter Your birthday"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Calculate" /> 
<asp:Label ID="Label2" runat="server" Text=""></asp:Label>

</div>
</form>
</body>
</html>

Default.aspx.vb

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
If IsDate(TextBox1.Text) Then
Dim bday As DateTime = DateTime.Parse(TextBox1.Text)
Dim days As Integer
days = ((DateTime.Now - bday).TotalDays) - 1
Label2.Text = days.ToString
Else
Label2.Text = "Enter a valid date"
End If

Catch
Dim ex As New Exception("that is not a date, buddy")
Label2.Text = ex.Message



End Try

End Sub
End Class

How to start A C++ Program in Visual Studio

Follow these instructions to get started with a C++ project in Visual Studio. We will begin with an empty Console project. This allows the best approximation of ANSI C++

1.Start Visual Studio
2.Go to FILE/ NEW PROJECT
3.Choose OTHER LANGUAGES/C++
4.Choose “WIN32 Console Application”
5.Give the solution a name click Next
6.The Win32 Application Wizard opens
7.Click Application Settings
8.Leave application type Console Application
9.Under additional options choose empty Project
10.Click finish
11.. Click Solution Explorer—you should see three folders
12.Right click source files and click ADD
13.ADD NEW ITEM
14.Choose C++ FILE (.cpp)
15.Give it a Name
16.Click ADD
17.In the code put

#include <iostream>
using namespace std;

int main()
{
}

Now you are ready to start

Thursday, May 13, 2010

MasterPage.master

<%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Master States Two</h1>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

</asp:ContentPlaceHolder>
<p><a href="Default3.aspx">Confirmation</a></p>
</div>
</form>
</body>
</html>

Default2.aspx

<%@ Page Title="Donation form" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<meta content="copyrighted by me" name="about" />
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<h2>Donate</h2>
<p>
<asp:Label ID="Label1" runat="server" Text="First Name"></asp:Label> 
<asp:TextBox
ID="txtFirstName" runat="server"></asp:TextBox> <br />

<asp:Label ID="Label2" runat="server" Text="* Last Name"></asp:Label> 
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtLastName" ErrorMessage="Please Enter Last name"
Display="None"></asp:RequiredFieldValidator>
<br />

<asp:Label ID="Label3" runat="server" Text="* Donation Amount"></asp:Label>
<asp:TextBox ID="txtDonation" runat="server"></asp:TextBox>

<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtDonation" ErrorMessage="Please enter a donation"
Display="None"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ErrorMessage="Enter a valid numerical amount" Type="Double"
Operator="DataTypeCheck" ControlToValidate="txtDonation" Display="None"></asp:CompareValidator>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />

</p>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
<p>
 </p>

</asp:Content>

Default2.aspx.vb


Partial Class Default2
Inherits System.Web.UI.Page

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
'PassByQueryString()

PassBySessionVariable()


End Sub

Protected Sub PassByQueryString()
Dim first As String = txtFirstName.Text
Dim last As String = txtLastName.Text
Dim donation As String = txtDonation.Text

Response.Redirect("Default3.aspx?firstname=" & first & "&lastname=" & last & "&donation=" & donation)
End Sub

Protected Sub PassBySessionVariable()
Dim first As String = txtFirstName.Text
Dim last As String = txtLastName.Text
Dim donation As String = txtDonation.Text

Session("firstname") = first
Session("lastname") = last
Session("donation") = donation

Response.Redirect("Default3.aspx")
End Sub
End Class

Default3.aspx

<%@ Page Title="Confirmation" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Default3.aspx.vb" Inherits="Default3" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<h2>Confirm</h2>
<asp:Label ID="lblFirstName" runat="server" Text="Label"></asp:Label><br />
<asp:Label ID="lblLastName" runat="server" Text="Label"></asp:Label><br />
<asp:Label ID="lblDonation" runat="server" Text="Label"></asp:Label><br />
<asp:Label ID="Label1" runat="server" Text="thank You"></asp:Label>
</asp:Content>

Default3.aspx.vb


Partial Class Default3
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'GetFromQueryString()
If Session("lastname") = Nothing Then
Response.Redirect("Default2.aspx")
Else
GetFromSessionVariable()
End If

End Sub

Public Sub GetFromQueryString()
lblFirstName.Text = Request.QueryString("firstname")
lblLastName.Text = Request.QueryString("lastname")
lblDonation.Text = Request.QueryString("donation")
End Sub

Protected Sub GetFromSessionVariable()
lblFirstName.Text = Session("firstname")
lblLastName.Text = Session("lastname")
lblDonation.Text = Session("donation")
End Sub
End Class

Thursday, May 6, 2010

Suggestions for coding

Here are some suggestion for troubleshooting while you are coding.

First, don't get lost in the details. Try to step back and see the pattern of what you are doing.

It doesn't hurt to write out the steps you need to take.

Use the intellisense. It tells you what objects or methods are available in the context of where the cursor is.

Most errors are typos, either misspelling a variable name or an object name. If VS claims an object doesn't exist and you know you dimmed it, the problem is probably that you mispelled it or it is out of scope.

Read the error messages. Even if they aren't exacty right about where the error is occuring, they can usually lead you there.

In VS if you doubleclick on a error message at the bottom of the code window it will move the cursor to the line and postion of the error. Always start with the first error message. Correcting it may clear dozens of other errors that cascaded from the first one.

Develop your program in discrete steps. (Do one small thing at a time.) Test your code after each step. That way if an error occurs you know it has to be in the most recent code you wrote.

For the classes, build your program after you complete each class. (You can't actually run the code in any useful way until you have all the necessary classes in place, but you can make sure they compile OK.)

If you find yourself struggling with something over a long period of time. Stop. Walk away from it for a couple of hours or even a day or so. You will often find whey you come back, the problem was obvious and staring you in the face, but you just couldn't see it at the time.

If you're having problems with a section of code, comment out parts to test variations. Don't delete the commented out code until you are sure you have it working properly. Often in a panic, people delete working and necessary code in a wild attempt to change things to make them work.

Related to this, only make one change at a time. Determine the effects of that change before trying to make a second change.

Wednesday, May 5, 2010

Testing Plan

Here is a picture of a testing plan for the core use case of the card reader app.

Tuesday, May 4, 2010

ADO Example complete

The default.aspx has not changed

here is the default.aspx.vb

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim NewDonor As New Donor("20202")
NewDonor.LastName = txtLastName.Text
NewDonor.FirstName = txtFirstName.Text
NewDonor.Address = txtStreet.Text
NewDonor.PersonCity = txtCity.Text
NewDonor.PersonState = txtState.Text
NewDonor.Zipcode = txtZip.Text

Dim myGift As New Donation(Double.Parse(txtDonation.Text))

Dim data As New DataLayer(NewDonor, myGift)
data.WriteDonation()
End Sub
End Class

Person donor and donation have not changed here is the datalayer

Imports System.Data
Imports System.Data.SqlClient

Public Class DataLayer

Private don As Donor
Private gift As Donation
Private connect As SqlConnection

Sub New(ByVal d As Donor, ByVal dn As Donation)
don = d
gift = dn
connect = New SqlConnection("Data Source=localhost;initial catalog=Communityassist;integrated security=true")
End Sub
Public Function WriteDonation() As Boolean
'enter into table person get fields from donor
'Get the person id which is an auto generated number
'enter into table Personaddress from Donor
'enter into table donation from donation
Dim success As Boolean = True

'insert into Person
Dim personSQL As String = "Insert into Person (Lastname, Firstname) Values (@lastname, @firstname)"

'Get New Person Key
Dim keySQL As String = "Select @@Identity"

'insert into PersonAddress
Dim addressSQL As String = "Insert into PersonAddress(Street, State, City, Zip, PersonKey) Values (@Street,@State, @City,@Zip, @Person)"

'insert into Donation
Dim donationSql As String = "Insert into Donation(DonationDate,DonationAmount,Personkey, EmployeeKey) Values(@Date, @Amount, @Person, 1)"

'set up the command objects
Dim personCommand As New SqlCommand(personSQL, connect)
personCommand.Parameters.AddWithValue("@lastname", don.LastName)
personCommand.Parameters.AddWithValue("@firstname", don.FirstName)


Dim keyCommand As New SqlCommand(keySQL, connect)

Dim addressCommand As New SqlCommand(addressSQL, connect)
addressCommand.Parameters.AddWithValue("@Street", don.Address)
addressCommand.Parameters.AddWithValue("@City", don.PersonCity)
addressCommand.Parameters.AddWithValue("@State", don.PersonState)
addressCommand.Parameters.AddWithValue("@zip", don.Zipcode)
addressCommand.Parameters.Add("@Person", SqlDbType.Int)

Dim donationCommand As New SqlCommand(donationSql, connect)
donationCommand.Parameters.AddWithValue("@Date", gift.DonationDate)
donationCommand.Parameters.AddWithValue("@Amount", gift.DonationAmount)
donationCommand.Parameters.Add("@Person", SqlDbType.Int)

connect.Open()

personCommand.ExecuteNonQuery()
Dim key As Integer = CInt(keyCommand.ExecuteScalar)
addressCommand.Parameters("@Person").Value = key
donationCommand.Parameters("@Person").Value = key

addressCommand.ExecuteNonQuery()
donationCommand.ExecuteNonQuery()

connect.Close()

Return success

End Function


End Class