Solved

Add row in datatable to session in asp.net

Posted on 2016-09-06
2
104 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

735 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