Solved

Datatable does not retain existing rows after postback.

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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:…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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