Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

Add row in datatable to session in asp.net

I am trying to add items into datatable which I want to store datatable in session. It keeps displaying only the last item I entered.
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

     
        If Page.IsPostBack = False Then

            Session("CartID") = 1


        End If


        GridView1.DataSource = Session("dt")
        GridView1.DataBind()


    End Sub  

  Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

       AddProduct()

    End Sub



Dim dt As New DataTable()
  Session("ID") += 1

    Protected Sub AddProduct()

     

        dt.Columns.AddRange(New DataColumn(1) {New DataColumn("ID"), New DataColumn("Product"), New DataColumn("Quantity")})


        If ASPxGridLookupProductDescription.Value <> "" Then

            
         
            dt.Rows.Add(Session("ID"), txtboxproduct.text, txtboxquantity.text)


            Session("dt") = dt
            GridView1.DataSource = TryCast(Session("dt"), DataTable)
            GridView1.DataBind()


        End If



    End Sub



<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowEditing="OnRowEditing">
<Columns>
    <asp:BoundField DataField="Product" HeaderText="Name" ItemStyle-Width="150" />
    <asp:BoundField DataField="Quantity" HeaderText="Country" ItemStyle-Width="150" />
    <asp:TemplateField>
        <ItemTemplate>
            <asp:LinkButton Text="Edit" runat="server" CommandName="Edit" />
        </ItemTemplate>
        <EditItemTemplate>
            <asp:LinkButton Text="Update" runat="server" OnClick="OnUpdate" />
            <asp:LinkButton Text="Cancel" runat="server" OnClick="OnCancel" />
        </EditItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>

Open in new window

0
jagr12
Asked:
jagr12
1 Solution
 
Lokesh B RDeveloperCommented:
Hi,

Add this code and try

Private dt As DataTable

Protected Sub AddProduct()

	Session("ID") = Convert.ToInt32(Session("ID")) + 1

	If Session("dt") IsNot Nothing Then
		dt = DirectCast(Session("dt"), DataTable)
	Else
		dt = New DataTable()

		dt.Columns.AddRange(New DataColumn(1) {New DataColumn("ID"), New DataColumn("Product"), New DataColumn("Quantity")})
	End If



	If Not String.IsNullOrEmpty(ASPxGridLookupProductDescription.Value) Then


		dt.Rows.Add(Session("ID"), txtboxproduct.text, txtboxquantity.text)


		Session("dt") = dt
		GridView1.DataSource = dt


		GridView1.DataBind()
	End If

End Sub

Open in new window


Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

     
        If Page.IsPostBack = False Then

            Session("CartID") = 1


        End If


        GridView1.DataSource = Session("dt")
        GridView1.DataBind()


    End Sub  

  Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

       AddProduct()

    End Sub

Open in new window

0
 
jagr12Author Commented:
Thank you very much Lokesh.  It works !!!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now