Datagrid - update to Database

When i try to update the Database from the datagrid, i get an error of TableMappings.
How can i update my Database?

Imports System.Data.OleDb
Imports System.Data
Public Class TestAssignments
Dim bk As New dbase("tests")
Dim info As New dbase("setinfo")
Dim i, j As Integer
Dim con As New OleDbConnection(str)
Dim cmd As OleDbCommand
Dim dr() As DataRow
Dim p As Integer
Dim dt As New DataTable
Dim dv As New DataGridView
Dim ds As New DataSet
Dim adp As OleDbDataAdapter
Dim testsamounts As String
Public sqlString As String
Public grouping As String = "b"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
Private Sub TestAssignments_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.Open()
dt = New DataTable
adp = New OleDbDataAdapter("select amounttests from setinfo", con)
adp.Fill(dt)
Dim testsamounts As String = dt.Rows(0).Item(0).ToString
Label3.Text = testsamounts
fillgrid(testsamounts, grouping)
End Sub
Sub fillgrid(ByVal amounts As Integer, ByVal grouping As String)
Dim tests As New DataSet
DataGridView1.Refresh()
dt = New DataTable
grouping = Trim(InputBox(" Please type the Group name :"))
If amounts = 1 Then
sqlString = "select studentname, test1 from tests where tests.group <> '" & grouping & "'"
adp = New OleDbDataAdapter(sqlString, con)
End If
If amounts = 2 Then
sqlString = "select studentname, test1 , test2 from tests where tests.group <> '" & grouping & "'"
adp = New OleDbDataAdapter(sqlString, con)
End If
If amounts = 3 Then
sqlString = "select studentname, test1 , test2, test3 from tests where tests.group <> '" & grouping & "'"
adp = New OleDbDataAdapter(sqlString, con)
End If
If amounts = 4 Then
sqlString = "select studentname, test1 , test2, test3, test4 from tests where tests.group <> '" & grouping & "'"
adp = New OleDbDataAdapter(sqlString, con)
End If
If amounts = 5 Then
sqlString = "select studentname, test1 , test2, test3, test4, test5 from tests where tests.group <> '" & grouping & "'"
adp = New OleDbDataAdapter(sqlString, con)
End If
If amounts = 6 Then
sqlString = "select studentname, test1 , test2, test3, test4, test5, test6 from tests where tests.group <> '" & grouping & "'"
adp = New OleDbDataAdapter(sqlString, con)
End If
If amounts = 7 Then
sqlString = "select studentname, test1 , test2, test3, test4, test5, test6, test7 from tests where tests.group <> '" & grouping & "'"
adp = New OleDbDataAdapter(sqlString, con)
End If
If amounts = 8 Then
sqlString = "select studentname, test1 , test2, test3, test4, test5, test6, test7, test8 from tests where tests.group <> '" & grouping & "'"
adp = New OleDbDataAdapter(sqlString, con)
End If
If amounts = 9 Then
sqlString = "select studentname, test1 , test2, test3, test4, test5, test6, test7, test8, test9 from tests where tests.group <> '" & grouping & "' "
adp = New OleDbDataAdapter(sqlString, con)
End If
If amounts >= 10 Then
sqlString = "select studentname, test1 , test2, test3, test4, test5, test6, test7, test8, test9,test10 from tests where tests.group <> '" & grouping & "'"
adp = New OleDbDataAdapter(sqlString, con)
End If
adp.Fill(tests)
adp.Fill(dt)
DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Blue
DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.White
DataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.Sunken
DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
DataGridView1.DataSource = dt
End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim command_builder As New OleDbCommandBuilder
con.Open()
adp.TableMappings.Add("test1", "tests")
command_builder = New OleDb.OleDbCommandBuilder(adp)
adp.Update(ds, "tests")
End Sub
End Class

Comments

  • seancampbellseancampbell Pennsylvania, USA
    Where is this specifically crashing. And in your fillGrid function are you able to verify that everything sucked out of the database OK?

    So after
    adp.Fill(tests)
    adp.Fill(dt)
    are you able to see columns and rows of data in the dt and tests objects?
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion