The DataAdapter.SelectCommand property needs to be initialized.

aarontham
aarontham used Ask the Experts™
on
i use VB.net 2008 and Firebird. below is my code.
the code fire by button. and first fire is ok. and the follow by second fire then i got this error (The DataAdapter.SelectCommand property needs to be initialized.)

If MsgBox("Do you want save the changes? ", vbYesNo, "Aaron's routine...") = vbYes Then
            Me.Cursor = Cursors.AppStarting

            Using connection As New FbConnection(cnString)
                connection.Open()

                Dim builder As FbCommandBuilder = New FbCommandBuilder(DACom)
                Me.Validate()
                Me.DACom.Update(DTCom)
                Me.DTCom.AcceptChanges()
                builder.Dispose()
                connection.Close()
                connection.Dispose()
                MessageBox.Show("Successfully Saved")

            End Using

        End If
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Expert of the Quarter 2010
Expert of the Year 2010

Commented:
What other code do you have that interacts with Me.DACom?
It looks like it is being cleared/reinitialised/unset somewhere after the first run.

Author

Commented:
I use it to fill a datagridview. below is the code.

    Public DTCom As New DataTable
    Public DACom As New FbDataAdapter


 Dim StrQuery As String = "SELECT * from DBSetting"
            DACom = New FbDataAdapter(StrQuery, cnString)
            DTCom = New DataTable
            DACom.Fill(DTCom)
            DGVSelectDB.DataSource = DTCom
Expert of the Quarter 2010
Expert of the Year 2010

Commented:
I cannot make from the fragments what goes into which event handler.
Is there a single event handler that contains a reference to DACom, and is the declaration including strquery local?

Author

Commented:
HI cyberkiwi,

plsfind attach is my full code.
pls help

thank you

Author

Commented:
HI cyberkiwi,

plsfind below is my full code.
pls help

thank you




Public Class frmDBSetting
    Dim style1 As New System.Windows.Forms.DataGridViewCellStyle
    Dim curDir As String = "c:\ESystem\Data"
    Dim curFile As String = curDir & "\MEConfig.FDB"
    Public cnString As String = "UserID=sysdba;Password=masterkey;Database=localhost:" & curFile & ";DataSource=localhost;Charset=NONE"
    Public DTCom As New DataTable
    Public DACom As New FbDataAdapter



 Public Sub LoadDBSetting()
        Try

            Dim StrQuery As String = "SELECT * from DBSetting"
            DACom = New FbDataAdapter(StrQuery, cnString)
            DTCom = New DataTable
            DACom.Fill(DTCom)
            DGVSelectDB.DataSource = DTCom
            With DGVSelectDB
                ' .Columns.Clear()
                .AllowUserToResizeRows = False
                .AlternatingRowsDefaultCellStyle.BackColor = style1.BackColor
                .SelectionMode = DataGridViewSelectionMode.FullRowSelect
                .RowTemplate.Height = 18
                .Columns("DBSet_ID").Width = 50
                .Columns("Company_Name").Width = 170
                .Columns("Database_Name").Width = 170
                .Columns("Database_Server").Width = 170
                .Columns("Database_Server_Port").Width = 170
                .Columns("Database_Create_Date").Width = 170
                ' .Columns("Send").Visible = False
            End With


            Me.Cursor = Cursors.Default

        Catch exError As Exception

            MessageBox.Show(exError.Message, _
            Me.Text, _
            MessageBoxButtons.OK, _
            MessageBoxIcon.Error)

            Me.Cursor = Cursors.Default
        End Try
    End Sub


 Private Sub BtnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUpdate.Click
        ' Try

        If MsgBox("Do you want save the changes? ", vbYesNo, "Aaron's routine...") = vbYes Then
            Me.Cursor = Cursors.AppStarting

            Using connection As New FbConnection(cnString)
                connection.Open()

                Dim builder As FbCommandBuilder = New FbCommandBuilder(DACom)

                Me.DACom.Update(DTCom)
                Me.DTCom.AcceptChanges()
                builder.Dispose()
                connection.Close()
                connection.Dispose()
                MessageBox.Show("Successfully Saved")

            End Using

        End If

        LoginForm1.LoadDBDVG()

        ' Catch exError As Exception

        ' MessageBox.Show(exError.Message, _
        ' Me.Text, _
        ' MessageBoxButtons.OK, _
        ' MessageBoxIcon.Error)

        'Me.Cursor = Cursors.Default
        'Finally

        'End Try
        Me.Cursor = Cursors.Default
    End Sub
Expert of the Quarter 2010
Expert of the Year 2010
Commented:
I think you might do better recreating DACom as a local.

Using connection As New FbConnection(cnString)
                connection.Open()

                Dim DACom As FbDataAdapter = new FbDataAdapter()
                DACom.SelectCommand = new FbCommand("SELECT * from DBSetting", connection)

                ' uses the select statement to build the update/delete statements
                Dim builder As FbCommandBuilder = New FbCommandBuilder(DACom)

                DACom.Update(DTCom)
                Me.DTCom.AcceptChanges()
                builder.Dispose()
                connection.Close()
                connection.Dispose()
                MessageBox.Show("Successfully Saved")

            End Using

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial