Inserting rows from DataTable into SQL Server database table
Posted on 2008-10-16
I am building a web site in ASP.NET with VB.NET on MS Visual Web Developer .NET. After an online customers add products to the shopping cart (I'm using a DataTable) and is going through the Checkout process, I need help to insert the details from each row of the datatable into the Orders_detail table of my SQL Server 2005 database.
I start off with Page_Load event:
If Not IsNothing(Session("Cart")) Then
objDT = Session("Cart")
dg.DataSource = objDT
I try to go through each datarow:
For intCounter = 0 To objDT.Rows.Count - 1
objDR = objDT.Rows(intCounter)
strSKU = objDR("SKU")
But that throws an error.
And once I set each variable to the values in each field of the datatable, I insert it into the database:
Dim AddOrdersDetail As String = "Insert into orders_detail (id, sku, product_name, price, qty) Values ('ShoppingCartNr','sku','product_name','price','qty')"
Dim Cmd7 As New SqlCommand(AddOrdersDetail, MyConn)