0

Good day,

I have been trying to figure out the source of error in my application. What this function (GetConcentrations()) is to display data from my database onto a DataGridView. The function is called in the PageLoad but works fine but when I call it in another Sub, I get this error.

Could anyone help me? I already tried different ways.

Thanks!

enter image description here

Here is the code:

Private Sub GetConcentrations()
    Dim conString As String = ConfigurationManager.ConnectionStrings("dbAsthmaConnectionString").ConnectionString
    Me.dataAdapter = New SqlDataAdapter("Select * from tblConcentrations", conString)
    'Dim adapter As New SqlDataAdapter("Select * from tblConcentrations", conString)
    Dim dataset As New Data.DataSet

    Try
        Dim commandBuilder As New SqlCommandBuilder(Me.dataAdapter)

        Dim table As New DataTable()
        table.Locale = System.Globalization.CultureInfo.InvariantCulture
        Me.dataAdapter.Fill(table)
        'Dim datasetgetconcentrations = New DataSet
        Me.dataAdapter.Fill(dataset)
        Me.bindingSource1.DataSource = table

        DataGridView3.AutoResizeColumns(DataGridViewAutoSizeColumnMode.AllCellsExceptHeader)

        DataGridView3.DataSource = dataset.Tables(0)

        dataset.Dispose()

    Catch ex As Exception
        MsgBox("Failed to display the concentration table (GetConcentrations)!", MsgBoxStyle.Information)
    End Try

    'Dim ConcentrationValue As Double = DataGridView2.Rows.Contains
    'TextBoxCurrentConcentration.Text = txtMessage.Text
End Sub
4

1 に答える 1

0

なぜFillメソッドを2回呼び出すのかわかりません。
1回はDataTableに入力し、もう1回はDataSetに入力します。実際に必要なのは、データセット用の1つだけです。これがあなたの問題の何かを変えるかどうか試してみてください。

    Dim ds as New DataSet
    ds.Locale = System.Globalization.CultureInfo.InvariantCulture 
    Me.dataAdapter.Fill(ds) 
    DataGridView3.AutoResizeColumns(DataGridViewAutoSizeColumnMode.AllCellsExceptHeader) 
    DataGridView3.DataSource = ds.Tables(0) 

また、終了時にデータセットを破棄しないでください。引き続き使用しています。
この場合、ガベージコレクションにこのタスクを処理させることをお勧めします。

于 2012-05-22T20:15:45.517 に答える