Solved

Add row in datatable to session in asp.net

Posted on 2016-09-06
2
88 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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.

832 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