在vb.net上使用数据适配器填充命令出现错误

时间:2019-07-24 08:42:44

标签: vb.net command fill dataadapter

在下面的代码中,我通过单击一个按钮多次运行此操作,该按钮每次都会更改布尔值。

它运行但停在da.Fill(ds,“ AlLData”)行,并出现错误“对象引用未设置为对象的实例”

私有子GetDataForForm()

 Try
       Dim cmd As New SqlCommand
        If bClientsOnly Then
           cmd.CommandText = "ViewClients"
            cmd.Parameters.Add("@IDArea", SqlDbType.Int).Value = IDAreaID
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = conn
            da = New SqlDataAdapter(cmd)
            ' ****Error here  *** '
            da.Fill(ds, "AllData")
            Me.bs1.DataSource = ds.Tables("AllData")
            da.Dispose()
        Else
            ' Clients & Services
             cmd.CommandText = "ViewServices"
            cmd.Parameters.Add("@IDArea", SqlDbType.Int).Value = IDAreaID
     cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = conn
            da1 = New SqlDataAdapter(cmd)
            da1.Fill(ds, "AllData2")
            Me.bs1.DataSource = ds.Tables("AllData2")
            da1.Dispose()
        End If
            '...bind DataGridView1 to BindingNavigator's BindingSource   
        Me.BindingNavigator1.BindingSource = Me.bs1
        Me.DataGridView1.DataSource = Me.BindingNavigator1.BindingSource
        cmd.Dispose()

    Catch ex As Exception
        MessageBox.Show(ex.Message, strTitle)
    End Try

End Sub

有趣的是,当我传递参数IDAreaID> 1时,会产生错误,但是如果我使用1,则每次都会起作用。我已经手动检查了存储过程,并且每次都运行。

0 个答案:

没有答案
相关问题