Solved

Datatable does not retain existing rows after postback.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now