title: asp.net grid view and dataList
body: <p>I need help with a part of my website I'm coding. I have a listBox_Products which is populated with sqlServerDataSource. When I click on a product, it will display a corresponding picture along a gridview with productPrice and productName. The gridview is databound programmatically in the codebehind, on the indexChange event. I have a button, addToShoppingCart. On the button_Click event, I want to add the item the user chose to the shopping cart (it's on the same page), I have a second gridView, which received the item the user chose and displays it. This is where I'm stuck. I know I can't append to gridview, and I know it has to be databound. My logic is this: get the first item user chooses, add it to a datatable, insert into second gridview. Now if the user chooses another product, same logic, except this time, I would add a new row, and add the new data to the new row, keeping old row as well. Problem is I can't figure out how to do this. I'm not very experienced in datatable. </p>
  
  <p>This is my code behind.</p>
  
  <p>This is my code to get the product from sql server:</p>
  
  <pre><code>Private Sub GetProducts()
  
      Dim TechShopConnectionString As String = ConfigurationManager.ConnectionStrings("Zapata_IT_DataBaseConnectionString").ConnectionString
      Dim TechCon As New SqlConnection(TechShopConnectionString)
      Dim TechCmd As New SqlCommand()
  
      Dim index As Integer = ListBox_Products.SelectedIndex()
  
      TechCmd.CommandType = CommandType.StoredProcedure
      TechCmd.CommandText = "GetAllProductInformationByID"
      TechCmd.Parameters.Add("@ProductID", SqlDbType.Int).Value = index
      TechCmd.Connection = TechCon
  
      Try
          TechCon.Open()
          GridView2.EmptyDataText = "No Records Found"
          GridView2.DataSource = TechCmd.ExecuteReader()
          GridView2.DataBind()
  
      Catch ex As Exception
          Throw ex
      Finally
          TechCon.Close()
          TechCon.Dispose()
      End Try
  End Sub
  </code></pre>
  
  <p>This is my code to add item to the second gridview:</p>
  
  <pre><code>Protected Sub Button_AddToCart_Click(sender As Object, e As EventArgs) Handles Button_AddToCart.Click
      Dim conn As SqlConnection = Nothing
      Dim index As Integer = ListBox_Products.SelectedIndex()
  
      Try
          Dim connString As String = ConfigurationManager.ConnectionStrings("Zapata_IT_DataBaseConnectionString").ConnectionString
          conn = New SqlConnection(connString)
          Dim cmd As SqlCommand = New SqlCommand()
  
          cmd.CommandType = CommandType.StoredProcedure
          cmd.CommandText = "GetAllProductInformationByID"
          cmd.Parameters.Add("@ProductID", SqlDbType.Int).Value = index
          cmd.Connection = conn
  
          conn.Open()
          Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
          Dim dr2 As DataRow
  
          dataTableCheckOut.NewRow()
          dataTableCheckOut.Load(dr, LoadOption.OverwriteChanges)
  
          GridView_CheckOut.DataSource = dataTableCheckOut
          GridView_CheckOut.DataBind()
  
      Catch ex As SqlException
  
      Catch ex As Exception
  
      Finally
          conn.Close()
      End Try
  
      ' Enter code here
  </code></pre>
  
  <p>As you can see I'm pretty lost. I know the logic is fine, but I cant figure out the code.</p>
  
  <p>Any help would be appreciated.</p>