Writting a details from a shopping cart purchase to an access database

kmh987

New member
Joined
Jun 1, 2007
Messages
1
Programming Experience
3-5
I currently have a check out asp.net web page which has a 3 step wizard step: Step 1: collect the shipping details eg name, address, phone number. Step 2: collects the credit card info, step 3: displays the total price and asks to submit order. When this is clicked it calls a method in a Class i have made called OrderDB (displayed below). The method is meant to begin by using the WebConfigurationManager class to retrieve the database connection string from web.config. Then it creates and opens a connection and obtains a transaction that can be used to coordinate the update. However when i run the code below i get an error seen below. Can anyone help me with this please?

System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object."
Source="App_Code.p6kwedhm"
StackTrace:
at OrderDB.WriteOrder(Order o) in C:\Users\Kieran\Documents\Uni\ITB007\Assignment - Website\Assignment\App_Code\OrderDB.vb:line 10
at CheckOut.Wizard1_FinishButtonClick(Object sender, WizardNavigationEventArgs e) in C:\Users\Kieran\Documents\Uni\ITB007\Assignment - Website\Assignment\CheckOut.aspx.vb:line 24
at System.Web.UI.WebControls.Wizard.OnFinishButtonClick(WizardNavigationEventArgs e)
at System.Web.UI.WebControls.Wizard.OnBubbleEvent(Object source, EventArgs e)
at System.Web.UI.WebControls.Wizard.WizardChildTable.OnBubbleEvent(Object source, EventArgs args)
at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
at System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

VB.NET:
Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Imports System.Web.Configuration

Public Class OrderDB
    Shared tran As SqlTransaction
    Shared con As SqlConnection
    Public Shared Function WriteOrder(ByVal o As Order) As Boolean
        Dim cs As String
        cs = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
        con = New SqlConnection(cs)
        con.Open()
        tran = con.BeginTransaction()
        Try
            InsertCustomer(o.Cust)
            Dim oNum As Integer
            oNum = InsertOrder(o)
            For Each item As CartItem _
            In o.Cart.GetItems()
                InsertItem(item, oNum)
            Next
            tran.Commit()
            con.Close()
            Return True
        Catch ex As Exception
            tran.Rollback()
            Return False
        End Try
    End Function

    Private Shared Sub InsertCustomer(ByVal cust As Customer)
        Dim cmd As New SqlCommand()
        cmd.Connection = con
        cmd.Transaction = tran
        Try
            cmd.CommandText = "INSERT INTO User " _
            + "(lastname, firstname, " _
            + "address, city, state, zipcode," _
            + "phone, email) " _
            + "VALUES (@LastName, @FirstName, " _
            + "@Address, @City, @ZipCode," _
            + "@PhoneNumber, @Email)"
            cmd.Parameters.AddWithValue( _
            "@LastName", cust.LastName)
            cmd.Parameters.AddWithValue( _
            "@FirstName", cust.FirstName)
            cmd.Parameters.AddWithValue( _
            "@Address", cust.Address)
            cmd.Parameters.AddWithValue( _
            "@City", cust.City)
            cmd.Parameters.AddWithValue( _
            "@ZipCode", cust.PostCode)
            cmd.Parameters.AddWithValue( _
            "@PhoneNumber", cust.PhoneNumber)
            cmd.Parameters.AddWithValue( _
            "@Email", cust.Email)
            cmd.ExecuteNonQuery()
        Catch ex As SqlException
            If ex.Number = 2627 Then 'Duplicate Key
                cmd.CommandText = "UPDATE Customers " _
                + "SET lastname = @LastName, " _
                + "firstname = @FirstName, " _
                + "address = @Address, " _
                + "city = @City, " _
                + "state = @State, " _
                + "zipcode = @ZipCode, " _
                + "phone = @PhoneNumber " _
                + "WHERE email = @Email "
                cmd.ExecuteNonQuery()

            Else
                Throw ex
            End If
        End Try
    End Sub
    Private Shared Function InsertOrder(ByVal o As Order) As Integer
        Dim cmd As New SqlCommand()
        cmd.Connection = con
        cmd.Transaction = tran
        cmd.CommandText = "INSERT INTO Purchases " _
        + "(orderdate, custemail, " _
        + "subtotal, salestax, " _
        + "shipping) " _
        + "VALUES (@OrderDate, @Custemail)"
        cmd.Parameters.AddWithValue( _
        "@OrderDate", DateTime.Now)
        cmd.Parameters.AddWithValue( _
        "@Custemail", o.Cust.Email)
        cmd.ExecuteNonQuery()
        cmd.CommandText = "SELECT @@IDENTITY"
        Return Convert.ToInt32(cmd.ExecuteScalar())
    End Function
    Private Shared Sub InsertItem(ByVal item As CartItem, ByVal oNum As Integer)
        Dim cmd As New SqlCommand()
        cmd.Connection = con
        cmd.Transaction = tran
        cmd.CommandText = "INSERT INTO OrderItems " _
        + "(ordernum, productid, " _
        + "name, price, quantity) " _
        + "VALUES (@OrderNum, @ProductID, " _
        + "@Name, @Price, @Quantity)"
        cmd.Parameters.AddWithValue( _
        "@OrderNum", oNum)
        cmd.Parameters.AddWithValue( _
        "@ProductID", item.ID)
        cmd.Parameters.AddWithValue( _
        "@Name", item.Name)
        cmd.Parameters.AddWithValue( _
        "@Price", item.Price)
        cmd.Parameters.AddWithValue( _
        "@Quantity", item.Quantity)
        cmd.ExecuteNonQuery()
    End Sub
End Class
 
In future please ask ASP.NET questions in the ASP.NET area of the forum. Do not post this question again in the correct section, instead PM a moderator (Neal, JohnH) to ask for this post to be moved
 
Back
Top