Sửa lỗi the dataadapter select command property needs tobe initialized năm 2024

First things first, get rid of the line that's throwing the exception. There is absolutely no point calling GetInsertCommand and assigning the result manually to the DataAdapter's InsertCommand if you aren't going to make any changes to it. If you're not going to make any changes to the command, e.g. enlist it in a transaction, then you should simply call Update on the DataAdapter and let the rest happen automatically.

That said, the CommandBuilder is not magic. It can't generate an INSERT statement with no knowledge of the table and columns being inserted into. In order to use a CommandBuilder with a DataAdapter, the DataAdapter's SelectCommand must contain a SQL SELECT statement. Only by your specifying where the data has originally come from can the CommandBuilder know where the new data is going to.

'Inline Code Example HereHello i'm having this error message when adding to multiple table: '"The DataAdapter.SelectCommand property needs to be initialized." 'I need to have my data saved into both table(tblStudentInfo, tblPayment) when btnFinish clicked. Please 'help, i'm working on this for days and haven't had any luck.

'For reference i attached codings below (frmMainMenu_load, btnFinish):

Public Class frmMainMenu
    Dim connetionString As String
    Dim connection As OleDbConnection
    Dim oledbAdapter As OleDbDataAdapter
    Dim ds As New DataSet
    Dim firstSql As String
    Dim secondSql As String
    Dim MaxRows As Integer = 8
    Dim subject As String
    Dim TotalSubject As Integer
    Private Sub frmMainMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DTS_Management.mdb;"
        firstSql = "SELECT * From tblStudentInfo"
        secondSql = "SELECT * From tblPayment"
        connection = New OleDbConnection(connetionString)
        Try
            connection.Open()
            oledbAdapter = New OleDbDataAdapter(firstSql, connection)
            oledbAdapter.Fill(ds, "First Table")
            oledbAdapter.SelectCommand.CommandText = secondSql
            oledbAdapter.Fill(ds, "Second Table")
            oledbAdapter.Dispose()
            connection.Close()
            'JUST TO CHECK IF CONNECTION ARE MADE
            ''retrieve first table data
            'For i = 0 To ds.Tables(0).Rows.Count - 1
            '    MsgBox(ds.Tables(0).Rows(i).Item(0) & "  --  " & ds.Tables(0).Rows(i).Item(1))
            'Next
            ''retrieve second table data
            'For i = 0 To ds.Tables(1).Rows.Count - 1
            '    MsgBox(ds.Tables(1).Rows(i).Item(0) & "  --  " & ds.Tables(1).Rows(i).Item(1))
            'Next
        Catch ex As Exception
            MsgBox("Can not open connection ! ")
        End Try
    End Sub
    Private Sub btnFinish_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFinish.Click
        Dim autono As New Random 'Variables for unique id_no
        Dim cb As New OleDb.OleDbCommandBuilder(oledbAdapter)
        Dim dsNewRow As DataRow
        dsNewRow = ds.Tables(0).NewRow()
        dsNewRow.Item(0) = autono.Next(1000, 9999) 'Input unique ID in D
        '-Student Record Tab
        dsNewRow.Item(1) = Studentnametxt.Text
        dsNewRow.Item(2) = IcNotxt.Text
        dsNewRow.Item(3) = Sexcb.Text
        dsNewRow.Item(4) = Format(BirthdatePicker.Value, "dd/MMM/yyyy")
        dsNewRow.Item(5) = Nationalitytxt.Text
        dsNewRow.Item(6) = TextBox2.Text 'email
        dsNewRow.Item(7) = Studentcontactno.Text
        dsNewRow.Item(8) = Addresstxt.Text
        dsNewRow.Item(9) = Guardiannametxt.Text
        dsNewRow.Item(10) = Guardiancontacttxt.Text
        dsNewRow.Item(11) = Guardian_Name2.Text
        dsNewRow.Item(12) = Guardian_Contact2.Text
        dsNewRow.Item(13) = StudentPicturetxt.Text
        dsNewRow.Item(14) = Elementarytxt.Text
        dsNewRow.Item(15) = Eleyeartxt.Text
        dsNewRow.Item(16) = CurrentSchooltxt.Text
        dsNewRow.Item(17) = cbCurrentYear.Text
        'start/end course
        dsNewRow.Item(18) = StartDate1.Text
        dsNewRow.Item(19) = DateStart2.Text
        dsNewRow.Item(20) = DateStart3.Text
        dsNewRow.Item(21) = End1.Text
        dsNewRow.Item(22) = End2.Text
        dsNewRow.Item(23) = End3.Text
        'This is to prevent from error
        If subject = "" Then
            subject = "nil"
        End If
        dsNewRow.Item(24) = subject
        ds.Tables(0).Rows.Add(dsNewRow)
        oledbAdapter.Update(ds, "First Table")
        MsgBox("Record added.")
    End Sub

This is what I was following, so either it is wrong or I have something in the wrong place. It doesn't suggest creating a command other than the CommandBuilder which I did .... so either the SQL statement I provided is wrong or I've done something else to muck it up.

All I wanna do is add some records and save it back to the DB.

When I googled this error I was given a lot of C# examples. That doesn't help me in this situation.


  • Jun 24th, 2020, 08:15 PM

    Re: The SelectCommand property has not been initialized before calling 'Fill'.

    You create a command object like this, Code:
        Dim da As OleDb.OleDbDataAdapter  
        da = New OleDb.OleDbDataAdapter  
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("Select * from crops")  
        con.Open()  
        da.SelectCommand = cmd  
        da.SelectCommand.Connection = con  
        Dim dt As New DataTable  
        da.Fill(dt)  
        MessageBox.Show(dt.Rows.Count.ToString)  
    
    If your following the tutorial then you don't need to create a new DataAdapter or DataTable because you already created them. So I'm not sure why your doing all this, the example was pretty straight forward.