Download presentation
Presentation is loading. Please wait.
Published byBrice McBride Modified over 9 years ago
1
ASP.NET - insert - delete -update DataTables (disconnected datasets) Shopping Basket
2
Insert Using the INSERT SQL statement with data hard-wired into it Using the INSERT SQL statement with placeholder variables to be filled by user input. – Parameters.Add used to fill placeholders
3
Insert using SQL command Open connection to database as usual, then: Dim dothis As OleDbCommand Dim strInsert As String strInsert = "INSERT INTO Company (CompanyName, City) VALUES ('Blue Bird', 'Leeds')" dothis= New OleDbCommand(strInsert, objConn) dothis.ExecuteNonQuery
4
Insert using Add method of OleDbCommand Dim dothis As OleDbCommand Dim strInsert As String strInsert= "INSERT INTO Company (CompanyName, City) VALUES (@Company, @City)" dothis= New OleDbCommand(strInsert, objConn) dothis.Parameters.Add("@Company", txtCompany.Text) dothis.Parameters.Add("@City", txtCity.Text) Dothis.ExecuteNonQuery
5
Delete SQL DELETE command SQL DELETE with Parameters.Add
6
Delete using SQL Dim dothis As OleDbCommand Dim strDelete As String strDelete = “DELETE FROM” &;_ “Company WHERE CompanyName = ‘Blue Bird’” dothis= New OleDbCommand(strDelete, objConn) dothis.ExecuteNonQuery
7
Delete using Add method of OleDbCommand strDelete = “DELETE FROM Company WHERE CompanyName = @CompanyName” dothis= New OleDbCommand(strDelete, objConn) dothis.Parameters.Add(“@CompanyName”, txtCompanyName.Text) dothis.ExecuteNonQuery
8
Update Executing an SQL statement with the UPDATE command against the database. SQL UPDATE with Parameters.Add
9
Update using SQL strUpdate = “UPDATE JobList SET EstimatedSalary = EstimatedSalary * 1.10 WHERE JobTitle = ‘Network Administrators’” dothis= New OleDbCommand(strUpdate, objConn) dothis.ExecuteNonQuery
10
Update using Add method of OleDbCommand strUpdate = “Update JobList SET EstimatedSalary = @EstimatedSalary WHERE JobTitle = @JobTitle” dothis= New OleDbCommand(strUpdate, objConn) dothis.Parameters.Add(“@EstimatedSalary”, txtEstimatedSalary.Text) dothis.Parameters.Add(“@JobTitle”, txtJobTitle.Text) dothis.ExecuteNonQuery
11
DataTables Table stored in server memory Disconnected DataSet Used for – Manipulating subsets of data – Shopping basket
12
Defining a datatable 1 – define columns Dim objDT As System.Data.DataTable objDT = New System.Data.DataTable("Cart") objDT.Columns.Add("Product", GetType(String)) objDT.Columns.Add("Quantity", GetType(Integer)) objDT.Columns.Add("Cost", GetType(Decimal))
13
Data Table - Header Row Product Quantity Cost After step 1
14
Step 2 Adding a new row Dim objDR As System.Data.DataRow objDR = objDT.NewRow Part A – create a new blank row that copies the table
15
After Step 2a Data Table (objDT)- Header Row Product Quantity Cost DataRow (objDR)
16
2b – fill the new row objDR("Quantity") = txtQuantity.Text objDR("Product") = ddlProducts.SelectedItem.Text objDR("Cost") = Decimal.Parse(ddlProducts.SelectedItem.Value)
17
After step 2b Data Table (objDT)- Header Row Product Quantity Cost DataRow (objDR) Something some number some price
18
Step 3 – put the new row in the table objDT.Rows.Add(objDR)
19
After step 3 Data Table (objDT) Product Quantity Cost Something some number some price Header Row Row 1
20
Shopping Basket User not logged in* – Session – Datatable stored in memory User logged in – Session – Datatable to & from database * This version for coursework
21
Shopping Basket Initialise basket 1.Create a datatable 2.Store it in memory (Session) Session(“cart”)=objDT
22
Shopping basket (cont 2) When “add to cart” clicked 1.Retrieve the datatable from memory 2.Create a new row 3.Fill in the data 4.Put the new row on the bottom of the datatable basket = Session(“cart”)
23
Shopping basket (cont 3) View Basket 1.Retrieve table from memory 2.Bind table to DataGrid BasketGrid.DataSource()=Session(“cart”)
24
TO DO Oasisplus – ASP.NET : INSERT, DELETE, UPDATE – ASP.NET : DataList – Shopping basket example code on discussion board – Or start your coursework NEXT WEEK – SHOPPING BASKET Where we take items from a list retrieved from the database
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.