我正在尝试学习VB.NET,我正在寻找从Access 2010视图中检索数据,使用Vb.NET 2008,问题是我收到以下错误。 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);
感谢任何人
答案 0 :(得分:2)
在这一行
myoleDataAdapter.Fill(mydataSet)
您尝试填充数据集,但此数据集从未初始化。
按照实际模式,您应该更改以这种方式初始化dataadapter的代码
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
变量传递和从未使用过的目的是什么?
答案 1 :(得分:0)
为什么不返回Dataset
而不是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")