Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a button on a form. The form also contains 8 text boxes. Once the text boxes have been filled out; the button is selected to save the data in a datatable called SetUpTable.
If the button is pressed again to prevent a error occurring, I have put in an If statement to check and see if the datatable contains a row. (There should only be 1 row maximum) However the code I am using reads 0 rows irrespective of whether the datatable is new with no rows or already has 1 row. So what am I doing wrong?

In debug mode, I can see that the datatable contains one row when I press the button the second time but the expression
VB
SetupTable.Rows.Count
states the row count to be 0.

What I have tried:

VB
Public SetupTable As DataTable = New DataTable()

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Title As String = "SetUp"
        Dim rowws As Integer
        rowws = SetupTable.Rows.Count  'Used to measure the row count
        If SetupTable.Rows.Count > 0 Then 'if true then change the data in row 1
            SetupTable.Rows(0).Item(0) = WFileDir.Text
            SetupTable.Rows(0).Item(1) = YBFileDir.Text
            SetupTable.Rows(0).Item(2) = MFileDir.Text
            SetupTable.Rows(0).Item(3) = Port.Text
            SetupTable.Rows(0).Item(4) = Cred.Text
            SetupTable.Rows(0).Item(5) = Host.Text
            SetupTable.Rows(0).Item(6) = EmailFrom.Text
            SetupTable.Rows(0).Item(7) = EmailTo.Text
        Else     'if false then create a new row of data
            SetupTable.Columns.AddRange(New DataColumn() {
            New DataColumn(WFileDir.Text, GetType(String)),
            New DataColumn(YBFileDir.Text, GetType(String)),
            New DataColumn(MFileDir.Text, GetType(String)),
            New DataColumn(Port.Text, GetType(String)),
            New DataColumn(Cred.Text, GetType(String)),
            New DataColumn(Host.Text, GetType(String)),
            New DataColumn(EmailFrom.Text, GetType(String)),
            New DataColumn(EmailTo.Text, GetType(String))})

        End If
Posted

1 solution

You don't actually insert a row - you create columns.

Insert this line at the end of your Else-block (after creating the columns):
VB
SetupTable.Rows.Add(SetupTable.NewRow)
 
Share this answer
 
Comments
Dave the Golfer 8-Feb-16 10:27am    
Thanks, I have been scratching my head for sometime now. Works beautifully
Sascha Lefèvre 8-Feb-16 10:32am    
You're welcome! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900