1

我正在尝试学习 VB.NET,并且希望使用 Vb.NET 2008 从 Access 2010 视图中检索数据,问题是我收到以下错误。System.ArgumentNullException 未处理 Message=Value 不能为 null

我无法弄清楚如何解决该错误,因此希望有更多经验的人可以提供帮助。

Public Function GetMyoleDataAdapterStudentQuestionRepeat(ByRef mydataSet As DataSet, ByVal topicId As String, ByVal groupId As String) As OleDbDataAdapter
            Try
                Dim strAccessConn As String = _appConfigDbConn
                Dim cn As OleDbConnection = New OleDbConnection(strAccessConn)
                Dim ds As New DataSet
                Dim da As New OleDbDataAdapter("qryStudentNameRandomBasedOnScore", cn)
                da.SelectCommand.CommandType = CommandType.StoredProcedure
                da.SelectCommand.Parameters.AddWithValue("@GroupID", groupId)
                da.Fill(ds, "Student")
                Return da
            Catch ex As Exception
                Throw New ApplicationException(ex.InnerException.Message.ToString())
            End Try
        End Function

在按钮内点击

Dim myoleDataAdapter As OleDbDataAdapter = GroupData.GetMyoleDataAdapterStudentQuestionRepeat(mydataSet, topicId, groupId)
            myoleDataAdapter.Fill(mydataSet)
            txtStudentName.DataBindings.Add("Text", mydataSet.Tables(0), "studentname")

此行引发错误:myoleDataAdapter.Fill(mydataSet)

如果有帮助,我的观点是

SELECT TOP 1 tblStudentNameAndScore.studentname
FROM tblStudentNameAndScore
WHERE (((tblStudentNameAndScore.[QuizCount]) Between 2 And 10)) AND  tblStudentNameAndScore.GroupID = ?
ORDER BY Rnd(QuizCount); 

感谢您的任何帮助

4

2 回答 2

2

在这一行

myoleDataAdapter.Fill(mydataSet)

您尝试填充数据集,但该数据集从未初始化。

按照您的实际模式,您应该更改以这种方式初始化数据适配器的代码

Public Function GetMyoleDataAdapterStudentQuestionRepeat(ByVal topicId As String, ByVal groupId As String) As OleDbDataAdapter
    Try
        Dim strAccessConn As String = _appConfigDbConn
        Dim cn As OleDbConnection = New OleDbConnection(strAccessConn)
        Dim ds As New DataSet
        Dim da As New OleDbDataAdapter("qryStudentNameRandomBasedOnScore", cn)
        da.SelectCommand.CommandType = CommandType.StoredProcedure
        da.SelectCommand.Parameters.AddWithValue("@GroupID", groupId)
        Return da
    Catch ex As Exception
        Throw New ApplicationException(ex.InnerException.Message.ToString())
    End Try
        End Function

然后在调用者代码中写

 Dim myoleDataAdapter As OleDbDataAdapter = GroupData.GetMyoleDataAdapterStudentQuestionRepeat( topicId, groupId)
 mydataSet = new DataSet()
 myoleDataAdapter.Fill(mydataSet)
 ....

在返回适配器的方法中,您不需要传递数据集,因为您不以任何方式使用它,也无需创建和填充另一个数据集。只需返回适配器并处理调用者代码

当然,您也可以更改方法并返回一个填充了您的数据的数据集,而不是 DataAdapter。可能这个解决方案可以被认为更加封装

Public Function GetStudentQuestionRepeat(ByVal topicId As String, ByVal groupId As String) _
                As Dataset
    ....
    Dim da As New OleDbDataAdapter("qryStudentNameRandomBasedOnScore", cn)
    da.SelectCommand.CommandType = CommandType.StoredProcedure
    da.SelectCommand.Parameters.AddWithValue("@GroupID", groupId)
    Dim ds = new Dataset()
    da.Fill(ds, "Student")
    Return ds
    ....        
End Function

顺便说一句,topidID变量传递但从未使用过的目的是什么?

于 2013-11-09T11:03:14.527 回答
0

为什么不返回 aDataset而不是DataAdapter,例如:

    Public Function GetMyoleDataAdapterStudentQuestionRepeat(ByVal topicId As String, ByVal groupId As String) As DataSet
        Try
            Dim strAccessConn As String = _appConfigDbConn
            Dim cn As OleDbConnection = New OleDbConnection(strAccessConn)
            Dim ds As New DataSet
            Dim da As New OleDbDataAdapter("qryStudentNameRandomBasedOnScore", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@GroupID", groupId)
            da.Fill(ds, "Student")
            Return ds
        Catch ex As Exception
            Throw New ApplicationException(ex.InnerException.Message.ToString())
        End Try
    End Function

然后当你打电话时它会更短:

  Dim myDataSet As DataSet = GroupData.GetMyoleDataAdapterStudentQuestionRepeat(topicId, groupId)

  txtStudentName.DataBindings.Add("Text", myDataSet.Tables(0), "studentname")
于 2013-11-09T11:13:53.267 回答