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

No comments:

Post a Comment