?
Solved

Datatable does not retain existing rows after postback.

Posted on 2008-10-22
3
Medium Priority
?
704 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 48

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 48

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

621 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