Solved

Add row in datatable to session in asp.net

Posted on 2016-09-06
2
67 Views
Last Modified: 2016-09-07
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
Comment
Question by:jagr12
2 Comments
 
LVL 14

Accepted Solution

by:
Lokesh B R earned 500 total points
ID: 41787275
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
 

Author Comment

by:jagr12
ID: 41788204
Thank you very much Lokesh.  It works !!!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now