none
datatable in session RRS feed

  • Question

  • Hi everyone

    uh..I have two questions about inserting data into database..but it is not simply about sql statement

    I created a "datatable" stored in session()...which this data_table contains columns...fields....so that it can store data from another table...

    for example:

    Cart = New DataTable()
                Cart.Columns.Add(New DataColumn("Item", GetType(String)))
                Cart.Columns.Add(New DataColumn("Price", GetType(Decimal)))
                Cart.Columns.Add(New DataColumn("Quantity", GetType(Integer)))
                Cart.Columns.Add(New DataColumn("ProductID", GetType(Integer)))
                Session("ShoppingCart") = Cart

    and,
    (1)
    I created a DataGrid to read this DataTable In Session...

    The question is I want to insert the data retrieved from "Session" which is displayed in "DataGrid" into SQL DataBase...besides...there is a table created in sql database which contains the same Fields name...


    (2)
    and...there is another way I tried, First..I created a Gridview to read table from sql database which contains the same fileds as the Datatable in session

    and I tried the  databinding "SqlDataSource" which every time you create a Gridview you can see a "SqlDataSource" below the GridView..........anyway, the properties of SqlDataSource contains one functionality called"Insert Query".....Which I can choose parameters to pass the values...So I chose the parameter source attached to Session....

    But the problem is this Session is a DataTable...containing Data Field..So I dont know how to Pass this Session which could specificlly pass each relevent Field I need....


    Sorry about this length........

    I am reaaaaalllly looking for help....................cheers....

    Monday, December 3, 2007 5:44 AM

Answers

All replies

  • And If I do not have to attach the data to sql database...just to display them in Datagrid...


    I Wrote the coding below..

        Dim Cart As DataTable
        Dim CartView As DataView
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            If Session("ShoppingCart") Is Nothing Then
                Cart = New DataTable()
                Cart.Columns.Add(New DataColumn("Item", GetType(String)))
                Cart.Columns.Add(New DataColumn("Price", GetType(Decimal)))
                Cart.Columns.Add(New DataColumn("Quantity", GetType(Integer)))
                Cart.Columns.Add(New DataColumn("ProductID", GetType(Integer)))
                Session("ShoppingCart") = Cart
            Else
                Cart = CType(Session("ShoppingCart"), DataTable)       
            End If           
            CartView = New DataView(Cart)
            ShoppingCart.DataSource = CartView
            ShoppingCart.DataBind()   
        End Sub 
        Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim row As GridViewRow = GridView1.Rows(index) ' index functionality
            Dim dr As Data.DataRow = Cart.NewRow()
            Dim IDcell As TableCell = row.Cells(0)
            Dim itemCell As TableCell = row.Cells(1)
            Dim PriceCell As TableCell = row.Cells(2)
            Dim qtyCell As TableCell = row.Cells(3)
            Dim item As String = itemCell.Text
            Dim Price As String = PriceCell.Text
            Dim Qty As String = qtyCell.Text
            Dim ID As Integer = IDcell.Text
            If e.CommandName = "AddToCart" Then
                dr(0) = item
                dr(1) = Price
                dr(2) = Qty
                dr(3) = ID
                Cart.Rows.Add(dr)
            End If
            ShoppingCart.DataBind()
        End Sub


    But Now,...I have to insert the data into  database after retrieved from Session.....the proble session is a dataTable not just one textbox...or something..I can simply queote to....
    I do not
    Monday, December 3, 2007 5:55 AM
  • I noticed you are trying to write a shopping cart with two tables that is not simple, you need to start with existing sample code and add what you need.  If your site is small you can use the sample code using HashTable, if you site is big then you need at least four tables and start with the sample that comes with the Commerce starter kit.  The problem is it is not easy to keep track of your users in session.  Hope this helps.


    http://www.codeproject.com/KB/applications/ShoppingCartCSharp.aspx?df=100&forumid=38314&exp=0&select=1329129

     

    http://www.asp.net/downloads/archived/starter-kits/commerce/

     

    Tuesday, December 4, 2007 6:27 PM