I have an empty table in a database that has default column headings (Field1, Field2.....).
I would like to change these headings programmatically by downloading the table to a dataset, changing the column captions in the dataset, then updating the database table from the dataset. (It's part of a course excercise).
My code seems to work OK - the database table fills the dataset and the column captions are changed in the dataset. However, after the database table is updated no changes occur to the column headings in the database.
I get no errors either.
Appreciate it if someone could point me in the right direction.
Following is my code. The database is Assign9.mdb (Access) and the database table is "StudentGrade".
I would like to change these headings programmatically by downloading the table to a dataset, changing the column captions in the dataset, then updating the database table from the dataset. (It's part of a course excercise).
My code seems to work OK - the database table fills the dataset and the column captions are changed in the dataset. However, after the database table is updated no changes occur to the column headings in the database.
I get no errors either.
Appreciate it if someone could point me in the right direction.
Following is my code. The database is Assign9.mdb (Access) and the database table is "StudentGrade".
Code:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
'Connect to database Assign9.mdb.
Dim con As New OleDb.OleDbConnection
con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Assign9.mdb"
con.Open()
'Fill dataset with records from database table StudentGrades.
Dim ds As New DataSet("Student")
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
sql = "SELECT * FROM StudentGrade"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "Student")
'Create array to hold label text.
Dim dsItem(7) As String
dsItem(1) = Label1.Text
dsItem(2) = Label2.Text
dsItem(3) = Label3.Text
dsItem(4) = Label4.Text
dsItem(5) = Label5.Text
dsItem(6) = Label6.Text
'Change column captions.
Dim dc(6) As DataColumn
Dim i As Integer
For i = 1 To 6
ds.Tables(0).Columns(i).Caption = dsItem(i)
Next
'Update database.
Dim cb As New OleDb.OleDbCommandBuilder(da)
da.Update(ds, "Student")
con.Close()
End Sub
End Class