Viewstate bug/issue

Hi, I have the code as below that I use for a shopping cart. It basically gets data from text boxes and binds it to a Repeater.

    Private Sub addline()
        'add lines to datatable
        If ViewState("lines") Is Nothing Then
            Dim dt As New DataTable("lines")

            dt.Columns.Add("quantity", GetType(String))
            dt.Columns.Add("partno", GetType(String))
            dt.Columns.Add("description", GetType(String))
            dt.Columns.Add("cost", GetType(String))
            dt.Columns.Add("ltotal", GetType(String))

            If Cache("lines") Is Nothing Then
                ViewState("lines") = dt
            Else
                ViewState("lines") = Cache("lines")
            End If

            dt.Rows.Add(New String(4) {TextBox2.Text, TextBox3.Text, TextBox4.Text, String.Format("{0:n2}", Convert.ToDecimal(TextBox5.Text)), String.Format("{0:n2}", (Convert.ToDecimal(TextBox5.Text) * Convert.ToDecimal(TextBox2.Text)))})
            DirectCast(ViewState("lines"), DataTable).Merge(dt)
            Cache("lines") = ViewState("lines")
        Else
            Dim dt As DataTable = DirectCast(ViewState("lines"), DataTable)

            dt.Rows.Add(New String(4) {TextBox2.Text, TextBox3.Text, TextBox4.Text, String.Format("{0:n2}", Convert.ToDecimal(TextBox5.Text)), String.Format("{0:n2}", (Convert.ToDecimal(TextBox5.Text) * Convert.ToDecimal(TextBox2.Text)))})
            DirectCast(ViewState("lines"), DataTable).Merge(dt)
            Cache("lines") = ViewState("lines")
        End If

        'bind lines to repeater
        Repeater1.DataSource = DirectCast(ViewState("lines"), DataTable)
        Repeater1.DataBind()
    End Sub

Open in new window


Every now and then when a user adds a line - for some odd reason it'll add items that are being entered/have been entered by a different user on a different machine. This is very concerning so just checking if there is something wrong with my code or if I have to add extra lines?
LVL 1
David CIT Projects ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

inxniCommented:
The problem should be Cache. Unlike viewstate or session, cache is global so it's possible to get other users' information if you get it from cache.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David CIT Projects ManagerAuthor Commented:
Thank you - I have changed Cache("lines") to Session("lines")
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.