Solved

Datatable does not retain existing rows after postback.

Posted on 2008-10-22
3
698 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
  • 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

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 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