Groups > Asp .Net > Data Access and ObjectDataSource Control > Re: Database From Codebehind Help Please




Database From Codebehind Help Please

Database From Codebehind Help Please
Thu, 3 Apr 2008 01:15:21 +0000
Hello, Could any one please tell me why this code is not working? The delete
function works fine but the select into statement isn't.

This is part of a shopping cart I'm trying to write. On the last page when the
order is submitted, I'm trying to move the items in the shopping cart to the
Ordered Items table and then remove the items from the shopping cart table. It
deletes the items from the cart fine but does not move them to the other table.
I used the select into statement but may be that is not how its done. Could
someone give me some suggestions? Thank you so much. 

 

Protected Sub FormViewOrders_ItemInserted(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.FormViewInsertedEventArgs)

Dim str_ConnectionString As String =
WebConfigurationManager.ConnectionStrings("MyDatabaseConnectionString"
).ConnectionString.ToString()
Dim conn_SqlConnection As New SqlConnection(str_ConnectionString)
        conn_SqlConnection.Open()

Dim str_SqlQueryString1 As String = "SELECT Cat2ShopCart.BasketID,
Cat2ShopCart.cat2ID, Cat2ShopCart.OrderDate, Cat2ShopCart.ProductTitle,
Cat2ShopCart.ProductSalePrice, Cat2ShopCart.ProductQty INTO Cat2OrderItems FROM
Cat2ShopCart CROSS JOIN Cat2OrderItems AS Cat2OrderItems_1 WHERE [BasketID] =
'" & Session("ShoppingCartID") & "'"
        Dim cmd_SqlCommand1 = New SqlCommand(str_SqlQueryString1,
conn_SqlConnection)
        
        Dim str_SqlQueryString2 As String = "Delete FROM [Cat2ShopCart]
WHERE [BasketID] = '" & Session("ShoppingCartID") &
"'"Dim cmd_SqlCommand2 = New SqlCommand(str_SqlQueryString2,
conn_SqlConnection)

        conn_SqlConnection.Close()

        Response.Redirect("Checkout-3.aspx")
End Sub
Post Reply
Re: Database From Codebehind Help Please
Thu, 3 Apr 2008 01:49:24 +0000
SELECT INTO will create a new table. If you want to copy items from one table to
another, use INSERT INTO... SELECT...
Post Reply
Re: Database From Codebehind Help Please
Thu, 3 Apr 2008 01:57:49 +0000
could you be a little more specific, i'm not sure what you mean. thanks.
Post Reply
Re: Database From Codebehind Help Please
Thu, 3 Apr 2008 21:04:43 +0000
No problem.

Select Into

SELECT Field1, Field2, Field3 INTO Table2 FROM Table1 Where Field1 = 'value'

This statement will try to create Table2 with the fields you've selected, based
on the schema of Table1. If Table2 already exists, you will get an error.

Insert

INSERT INTO Table2 (
   Field1,
   Field2,
   Field3
)
SELECT Field1, Field2, Field3
FROM Table1
WHERE Field1 = 'value'

This statement will insert rows from Table1 into Table2, and assumes Table2
already exists (or you will get an error).
Post Reply
about | contact