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 方法两次。
一次填充 DataTable,再次填充 DataSet。只有一个数据集是真正需要的。如果这改变了你的问题,你能试试吗?

    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 回答