Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello world !
i'm developing a shopping cart in asp net.
Products : Db
Shopping Cart : Data Table
Order : Db (but order is not important now)

The connections string work fine, the only problem is this :
If i populate the data table with a insert into sql command, it work on db and i don't want it.
I want that the sql command write only on the data table and don't modify the Database.

What I have tried:

This is the code :

VB
If Not Me.IsPostBack Then
           Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
           Using con As New SqlConnection(constr)

               '

                   Using cmd As New SqlCommand("INSERT INTO Carrello (ID,Nome,PrezzoFloat, Quantità) SELECT Id,NomeProdotto, PrezzoProdotto, Quantità FROM aProdotti WHERE ID='2'")
                   cmd.Connection = con
                   Using sda As New SqlDataAdapter(cmd)
                       Dim dt As New DataTable()
                       sda.Fill(dt)
                       GridView1.DataSource = dt
                       GridView1.DataBind()
                   End Using
               End Using
           End Using


       End If
Posted
Updated 4-May-17 1:04am

An INSERT query will insert data to the database.

You want to SELECT data.
using cmd As New SqlCommand("SELECT Id,NomeProdotto, PrezzoProdotto, Quantità FROM aProdotti WHERE ID='2'")
Also check the data type of the Column ID - if you want it to be a number then use a numeric type not a varchar. If it already a numeric type then lose the single quotes around the 2
 
Share this answer
 
Comments
EmanuelPirovano 4-May-17 8:07am    
Thank you man.
So if i include a session, can it works for a shopping cart ?
CHill60 4-May-17 8:11am    
I don't understand the question in your comment, however there are several CodeProject Articles[^] on Shopping Carts that may help
EmanuelPirovano 4-May-17 8:15am    
Let me try explain in a better way.
On a click "buy", i select an item from db to data table, after if i buy another item they replace the existent item in data table, how can i deal add multiple item in data table with select command ?
CHill60 4-May-17 8:21am    
Don't specify just one specific ID ... you can use an IN clause or OR clauses to get more. I suggest you read some of those articles in the link I gave.
EmanuelPirovano 4-May-17 8:42am    
Probably , i have gaps in Sql .
I' ve already read some tutorial but i can't unserstand, where i would place IN/OR ? What is the correct sql function ?

Anyway thanks for the help.
Your datatable with not going to fill wirh INSERT statement.

You need to pass select command to fill your datatable with values from database

You statement should
SQL
Select Column1,Column2.... from TableName


I hope it is clear to you.
 
Share this answer
 
Comments
EmanuelPirovano 4-May-17 8:07am    
Thank you very much !!
Now i will try.
Nirav Prabtani 4-May-17 8:48am    
welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900