异常"DataTableReader对于当前DataTable无效". [英] Exception "DataTableReader is invalid for current DataTable"

查看:172
本文介绍了异常"DataTableReader对于当前DataTable无效".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有一些直到最近才起作用的代码,但是突然开始抛出异常"DataTableReader对于当前DataTable无效".奇怪的是,这仅在调试时发生.发布配置正常运行.
代码是:

Hello everybody,
I have some code that was working until recently, but suddenly started throwing the exception "DataTableReader is invalid for current DataTable". The strange is that this happens only when debuging. Release configuration runs normally.
The code is:

public static DataTable Get3wStatusesToSend() {
	DataSet oDataSet = new DataSet();
	string sGatherSQL = "SELECT * FROM ... ";
	SqlCommand oCommand = new SqlCommand(sGatherSQL);
	oCommand.Connection = OpenConnection();
	
	SqlDataAdapter oAdapter = new SqlDataAdapter(oCommand);
	oAdapter.Fill(oDataSet);
	oCommand.Connection.Close();
	return oDataSet.Tables[0];
}


还有其他地方


and somewhere else

DataTableReader oReader = Get3wStatusesToSend().CreateDataReader();
while(oReader.Read()) {


此时将引发异常.
该表不为空,已检查并具有行.
有任何建议请.


At this point the exception is thrown.
The table is not empty, have checked and it has rows.
Any suggestions please.

Thanks in advance!

推荐答案

您正在关闭方法Get3wStatusesToSend中的SqlConnection.也许与此有关?

或者,我将对从Get3wStatusesToSend方法返回的DataTable进行foreach.
You are closing the SqlConnection in method Get3wStatusesToSend. Perhaps it has something to do with that?

Alternative would I do a foreach on the DataTable returned from Get3wStatusesToSend method.
DataTable tbl = Get3wStatusesToSend();

foreach (DataRow row in tbl.Rows)
{
  // Do processing of each row.
  string name = (string)row["name"]; // Get name from row
}


我也遇到过类似的问题.我仍然不确定如何解决它,但是我找到了原因.在我的情况下,问题是由返回的字段之一中的NULL值引起的.事实上,如果任何字段包含NULL值,我都会收到错误消息:"DataTableReader对于当前DataTable" DataSet"无效.
I have had a similar problem. I am still not sure how to solve it, but I found the cause. In my case the problem was caused by a NULL value in one of the fields that were returned. As a matter of fact, if any of the fields contained a NULL value I would get the error "DataTableReader is invalid for current DataTable ''DataSet"


感谢您的答复.

我认为自从我使用DataTable以来,SqlConnection无关.
我正在从DataTable创建DataTableReader,因为我具有此功能.

Thanks for you reply.

I think that the SqlConnection has nothing to do since I''m working with DataTable.
I''m creating the DataTableReader from DataTable because I have this function.

public Checkpoint LoadFromDataReader(IDataReader oReader) {
	nkChk = (int)oReader["nkChk"];
	sChkStatus = ((string)oReader["sChkStatus"]).ToLower();
	sChkStatus3w = ((string)oReader["sChkStatus3w"]).ToLower();
	sChkSerial = ((string)oReader["sChkSerial"]).Trim();
	
	.
	.
	.
	return this;
}

while(oReader.Read()) {
	Checkpoint oCheckpoint = new Checkpoint().LoadFromDataReader(oReader);
}



这样我就可以用任何读取器(SqlDataReader,DataTableReader等)调用它了.

所以我这样做是为了尝试您的解决方案



and that way I can call it with any reader (SqlDataReader, DataTableReader, ...)

So I did this to try your solution

public Checkpoint LoadFromDataRow(DataRow oRow) {
	nkChk = (int)oRow["nkChk"];
	sChkStatus = ((string)oRow["sChkStatus"]).ToLower();
	sChkStatus3w = ((string)oRow["sChkStatus3w"]).ToLower();
	sChkSerial = ((string)oRow["sChkSerial"]).Trim();
	.
	.
	.
	return this;
}

foreach(DataRow oRow in oTable.Rows) {
	Checkpoint oCheckpoint = new Checkpoint().LoadFromDataRow(oRow);
}



虽然有效,但是却破坏了通用性.同时也让我烦恼,它只有在调试时才会崩溃,并且直到最近它都可以正常工作.
我不会接受您的答案作为解决方案,以防万一有人解释了为什么会发生这种情况,但是我会用5来打分.



and it works, but it spoils the generality. Also it keeps bugging me that it crashes only when debugging and that it was working fine until recently.
I will not accept your answer as solution yet, in case someone explains why is this happening, but I''ll rate it with 5.


这篇关于异常"DataTableReader对于当前DataTable无效".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆