MaylorTaylor
MaylorTaylor

Reputation: 5041

Adding rows to a datatable with Auto Generated field

Firstly, I have already scoured the interwebs to find the solution but nothing seemed to be exactly right.

I have a DataGrid that is displaying information from my database. I am trying to add records to the datagrid and also the database. The below code is what i have so far for the 'add' button.

Dim TaxConnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ConfigurationManager.AppSettings("Database")
    Dim dbConnection As OleDbConnection = New OleDbConnection(TaxConnStr)
    Dim dt As New DataTable
    Dim ds As New DataSet

    Try

        dt.Rows.Add(New String() {
                                            boxAssignTo.Text, _
                                            boxState.Text, _
                                            boxCounty.Text, _
                                            boxAmount.Text, _
                                            boxType.Text, _
                                            boxRank.Text})


    Catch ex As Exception
    Finally
        dbConnection.Close()
    End Try

Right now I am having the error "Input array is longer than the number of columns in this table." show during this process. I have surmised that it is because in the table on the database I have an autogenerated ID Field that I am not adding to. But i'm clueless on how to add this in.

Dont know if this matters, but when i display the DataGrid i have

DataGridView1.Columns.Remove("ID")

So i don't physically see the ID field (kinda pointless on this winForm).

Be gentle, i'm very new to databases and VB

Upvotes: 0

Views: 768

Answers (1)

jason
jason

Reputation: 3615

The reason you are getting this error is because you have not added any columns:

    Try
        dt.Columns.Add("a")
        dt.Columns.Add("b")
        dt.Columns.Add("c")
        dt.Columns.Add("d")
        dt.Columns.Add("e")
        dt.Columns.Add("f")

        dt.Rows.Add(New String() {"1","2","3","4","5","6"})


    Catch ex As Exception
    Finally

    End Try

Upvotes: 1

Related Questions