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
Solved

Add row in datatable to session in asp.net

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
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 …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

856 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