Solved

Datatable does not retain existing rows after postback.

Posted on 2008-10-22
3
701 Views
Last Modified: 2013-11-08
I am trying to add datarows to a datatable programatically and bind it to a gridview. I created a class to create the datatable and add rows to it. I am able to initialize the table and add a row to it. When I try to add another row, after postback, the existing rows disappear but the new ones stay. Am I losing the contents of my datatable on postback?

The reason I am not committing the rows into a database is because I want to make a bulk insert at the end of the "wizard" I am developing.

Thanks so much.
'this is a sub to call add a datarow in the "clsProjectContacts" class
    Private Sub addrow()
        Dim stuff As New clsProjectContacts
        stuff.AddRow("No", "Consultant", "Spinitar", "Bob Builder", "My notes")
 
        Me.gridContacts.DataSource = stuff.ContactTable
        Me.gridContacts.DataBind()
    End Sub
 
'below is code snippet for the "clsProjectContacts" for creating the datatable and rows for project contacts
 
    Public Sub AddRow(ByVal strContract As String, _
                      ByVal strType As String, _
                      ByVal strAccount As String, _
                      ByVal strContact As String, _
                      ByVal strNotes As String)
        r = tbl.NewRow
        r.Item("Contract") = strContract
        r.Item("Type") = strType
        r.Item("Account") = strAccount
        r.Item("Contact") = strContact
        r.Item("Notes") = strNotes
        tbl.Rows.Add(r)
        tbl.AcceptChanges()
 
    End Sub
 
    Public Function MakeContactTable() As DataTable
        ' Create a new DataTable titled 'Names.'
        Dim namesTable As DataTable = New DataTable("Contacts")
 
        ' Add five column objects to the table.
        Dim idColumn As DataColumn = New DataColumn()
        idColumn.DataType = System.Type.GetType("System.Int32")
        idColumn.ColumnName = "id"
        idColumn.AutoIncrement = True
        namesTable.Columns.Add(idColumn)
 
        Dim ContractColumn As DataColumn = New DataColumn()
        ContractColumn.DataType = System.Type.GetType("System.String")
        ContractColumn.ColumnName = "Contract"
        namesTable.Columns.Add(ContractColumn)
 
        Dim TypeColumn As DataColumn = New DataColumn()
        TypeColumn.DataType = System.Type.GetType("System.String")
        TypeColumn.ColumnName = "Type"
        namesTable.Columns.Add(TypeColumn)
 
        Dim AccountColumn As DataColumn = New DataColumn()
        AccountColumn.DataType = System.Type.GetType("System.String")
        AccountColumn.ColumnName = "Account"
        namesTable.Columns.Add(AccountColumn)
 
        Dim ContactColumn As DataColumn = New DataColumn()
        ContactColumn.DataType = System.Type.GetType("System.String")
        ContactColumn.ColumnName = "Contact"
        namesTable.Columns.Add(ContactColumn)
 
        Dim NotesColumn As DataColumn = New DataColumn()
        NotesColumn.DataType = System.Type.GetType("System.String")
        NotesColumn.ColumnName = "Notes"
        namesTable.Columns.Add(NotesColumn)
 
        ' Create an array for DataColumn objects.
        Dim keys(0) As DataColumn
        keys(0) = idColumn
        namesTable.PrimaryKey = keys
 
        ' Return the new DataTable.
        MakeContactTable = namesTable
    End Function

Open in new window

0
Comment
Question by:ervine13
[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
3 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 22783678
Every time you run this code....

    Private Sub addrow()
        Dim stuff As New clsProjectContacts
        stuff.AddRow("No", "Consultant", "Spinitar", "Bob Builder", "My notes")
 
        Me.gridContacts.DataSource = stuff.ContactTable
        Me.gridContacts.DataBind()
    End Sub

...it creates a new DataTable. So in essence, the existing rows don't "disappear", they don't exist in the current table.

To fix, you need to keep your clsProjectContacts object global, which is done by declaring it at class level (at the top of your form's class). Then do this in the "addrow" routine....

    Private stuff As clsProjectContacts

    Private Sub addrow()
        If stuff Is Nothing Then stuff = New clsProjectContacts
        stuff.AddRow("No", "Consultant", "Spinitar", "Bob Builder", "My notes")
        Me.gridContacts.DataSource = stuff.ContactTable
        Me.gridContacts.DataBind()
    End Sub

Wayne
0
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 250 total points
ID: 22783720
Sorry, what I posted above won't work in ASP.Net. You'll need to store your object in a Session variable instead....

    Private Sub addrow()
        Dim stuff As clsProjectContacts = Session("stuff")
        If stuff Is Nothing Then
            stuff = New clsProjectContacts
            Session("stuff") = stuff
        End If
        stuff.AddRow("No", "Consultant", "Spinitar", "Bob Builder", "My notes")
        Me.gridContacts.DataSource = stuff.ContactTable
        Me.gridContacts.DataBind()
    End Sub
0
 

Author Closing Comment

by:ervine13
ID: 31509080
Thank you sooooo much. I wanted to try it as soon as you responded but was not able. Copied your code and it worked like a champ! Thanks so much!
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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