Click here to Skip to main content
15,356,641 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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!
Posted
Updated 6-Jun-11 23:14pm
v2

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.
C#
DataTable tbl = Get3wStatusesToSend();

foreach (DataRow row in tbl.Rows)
{
  // Do processing of each row.
  string name = (string)row["name"]; // Get name from row
}
   
Comments
ambarishtv 5-Jun-11 4:32am
   
+5 :)
Kim Togo 5-Jun-11 5:06am
   
Thanks :-)
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”
   
Comments
AFokas 5-Jul-11 3:43am
   
Hello and thanks for your reply. Unfortunately this does not apply to me. The dataset that's returned does not have NULL values in it.
mfgirruite 2-Sep-14 7:57am
   
Thanks a lot,I couldn't find cause but now that you stated yours I even managed to solve my problem
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);
}


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);
}


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.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900