Here is the code on the form side (Where you enter the data)with all three different methods
Partial Class Default2
Inherits System.Web.UI.Page
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
' QueryString()
'SessionMethod()
CookieMethod()
End Sub
Protected Sub QueryString()
Response.Redirect("Default3.aspx?Firstname=" & txtFirstName.Text & "&Lastname=" & txtLastName.Text)
End Sub
Protected Sub SessionMethod()
Session("First") = txtFirstName.Text
Session("Last") = txtLastName.Text
Response.Redirect("Default3.aspx")
End Sub
Protected Sub CookieMethod()
Dim chocolateChip As New HttpCookie("sugar")
chocolateChip("first") = txtFirstName.Text
chocolateChip("last") = txtLastName.Text
chocolateChip.Path = "C:\Sugar.txt"
Response.Cookies.Add(chocolateChip)
Response.Redirect("Default3.aspx")
End Sub
End Class
Here is the code from the receiving side:
Partial Class Default3
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'GetQueryString()
'GetSession()
ReadCookie()
End Sub
Protected Sub GetQueryString()
lblFirstName.Text = Request.QueryString("FirstName")
lblLastName.Text = Request.QueryString("LastName")
End Sub
Protected Sub GetSession()
If Session("First") <> Nothing Then
lblFirstName.Text = Session("First")
lblLastName.Text = Session("Last")
End If
End Sub
Protected Sub ReadCookie()
Dim cookie As HttpCookie = Request.Cookies("C:\sugar.txt")
lblFirstName.Text = cookie("first")
lblLastName.Text = cookie("last")
End Sub
End Class
Showing posts with label ITC 172. Show all posts
Showing posts with label ITC 172. Show all posts
Thursday, February 18, 2010
Friday, February 5, 2010
Donation Site code
Here is the code for the Donation site with comments. First the web form. There are no changes here:
<%@ 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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>Enter your information</p>
<asp:Table ID="Table1" runat="server" Width="248px">
<asp:TableRow>
<asp:TableCell>Last Name</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>First Name</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Address</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>City</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>State</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtState" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Zip code</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtZipcode" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Home Phone</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtHomePhone" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Email</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Donation Amount</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtDonation" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell> </asp:TableCell>
<asp:TableCell>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</div>
</form>
</body>
</html>
Here is the code for Default.aspx.vb
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Try
Dim dude As New Person
dude.PersonFirstName = txtFirstName.Text
dude.PersonLastName = txtLastName.Text
dude.PersonAddress = txtAddress.Text
dude.PersonCity = txtCity.Text
dude.PersonState = txtState.Text
dude.PersonZipcode = txtZipcode.Text
dude.PersonHomePhone = txtDonation.Text
dude.PersonEmail = txtEmail.Text
Dim gift As New Donation
gift.DonationDate = DateTime.Now
gift.DonationAmount = Double.Parse(txtDonation.Text)
Dim data As New DataLayer(dude, gift)
Response.Write("thanks for the donation")
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
End Class
Now here is the Person class--no changes here:
Imports Microsoft.VisualBasic
'*****************************************
'This class ecnapsulates a person, including
'the address and contacts
'it consists only of fields (private class
'variables and properties that expose those
'variables to other classes
'****************************************
Public Class Person
'private class level variables (fields)
Private firstName As String
Private lastName As String
Private address As String
Private city As String
Private state As String
Private zipcode As String
Private homePhone As String
Private email As String
Private PKey As Integer
'public properties
Public Property PersonFirstName() As String
Get
Return FirstName
End Get
Set(ByVal value As String)
FirstName = value
End Set
End Property
Public Property PersonLastName() As String
Get
Return LastName
End Get
Set(ByVal value As String)
LastName = value
End Set
End Property
Public Property PersonAddress() As String
Get
Return address
End Get
Set(ByVal value As String)
address = value
End Set
End Property
Public Property PersonCity() As String
Get
Return city
End Get
Set(ByVal value As String)
city = value
End Set
End Property
Public Property PersonState() As String
Get
Return state
End Get
Set(ByVal value As String)
'in this porpery I validated the input
'if it is longer than 2 characters
'throw an error message back
'to the calling method
If value.Length > 2 Then
Dim ex As New Exception("use two letters for a state")
Throw ex
Else
state = value
End If
End Set
End Property
Public Property PersonZipcode() As String
Get
Return zipcode
End Get
Set(ByVal value As String)
zipcode = value
End Set
End Property
Public Property PersonHomePhone() As String
Get
Return homePhone
End Get
Set(ByVal value As String)
homePhone = value
End Set
End Property
Public Property PersonEmail() As String
Get
Return email
End Get
Set(ByVal value As String)
email = value
End Set
End Property
Public Property PersonKey() As Integer
Get
Return PKey
End Get
Set(ByVal value As Integer)
PKey = value
End Set
End Property
End Class
Here is the donation class--also no changes
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
'**************************************
'this class encapsulates a donation
'it consists of three fields and
'three properties
'*************************************
Public Class Donation
Private amount As Double
Private donDate As Date
Private Pkey As Integer
Public Property DonationAmount() As Double
Get
Return amount
End Get
Set(ByVal value As Double)
amount = value
End Set
End Property
Public Property DonationDate() As Date
Get
Return donDate
End Get
Set(ByVal value As Date)
donDate = value
End Set
End Property
Public Property DonationPersonKey() As Integer
Get
Return Pkey
End Get
Set(ByVal value As Integer)
Pkey = value
End Set
End Property
End Class
Here is the datalayer class. The one thing that has changed is the WriteDonation method is called in the constructor. As noted in the previous post, this is where the error was.
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Data 'import generic data objects
Imports System.Data.SqlClient 'import sql server specific objects
Public Class DataLayer
Private pers As Person
Private don As Donation
Private connect As SqlConnection
Public Sub New(ByRef p As Person, ByRef d As Donation)
'assign the passed objects to the internal fields
pers = p
don = d
'instantiate the sqlconnection string to enable
'connections to the database
connect = New SqlConnection("Data source=.\sqlexpress;initial catalog=communityAssist;integrated security=true;")
'call the method that actually writes the data
writeDonation()
End Sub
Sub writeDonation()
'*******************************************
'this method takes the values stored in the
'person and donation objects and writes them
'to the database. If you are clever you could
'break this method into smaller methods
'each of which does just one thing
'here we did it together so the pattern is
'cleare
'*******************************************
'this will insert into person
'first set up the sql insert statement. the @last, etc are
'sql variables
Dim PersonSql As String = "Insert into Person(lastName, Firstname) Values(@last, @first)"
'instantiate the command object and pass the sql and connection to its constructor
Dim personCommand As New SqlCommand(PersonSql, connect)
'Create parameters to store and assign values to the variables in the sql script
personCommand.Parameters.AddWithValue("@Last", pers.PersonLastName)
personCommand.Parameters.AddWithValue("@first", pers.PersonFirstName)
'will get the new PersonKey
'the @@identiy is a built in SQL Server variable
'that returns the last identity (autonumber)
'generated by the server--this will be the personkey
'from the person table where we just inserted a record
'we need the personkey to tie the address,
'contact information, and donation to the person
Dim personKeySql As String = "Select @@identity"
Dim personKeyCommand As New SqlCommand(personKeySql, connect)
'inserts into PersonAddress
Dim addressSQL As String = "Insert into PersonAddress(street, city, state, zip, personKey) Values(@street, @city, @state,@zip, @personKey)"
Dim addressCommand As New SqlCommand(addressSQL, connect)
addressCommand.Parameters.AddWithValue("@street", pers.PersonAddress)
addressCommand.Parameters.AddWithValue("@City", pers.PersonCity)
addressCommand.Parameters.AddWithValue("@state", pers.PersonState)
addressCommand.Parameters.AddWithValue("@zip", pers.PersonZipcode)
addressCommand.Parameters.Add("@PersonKey", SqlDbType.Int)
'insert into contact for home phone
Dim homePhoneSql As String = "Insert into PersonContact(ContactInfo, personKey, contactTypeKey)Values(@ContactInfo, @personKey, 1)"
Dim homephoneCommand As New SqlCommand(homePhoneSql, connect)
homephoneCommand.Parameters.AddWithValue("@ContactInfo", pers.PersonHomePhone)
homephoneCommand.Parameters.Add("@PersonKey", SqlDbType.Int)
'inserts into contact for email
Dim emailsql As String = "Insert into PersonContact(ContactInfo, personKey, contactTypeKey)Values(@ContactInfo, @personKey, 6)"
Dim emailCommand As New SqlCommand(emailsql, connect)
emailCommand.Parameters.AddWithValue("@ContactInfo", pers.PersonEmail)
emailCommand.Parameters.Add("@PersonKey", SqlDbType.Int)
'inserts into donation
Dim donationSql As String = "insert into donation(donationdate, donationamount,Personkey)Values(@date, @amount, @PersonKey)"
Dim donationCommand As New SqlCommand(donationSql, connect)
donationCommand.Parameters.AddWithValue("@Date", don.DonationDate)
donationCommand.Parameters.AddWithValue("@Amount", don.DonationAmount)
donationCommand.Parameters.Add("@PersonKey", SqlDbType.Int)
'now that everything is set up we will open
'the connection to the database.
'if there are any problems with the connection string
'the error will fall here
connect.Open()
Dim pk As Integer 'to store the personKey
'the transaction object is used to group
'all the statements into one thing
'either all of them happen or none
'of them happen
Dim tran As SqlTransaction
tran = connect.BeginTransaction
'here we assign the new transaction
'to all the sqlcommands
personCommand.Transaction = tran
personKeyCommand.Transaction = tran
addressCommand.Transaction = tran
homephoneCommand.Transaction = tran
emailCommand.Transaction = tran
donationCommand.Transaction = tran
'try the following code and catch any errors
Try
'first we insert the person
'ExecuteNonQuery is for any command
'that doesn't return results
personCommand.ExecuteNonQuery()
'now we get the person key. ExecuteScaler
'is used for queries that will return only
'a single value. the result an integer
'so we cast it CInt to an integer
pk = CInt(personKeyCommand.ExecuteScalar)
'Now we assign the value of personkey
'to the parameters of the
'various commands
addressCommand.Parameters("@PersonKey").Value = pk
homephoneCommand.Parameters("@PersonKey").Value = pk
emailCommand.Parameters("@PersonKey").Value = pk
donationCommand.Parameters("@PersonKey").Value = pk
'Now we execute each of the remaining commands
addressCommand.ExecuteNonQuery()
homephoneCommand.ExecuteNonQuery()
emailCommand.ExecuteNonQuery()
donationCommand.ExecuteNonQuery()
'if we don't have an error by now
'commit them all
tran.Commit()
Catch ex As Exception
'if there is an error undo it all
tran.Rollback()
'throw the error back to the form
Throw ex
Finally
'no matter what, close the connections
connect.Close()
End Try
End Sub
End Class
It all works on my machine
<%@ 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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>Enter your information</p>
<asp:Table ID="Table1" runat="server" Width="248px">
<asp:TableRow>
<asp:TableCell>Last Name</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>First Name</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Address</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>City</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>State</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtState" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Zip code</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtZipcode" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Home Phone</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtHomePhone" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Email</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Donation Amount</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtDonation" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell> </asp:TableCell>
<asp:TableCell>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</div>
</form>
</body>
</html>
Here is the code for Default.aspx.vb
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Try
Dim dude As New Person
dude.PersonFirstName = txtFirstName.Text
dude.PersonLastName = txtLastName.Text
dude.PersonAddress = txtAddress.Text
dude.PersonCity = txtCity.Text
dude.PersonState = txtState.Text
dude.PersonZipcode = txtZipcode.Text
dude.PersonHomePhone = txtDonation.Text
dude.PersonEmail = txtEmail.Text
Dim gift As New Donation
gift.DonationDate = DateTime.Now
gift.DonationAmount = Double.Parse(txtDonation.Text)
Dim data As New DataLayer(dude, gift)
Response.Write("thanks for the donation")
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
End Class
Now here is the Person class--no changes here:
Imports Microsoft.VisualBasic
'*****************************************
'This class ecnapsulates a person, including
'the address and contacts
'it consists only of fields (private class
'variables and properties that expose those
'variables to other classes
'****************************************
Public Class Person
'private class level variables (fields)
Private firstName As String
Private lastName As String
Private address As String
Private city As String
Private state As String
Private zipcode As String
Private homePhone As String
Private email As String
Private PKey As Integer
'public properties
Public Property PersonFirstName() As String
Get
Return FirstName
End Get
Set(ByVal value As String)
FirstName = value
End Set
End Property
Public Property PersonLastName() As String
Get
Return LastName
End Get
Set(ByVal value As String)
LastName = value
End Set
End Property
Public Property PersonAddress() As String
Get
Return address
End Get
Set(ByVal value As String)
address = value
End Set
End Property
Public Property PersonCity() As String
Get
Return city
End Get
Set(ByVal value As String)
city = value
End Set
End Property
Public Property PersonState() As String
Get
Return state
End Get
Set(ByVal value As String)
'in this porpery I validated the input
'if it is longer than 2 characters
'throw an error message back
'to the calling method
If value.Length > 2 Then
Dim ex As New Exception("use two letters for a state")
Throw ex
Else
state = value
End If
End Set
End Property
Public Property PersonZipcode() As String
Get
Return zipcode
End Get
Set(ByVal value As String)
zipcode = value
End Set
End Property
Public Property PersonHomePhone() As String
Get
Return homePhone
End Get
Set(ByVal value As String)
homePhone = value
End Set
End Property
Public Property PersonEmail() As String
Get
Return email
End Get
Set(ByVal value As String)
email = value
End Set
End Property
Public Property PersonKey() As Integer
Get
Return PKey
End Get
Set(ByVal value As Integer)
PKey = value
End Set
End Property
End Class
Here is the donation class--also no changes
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
'**************************************
'this class encapsulates a donation
'it consists of three fields and
'three properties
'*************************************
Public Class Donation
Private amount As Double
Private donDate As Date
Private Pkey As Integer
Public Property DonationAmount() As Double
Get
Return amount
End Get
Set(ByVal value As Double)
amount = value
End Set
End Property
Public Property DonationDate() As Date
Get
Return donDate
End Get
Set(ByVal value As Date)
donDate = value
End Set
End Property
Public Property DonationPersonKey() As Integer
Get
Return Pkey
End Get
Set(ByVal value As Integer)
Pkey = value
End Set
End Property
End Class
Here is the datalayer class. The one thing that has changed is the WriteDonation method is called in the constructor. As noted in the previous post, this is where the error was.
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Data 'import generic data objects
Imports System.Data.SqlClient 'import sql server specific objects
Public Class DataLayer
Private pers As Person
Private don As Donation
Private connect As SqlConnection
Public Sub New(ByRef p As Person, ByRef d As Donation)
'assign the passed objects to the internal fields
pers = p
don = d
'instantiate the sqlconnection string to enable
'connections to the database
connect = New SqlConnection("Data source=.\sqlexpress;initial catalog=communityAssist;integrated security=true;")
'call the method that actually writes the data
writeDonation()
End Sub
Sub writeDonation()
'*******************************************
'this method takes the values stored in the
'person and donation objects and writes them
'to the database. If you are clever you could
'break this method into smaller methods
'each of which does just one thing
'here we did it together so the pattern is
'cleare
'*******************************************
'this will insert into person
'first set up the sql insert statement. the @last, etc are
'sql variables
Dim PersonSql As String = "Insert into Person(lastName, Firstname) Values(@last, @first)"
'instantiate the command object and pass the sql and connection to its constructor
Dim personCommand As New SqlCommand(PersonSql, connect)
'Create parameters to store and assign values to the variables in the sql script
personCommand.Parameters.AddWithValue("@Last", pers.PersonLastName)
personCommand.Parameters.AddWithValue("@first", pers.PersonFirstName)
'will get the new PersonKey
'the @@identiy is a built in SQL Server variable
'that returns the last identity (autonumber)
'generated by the server--this will be the personkey
'from the person table where we just inserted a record
'we need the personkey to tie the address,
'contact information, and donation to the person
Dim personKeySql As String = "Select @@identity"
Dim personKeyCommand As New SqlCommand(personKeySql, connect)
'inserts into PersonAddress
Dim addressSQL As String = "Insert into PersonAddress(street, city, state, zip, personKey) Values(@street, @city, @state,@zip, @personKey)"
Dim addressCommand As New SqlCommand(addressSQL, connect)
addressCommand.Parameters.AddWithValue("@street", pers.PersonAddress)
addressCommand.Parameters.AddWithValue("@City", pers.PersonCity)
addressCommand.Parameters.AddWithValue("@state", pers.PersonState)
addressCommand.Parameters.AddWithValue("@zip", pers.PersonZipcode)
addressCommand.Parameters.Add("@PersonKey", SqlDbType.Int)
'insert into contact for home phone
Dim homePhoneSql As String = "Insert into PersonContact(ContactInfo, personKey, contactTypeKey)Values(@ContactInfo, @personKey, 1)"
Dim homephoneCommand As New SqlCommand(homePhoneSql, connect)
homephoneCommand.Parameters.AddWithValue("@ContactInfo", pers.PersonHomePhone)
homephoneCommand.Parameters.Add("@PersonKey", SqlDbType.Int)
'inserts into contact for email
Dim emailsql As String = "Insert into PersonContact(ContactInfo, personKey, contactTypeKey)Values(@ContactInfo, @personKey, 6)"
Dim emailCommand As New SqlCommand(emailsql, connect)
emailCommand.Parameters.AddWithValue("@ContactInfo", pers.PersonEmail)
emailCommand.Parameters.Add("@PersonKey", SqlDbType.Int)
'inserts into donation
Dim donationSql As String = "insert into donation(donationdate, donationamount,Personkey)Values(@date, @amount, @PersonKey)"
Dim donationCommand As New SqlCommand(donationSql, connect)
donationCommand.Parameters.AddWithValue("@Date", don.DonationDate)
donationCommand.Parameters.AddWithValue("@Amount", don.DonationAmount)
donationCommand.Parameters.Add("@PersonKey", SqlDbType.Int)
'now that everything is set up we will open
'the connection to the database.
'if there are any problems with the connection string
'the error will fall here
connect.Open()
Dim pk As Integer 'to store the personKey
'the transaction object is used to group
'all the statements into one thing
'either all of them happen or none
'of them happen
Dim tran As SqlTransaction
tran = connect.BeginTransaction
'here we assign the new transaction
'to all the sqlcommands
personCommand.Transaction = tran
personKeyCommand.Transaction = tran
addressCommand.Transaction = tran
homephoneCommand.Transaction = tran
emailCommand.Transaction = tran
donationCommand.Transaction = tran
'try the following code and catch any errors
Try
'first we insert the person
'ExecuteNonQuery is for any command
'that doesn't return results
personCommand.ExecuteNonQuery()
'now we get the person key. ExecuteScaler
'is used for queries that will return only
'a single value. the result an integer
'so we cast it CInt to an integer
pk = CInt(personKeyCommand.ExecuteScalar)
'Now we assign the value of personkey
'to the parameters of the
'various commands
addressCommand.Parameters("@PersonKey").Value = pk
homephoneCommand.Parameters("@PersonKey").Value = pk
emailCommand.Parameters("@PersonKey").Value = pk
donationCommand.Parameters("@PersonKey").Value = pk
'Now we execute each of the remaining commands
addressCommand.ExecuteNonQuery()
homephoneCommand.ExecuteNonQuery()
emailCommand.ExecuteNonQuery()
donationCommand.ExecuteNonQuery()
'if we don't have an error by now
'commit them all
tran.Commit()
Catch ex As Exception
'if there is an error undo it all
tran.Rollback()
'throw the error back to the form
Throw ex
Finally
'no matter what, close the connections
connect.Close()
End Try
End Sub
End Class
It all works on my machine
The ADO Example Fix
Speak of brain farts. . . I figured out why the code we did in class didn't work and why it didn't throw any errors. We forgot to call the method that actually writes the data to the database. I added a call to the method in the constructor of the DataLayer class. Here is the code for the constructor now.
Public Sub New(ByRef p As Person, ByRef d As Donation)
pers = p
don = d
connect = New SqlConnection("Data source=.\sqlexpress;initial catalog=communityAssist;integrated security=true;")
'we forgot to call the method that actually writes the data
writeDonation()
End Sub
I will post the rest of the code before the weekend is over.
Public Sub New(ByRef p As Person, ByRef d As Donation)
pers = p
don = d
connect = New SqlConnection("Data source=.\sqlexpress;initial catalog=communityAssist;integrated security=true;")
'we forgot to call the method that actually writes the data
writeDonation()
End Sub
I will post the rest of the code before the weekend is over.
Tuesday, February 2, 2010
VB Classes
Here is what we did with the form. We created a table with text fields and a button. We will use this eventually to insert data and populate the People and Donation classes:
<%@ 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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>Enter your information</p>
<asp:Table ID="Table1" runat="server" Width="248px">
<asp:TableRow>
<asp:TableCell>Last Name</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>First Name</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Address</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>City</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>State</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtState" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Zip code</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtZipcode" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Home Phone</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtHomePhone" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Email</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Donation Amount</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtDonation" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell> </asp:TableCell>
<asp:TableCell>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</div>
</form>
</body>
</html>
Now here is the Person class. It consists soley of private fields and public properties. Its purpose it to store the data for a new person:
Imports Microsoft.VisualBasic
Public Class Person
Private firstName As String
Private lastName As String
Private address As String
Private city As String
Private state As String
Private zipcode As String
Private homePhone As String
Private email As String
Private PKey As Integer
Public Property PersonFirstName() As String
Get
Return FirstName
End Get
Set(ByVal value As String)
FirstName = value
End Set
End Property
Public Property PersonLastName() As String
Get
Return LastName
End Get
Set(ByVal value As String)
LastName = value
End Set
End Property
Public Property PersonAddress() As String
Get
Return address
End Get
Set(ByVal value As String)
address = value
End Set
End Property
Public Property PersonCity() As String
Get
Return city
End Get
Set(ByVal value As String)
city = value
End Set
End Property
Public Property PersonState() As String
Get
Return state
End Get
Set(ByVal value As String)
If value.Length > 2 Then
Dim ex As New Exception("use two letters for a state")
Throw ex
Else
state = value
End If
End Set
End Property
Public Property PersonZipcode() As String
Get
Return zipcode
End Get
Set(ByVal value As String)
zipcode = value
End Set
End Property
Public Property PersonHomePhone() As String
Get
Return homePhone
End Get
Set(ByVal value As String)
homePhone = value
End Set
End Property
Public Property PersonEmail() As String
Get
Return email
End Get
Set(ByVal value As String)
email = value
End Set
End Property
Public Property PersonKey() As Integer
Get
Return PKey
End Get
Set(ByVal value As Integer)
PKey = value
End Set
End Property
End Class
Now we have the Donation Class:
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Public Class Donation
Private amount As Double
Private donDate As Date
Private Pkey As Integer
Public Property DonationAmount() As Double
Get
Return amount
End Get
Set(ByVal value As Double)
amount = value
End Set
End Property
Public Property DonationDate() As Date
Get
Return donDate
End Get
Set(ByVal value As Date)
donDate = value
End Set
End Property
Public Property DonationPersonKey() As Integer
Get
Return Pkey
End Get
Set(ByVal value As Integer)
Pkey = value
End Set
End Property
End Class
Finally we have just the beginnings of the DataLayer Class. All we have done so far is import the Data and Data.SqlClient namespaces, added to object fields to recieve a person and a donation, and created a constructor with which to pass them to the class
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Data
Imports System.Data.SqlClient
Public Class DataLayer
Private pers As Object
Private don As Object
Public Sub New(ByRef p As Person, ByRef d As Donation)
pers = p
don = d
End Sub
End Class
<%@ 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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>Enter your information</p>
<asp:Table ID="Table1" runat="server" Width="248px">
<asp:TableRow>
<asp:TableCell>Last Name</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>First Name</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Address</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>City</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>State</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtState" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Zip code</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtZipcode" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Home Phone</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtHomePhone" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Email</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Donation Amount</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtDonation" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell> </asp:TableCell>
<asp:TableCell>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</div>
</form>
</body>
</html>
Now here is the Person class. It consists soley of private fields and public properties. Its purpose it to store the data for a new person:
Imports Microsoft.VisualBasic
Public Class Person
Private firstName As String
Private lastName As String
Private address As String
Private city As String
Private state As String
Private zipcode As String
Private homePhone As String
Private email As String
Private PKey As Integer
Public Property PersonFirstName() As String
Get
Return FirstName
End Get
Set(ByVal value As String)
FirstName = value
End Set
End Property
Public Property PersonLastName() As String
Get
Return LastName
End Get
Set(ByVal value As String)
LastName = value
End Set
End Property
Public Property PersonAddress() As String
Get
Return address
End Get
Set(ByVal value As String)
address = value
End Set
End Property
Public Property PersonCity() As String
Get
Return city
End Get
Set(ByVal value As String)
city = value
End Set
End Property
Public Property PersonState() As String
Get
Return state
End Get
Set(ByVal value As String)
If value.Length > 2 Then
Dim ex As New Exception("use two letters for a state")
Throw ex
Else
state = value
End If
End Set
End Property
Public Property PersonZipcode() As String
Get
Return zipcode
End Get
Set(ByVal value As String)
zipcode = value
End Set
End Property
Public Property PersonHomePhone() As String
Get
Return homePhone
End Get
Set(ByVal value As String)
homePhone = value
End Set
End Property
Public Property PersonEmail() As String
Get
Return email
End Get
Set(ByVal value As String)
email = value
End Set
End Property
Public Property PersonKey() As Integer
Get
Return PKey
End Get
Set(ByVal value As Integer)
PKey = value
End Set
End Property
End Class
Now we have the Donation Class:
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Public Class Donation
Private amount As Double
Private donDate As Date
Private Pkey As Integer
Public Property DonationAmount() As Double
Get
Return amount
End Get
Set(ByVal value As Double)
amount = value
End Set
End Property
Public Property DonationDate() As Date
Get
Return donDate
End Get
Set(ByVal value As Date)
donDate = value
End Set
End Property
Public Property DonationPersonKey() As Integer
Get
Return Pkey
End Get
Set(ByVal value As Integer)
Pkey = value
End Set
End Property
End Class
Finally we have just the beginnings of the DataLayer Class. All we have done so far is import the Data and Data.SqlClient namespaces, added to object fields to recieve a person and a donation, and created a constructor with which to pass them to the class
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Data
Imports System.Data.SqlClient
Public Class DataLayer
Private pers As Object
Private don As Object
Public Sub New(ByRef p As Person, ByRef d As Donation)
pers = p
don = d
End Sub
End Class
Tuesday, January 26, 2010
LINQ Examples
Here is the code from the class. Rember, for any of this to work, you must use the server Explorer window, create a connection to Community Assist Database. Add a new LINQ to SQL Classes object to your project and drag the Service, Person and PersonGrant tables into the designer.
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim db As New CAssistDataContext
Dim caService = (From s In db.Services _
Order By s.ServiceName _
Select s.ServiceKey, s.ServiceName).Distinct
DropDownList1.DataSource = caService
DropDownList1.DataTextField = "ServiceName"
DropDownList1.DataValueField = "ServiceKey"
DropDownList1.DataBind()
End If
End Sub
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
Dim db As New CAssistDataContext
Dim caGrant = From g In db.ServiceGrants _
Order By g.GrantDate _
Where g.ServiceKey = DropDownList1.SelectedValue _
Select g.Person.LastName, g.Person.FirstName, g.GrantDate, g.GrantAmount
DataList1.DataSource = caGrant
DataList1.DataBind()
End Sub
End Class
<%@ 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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
</asp:DropDownList>
</div>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<hr />
<p>
<asp:Label ID="lblLastname" runat="server" Text='<%#Eval("LastName") %>'></asp:Label>,
<asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
<br />
<asp:Label ID="lblDate" runat="server" Text='<%#Eval("GrantDate") %>'></asp:Label>
<br />
<asp:Label ID="lblGrantAmount" runat="server" Text='<%#Eval("GrantAmount") %>'></asp:Label>
</p>
<hr />
</ItemTemplate>
</asp:DataList>
</form>
</body>
</html>
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim db As New CAssistDataContext
Dim caService = (From s In db.Services _
Order By s.ServiceName _
Select s.ServiceKey, s.ServiceName).Distinct
DropDownList1.DataSource = caService
DropDownList1.DataTextField = "ServiceName"
DropDownList1.DataValueField = "ServiceKey"
DropDownList1.DataBind()
End If
End Sub
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
Dim db As New CAssistDataContext
Dim caGrant = From g In db.ServiceGrants _
Order By g.GrantDate _
Where g.ServiceKey = DropDownList1.SelectedValue _
Select g.Person.LastName, g.Person.FirstName, g.GrantDate, g.GrantAmount
DataList1.DataSource = caGrant
DataList1.DataBind()
End Sub
End Class
<%@ 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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
</asp:DropDownList>
</div>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<hr />
<p>
<asp:Label ID="lblLastname" runat="server" Text='<%#Eval("LastName") %>'></asp:Label>,
<asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
<br />
<asp:Label ID="lblDate" runat="server" Text='<%#Eval("GrantDate") %>'></asp:Label>
<br />
<asp:Label ID="lblGrantAmount" runat="server" Text='<%#Eval("GrantAmount") %>'></asp:Label>
</p>
<hr />
</ItemTemplate>
</asp:DataList>
</form>
</body>
</html>
Thursday, January 21, 2010
Watermarks
If anyone has completed the watermark extra credit assignment and wants to share how they didi it, I would appreciate it. I haven't had time to test teh book's code. I know that you have to provide a different prefix--something other than "asp"-- to the ajax extension controls.
If you post the asp, ajax, or xhtml code in the blog you must escape all < brackets with &lt; Otherwise the blog will attempt to process the contents.
If you post the asp, ajax, or xhtml code in the blog you must escape all < brackets with &lt; Otherwise the blog will attempt to process the contents.
Tuesday, January 19, 2010
Ajax Samples 2
Here is the code we did for class today. It has two Ajax Update Panels, one that does the tip calculator and one that reveals or hides a panel with a table in it. Remember to add a Script manager, and remember that all the controls in an Update Panel must be inside a control template element.
<%@ 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 id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Tip Calculator</h1>
<p>
</p>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<p> Enter the Meal AmountEnter the Meal AmountEnter the Meal Amount
<asp:TextBox ID="TxtMeal" runat="server"></asp:TextBox>
</p>
<asp:Label ID="Label2" runat="server" Text="Choose the tip Percent"></asp:Label>
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
</asp:RadioButtonList>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
<br />
<asp:Label ID="Label3" runat="server" Text="Your tip will be"></asp:Label>
<asp:Label ID="lbltip" runat="server" Text=""></asp:Label> <br />
<asp:Label ID="Label4" runat="server" Text="Your total will be"></asp:Label>
<asp:Label ID="lbltotal" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:RadioButtonList ID="RadioButtonList2" runat="server" AutoPostBack="True"
onselectedindexchanged="Unnamed1_SelectedIndexChanged">
<asp:ListItem Text="Show" Value="1"></asp:ListItem>
<asp:ListItem Text="Hide" Value="0"></asp:ListItem>
</asp:RadioButtonList>
<asp:Panel ID="Panel1" runat="server" BackColor="Beige" Visible="False">
<asp:Table ID="Table1" runat="server">
<asp:TableRow>
<asp:TableCell>FirstName</asp:TableCell>
<asp:TableCell><asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>LastName</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox></asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
Option Explicit On
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Create an array of percents
If Not IsPostBack Then
Dim percents() As String = {"10 Percent", "15 Percent", "20 Percent"}
RadioButtonList1.DataSource = percents
RadioButtonList1.DataBind()
End If
'Dim x As Integer
End Sub
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim mealAmount As Double
If txtMeal.Text = Nothing Then
Response.Write("Enter Something")
Return
End If
'If IsNumeric(txtMeal.Text) Then
' mealAmount = Double.Parse(txtMeal.Text)
'Else
' lbltip.Text = "please enter a valid numeric amount for the meal"
' Return
'End If
Dim testResult As Boolean
testResult = Double.TryParse(txtMeal.Text, mealAmount)
If testResult = False Then
lbltip.Text = "please enter a valid numeric amount for the meal"
lbltotal.Text = ""
Return
End If
Dim tip As Double
Dim total As Double
If RadioButtonList1.SelectedItem.Text = "10 Percent" Then
tip = mealAmount * 0.1
ElseIf RadioButtonList1.SelectedItem.Text = "15 Percent" Then
tip = mealAmount * 0.15
Else
tip = mealAmount * 0.2
End If
total = mealAmount + tip
lbltip.Text = tip.ToString("$#,##0.00")
lbltotal.Text = total.ToString("$#,##0.00")
End Sub
Protected Sub Unnamed1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
If RadioButtonList2.SelectedValue = 1 Then
Panel1.Visible = True
Else
Panel1.Visible = False
End If
End Sub
End Class
<%@ 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 id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Tip Calculator</h1>
<p>
</p>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<p> Enter the Meal AmountEnter the Meal AmountEnter the Meal Amount
<asp:TextBox ID="TxtMeal" runat="server"></asp:TextBox>
</p>
<asp:Label ID="Label2" runat="server" Text="Choose the tip Percent"></asp:Label>
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
</asp:RadioButtonList>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
<br />
<asp:Label ID="Label3" runat="server" Text="Your tip will be"></asp:Label>
<asp:Label ID="lbltip" runat="server" Text=""></asp:Label> <br />
<asp:Label ID="Label4" runat="server" Text="Your total will be"></asp:Label>
<asp:Label ID="lbltotal" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:RadioButtonList ID="RadioButtonList2" runat="server" AutoPostBack="True"
onselectedindexchanged="Unnamed1_SelectedIndexChanged">
<asp:ListItem Text="Show" Value="1"></asp:ListItem>
<asp:ListItem Text="Hide" Value="0"></asp:ListItem>
</asp:RadioButtonList>
<asp:Panel ID="Panel1" runat="server" BackColor="Beige" Visible="False">
<asp:Table ID="Table1" runat="server">
<asp:TableRow>
<asp:TableCell>FirstName</asp:TableCell>
<asp:TableCell><asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>LastName</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox></asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
Option Explicit On
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Create an array of percents
If Not IsPostBack Then
Dim percents() As String = {"10 Percent", "15 Percent", "20 Percent"}
RadioButtonList1.DataSource = percents
RadioButtonList1.DataBind()
End If
'Dim x As Integer
End Sub
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim mealAmount As Double
If txtMeal.Text = Nothing Then
Response.Write("Enter Something")
Return
End If
'If IsNumeric(txtMeal.Text) Then
' mealAmount = Double.Parse(txtMeal.Text)
'Else
' lbltip.Text = "please enter a valid numeric amount for the meal"
' Return
'End If
Dim testResult As Boolean
testResult = Double.TryParse(txtMeal.Text, mealAmount)
If testResult = False Then
lbltip.Text = "please enter a valid numeric amount for the meal"
lbltotal.Text = ""
Return
End If
Dim tip As Double
Dim total As Double
If RadioButtonList1.SelectedItem.Text = "10 Percent" Then
tip = mealAmount * 0.1
ElseIf RadioButtonList1.SelectedItem.Text = "15 Percent" Then
tip = mealAmount * 0.15
Else
tip = mealAmount * 0.2
End If
total = mealAmount + tip
lbltip.Text = tip.ToString("$#,##0.00")
lbltotal.Text = total.ToString("$#,##0.00")
End Sub
Protected Sub Unnamed1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
If RadioButtonList2.SelectedValue = 1 Then
Panel1.Visible = True
Else
Panel1.Visible = False
End If
End Sub
End Class
Friday, January 15, 2010
Class sample code from Jan, 14
First we looked at how to check that the input was numeric. You can use one of two methods. The first is based on an old VB function called "IsNumeric." The second uses the TryParse method of the Double type.
Here is an example of the first way:
Dim mealAmount As Double
If IsNumeric(txtMeal.Text) then
mealAmount = Double.Parse(txtMeal.Text)
Else
lblError.Text="Please Enter the meal as a numeric amount"
Return
End If
Here is an example of the second way. If the text in the text box parses, the value
is assigned to the mealAmount variable
Dim mealAmount as double
Dim result as Boolean
Result=Double.TryParse(txtMeal.Text, mealAmount)
If result=False then
lblError.Text="Please Enter the meal as a numeric amount"
Return
End If
Next we covered simple Ajax in ASP. The first thing you must do is add an scriptmanager object to your web page.
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
Next you add an Update Panel. Inside the Update panel you put all the controls that you want to post back separately from the main web page. We put in a panel control (not another update panel, just a simple panel that is used to contain other controls. We did it so that we could make the controls invisible and then make them visible with a button click.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<!--this is a button to make the panel visible-->
<asp:Button ID="Button3" runat="server" Text="Show time?" />
<asp:Panel ID="Panel1" runat="server" BackColor="BlanchedAlmond" Visible="false">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:Button ID="Button2" runat="server" Text="Get Time" />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
I left out the calendar and the other page elements to keep this simple. Here is the code for the two buttons:
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Label1.Text = TimeOfDay().ToShortTimeString
End Sub
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
Panel1.Visible =True
End Sub
Have a good long weekend
Here is an example of the first way:
Dim mealAmount As Double
If IsNumeric(txtMeal.Text) then
mealAmount = Double.Parse(txtMeal.Text)
Else
lblError.Text="Please Enter the meal as a numeric amount"
Return
End If
Here is an example of the second way. If the text in the text box parses, the value
is assigned to the mealAmount variable
Dim mealAmount as double
Dim result as Boolean
Result=Double.TryParse(txtMeal.Text, mealAmount)
If result=False then
lblError.Text="Please Enter the meal as a numeric amount"
Return
End If
Next we covered simple Ajax in ASP. The first thing you must do is add an scriptmanager object to your web page.
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
Next you add an Update Panel. Inside the Update panel you put all the controls that you want to post back separately from the main web page. We put in a panel control (not another update panel, just a simple panel that is used to contain other controls. We did it so that we could make the controls invisible and then make them visible with a button click.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<!--this is a button to make the panel visible-->
<asp:Button ID="Button3" runat="server" Text="Show time?" />
<asp:Panel ID="Panel1" runat="server" BackColor="BlanchedAlmond" Visible="false">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:Button ID="Button2" runat="server" Text="Get Time" />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
I left out the calendar and the other page elements to keep this simple. Here is the code for the two buttons:
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Label1.Text = TimeOfDay().ToShortTimeString
End Sub
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
Panel1.Visible =True
End Sub
Have a good long weekend
Subscribe to:
Posts (Atom)