Visual Basic .NET » Database Programming
DataReader Problem -- gnalin --


Hello guys,

Please, anyone already see this problem ?


System.Data.SqlClient.SqlDataReader.PrepareSQLRecord(Int32 i) +159
System.Data.SqlClient.SqlDataReader.GetSqlString(Int32 i) +20
System.Data.SqlClient.SqlDataReader.GetString(Int32 i) +32

The properties HasRows and isClosed is okay... The reader has rows and not closed !!

How do to solve this ?

Thanks a lot and sorry.... my english is bad.

-- BradleyPeter --


Hard to say without you giving us an example of your code ...

But in general, a NullReferenceException is thrown if you try to use
(e.g. call a method or access a property) an instance of a reference type that has not been initialized.

HTH
Peter

-----Original Message-----
From: DotNetDevelopment
DataReader Problem
Hello guys,

Please, anyone already see this problem ?


System.Data.SqlClient.SqlDataReader.PrepareSQLRecord(Int32 i) +159
System.Data.SqlClient.SqlDataReader.GetSqlString(Int32 i) +20
System.Data.SqlClient.SqlDataReader.GetString(Int32 i) +32

The properties HasRows and isClosed is okay... The reader has rows and not closed !!

How do to solve this ?

Thanks a lot and sorry.... my english is bad.


-- gnalin --


Yes, I agree ... but in the stack trace above the error occurs in System classes ! .... My object is a SqlDataReader and it is seted and hasRows ans is not Closed !

The code is simple.... is a While(Read()) ... inside this I try to get a string value... Reader.GetString(0)...... so, at this point, the error occurs !!!!! The methos Read() run ok .....

-- BradleyPeter --


What is the line in your code that causes the exception to be thrown?
Some lines either side for context would be nice as well.

I take it that when you say that isClosed is okay, you mean that it returns false.

Peter

-----Original Message-----
From: DotNetDevelopment
Re: DataReader Problem
Yes, I agree ... but in the stack trace above the error occurs in System classes ! .... My object is a SqlDataReader and it is seted and hasRows ans is not Closed !

The code is simple.... is a While(Read()) ... inside this I try to get a string value... Reader.GetString(0)...... so, at this point, the error occurs !!!!! The methos Read() run ok .....


-- gnalin --


Yes ! isClosed is False.

The line is:

string myString = dr.GetString(0);

Detail: The error occurs, but, if you press F5... Works !!! So ... only any times works !! sometimes works... and sometimes not work !!! With the same dataSource....

Bradley, Peter wrote:
What is the line in your code that causes the exception to be thrown?
Some lines either side for context would be nice as well.

I take it that when you say that isClosed is okay, you mean that it
returns false.

Peter

-----Original Message-----
From: DotNetDevelopment
On Behalf Of gnalin
Sent: 12 July 2006 13:14
To: DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web
Services,.NET Remoting
Subject:
Re: DataReader Problem
Yes, I agree ... but in the stack trace above the error occurs in
System classes ! .... My object is a SqlDataReader and it is seted and
hasRows ans is not Closed !

The code is simple.... is a While(Read()) ... inside this I try to get
a string value... Reader.GetString(0)...... so, at this point, the
error occurs !!!!! The methos Read() run ok .....

 

-- BradleyPeter --


Well, as I keep saying, some code in context would help. If you don't give us some context it's really hard to help.

You're not trying to read a value you've already read are you?
DataReaders are forward-only. But, without context, I can't see what you're trying to do.

Sorry
Peter
-----Original Message-----
From: DotNetDevelopment
Re: DataReader Problem
Yes ! isClosed is False.

The line is:

string myString = dr.GetString(0);

Detail: The error occurs, but, if you press F5... Works !!! So ... only any times works !! sometimes works... and sometimes not work !!! With the same dataSource....

Bradley, Peter wrote:
What is the line in your code that causes the exception to be thrown?
Some lines either side for context would be nice as well.

I take it that when you say that isClosed is okay, you mean that it
returns false.

Peter

-----Original Message-----
From: DotNetDevelopment
On Behalf Of gnalin
Sent: 12 July 2006 13:14
To: DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web
Services,.NET Remoting
Subject:
Re: DataReader Problem
Yes, I agree ... but in the stack trace above the error occurs in
System classes ! .... My object is a SqlDataReader and it is seted and
hasRows ans is not Closed !

The code is simple.... is a While(Read()) ... inside this I try to get
a string value... Reader.GetString(0)...... so, at this point, the
error occurs !!!!! The methos Read() run ok .....


 

-- gnalin --



The error occurs in method setNewRow at the first GetString()

protected void setNewRow(int loopIndex, DataTable dtRet,Abit.Comex.Data.DataReader dr)
{
DataRow newRow = dtRet.NewRow();
newRow
= dr.Reader.GetString(0);
dr.Reader.GetInt32(1);
if(dr.Reader
=
dr.Reader.GetString(2);
if(dr.Reader
=
dr.Reader.GetDouble(3);
if(dr.Reader
=
dr.Reader.GetDouble(4);
dtRet.Rows.Add(newRow);
}

public DataTable ExecuteRelatorio(IRelatorioExecution DataClassExecution, TypeRelatorio TipoRelatorio,string FiltroNCMIni,
string FiltroNCMFinal, params DateTime
Dates)
{
DataTable dtRet = GetRelatorioDataTable(Dates);
HelperCommand HComm = null;
for(int i = 0; i < Dates.Length; i=i+2)
{
if(TipoRelatorio == TypeRelatorio.Exportacao) HComm =
DataClassExecution.ExecuteRelatorioExp(FiltroNCMIni, FiltroNCMFinal,
Dates
);
else HComm = DataClassExecution.ExecuteRelatorioImp(FiltroNCMIni,
FiltroNCMFinal, Dates
);
DataReader dr =
HComm.ExecuteReader(CommandBehavior.CloseConnection);
if(dr.Reader.IsClosed) return(dtRet);
while (dr.Reader.Read())
{
if(i==0) setNewRow(i,dtRet,dr);
else
{
string NCM = dr.Reader.GetString(0);
int ID = dr.Reader.GetInt32(1);
DataRow
rows = dtRet.Select("ID=" + ID + " and NCM ='"+ NCM
+"'");
if(rows.Length == 0) setNewRow(i,dtRet,dr);
else
{
rows
= dr.Reader.GetDouble(3);
rows
= dr.Reader.GetDouble(4);
}
}

}
dr.Reader.Close();
HComm.CommandDisponse();
}
HComm.Finish();
return(dtRet);
}

-- BradleyPeter --


Then assuming that the dr.Reader property returns a DataReader, I think it's because you haven't done a Read() on it, so it isn't pointing to a row, so its first column is pointing to null.

There is no Reader property of a standard DataReader, so you may have to dig into the documentation of the Abit.Comex.Data.DataReader object.
Peter

-----Original Message-----
From: DotNetDevelopment
Re: DataReader Problem

The error occurs in method setNewRow at the first GetString()

protected void setNewRow(int loopIndex, DataTable dtRet,Abit.Comex.Data.DataReader dr)
{
DataRow newRow = dtRet.NewRow();
newRow
= dr.Reader.GetString(0);
dr.Reader.GetInt32(1);
if(dr.Reader
!= DBNull.Value)
newRow
=
dr.Reader.GetString(2);
if(dr.Reader
!= DBNull.Value)
newRow
=
dr.Reader.GetDouble(3);
if(dr.Reader
!= DBNull.Value)
newRow
=
dr.Reader.GetDouble(4);
dtRet.Rows.Add(newRow);
}

public DataTable ExecuteRelatorio(IRelatorioExecution DataClassExecution, TypeRelatorio TipoRelatorio,string FiltroNCMIni,
string FiltroNCMFinal, params DateTime
Dates)
{
DataTable dtRet = GetRelatorioDataTable(Dates);
HelperCommand HComm = null;
for(int i = 0; i < Dates.Length; i=i+2)
{
if(TipoRelatorio ==
TypeRelatorio.Exportacao) HComm =
DataClassExecution.ExecuteRelatorioExp(FiltroNCMIni, FiltroNCMFinal,
Dates
);
else HComm =
DataClassExecution.ExecuteRelatorioImp(FiltroNCMIni,
FiltroNCMFinal, Dates
);
DataReader dr =
HComm.ExecuteReader(CommandBehavior.CloseConnection);
if(dr.Reader.IsClosed) return(dtRet);
while (dr.Reader.Read())
{
if(i==0) setNewRow(i,dtRet,dr);
else
{
string NCM =
dr.Reader.GetString(0);
int ID =
dr.Reader.GetInt32(1);
DataRow
rows =
dtRet.Select("ID=" + ID + " and NCM ='"+ NCM
+"'");
if(rows.Length == 0)
setNewRow(i,dtRet,dr);
else
{
rows
=
dr.Reader.GetDouble(3);
rows
=
dr.Reader.GetDouble(4);
}
}

}
dr.Reader.Close();
HComm.CommandDisponse();
}
HComm.Finish();
return(dtRet);
}


-- gnalin --


Yes... I develop the Abit.Comex.Data.DataReader object.... and it is only for mask the dataLayer... in this case, dr.Reader is a SqlDataReader....

Ok, i understood ... but if the method Read() executes and return
"true"... It means that the row pointer sucessfuly point to a row...Or not ?

-- BradleyPeter --


Oops! Sorry, I didn't directly answer your question. Here's what MSDN says about the SqlDataReader:

"Return Value -
true if there are more rows; otherwise, false."

So, assuming the DataReader in your object is the same, then the return value indicates whether or not there are any rows left. This is why you can do:

while(dr.Read()){}

HTH
Peter

-----Original Message-----
From: DotNetDevelopment
Re: DataReader Problem
Yes... I develop the Abit.Comex.Data.DataReader object.... and it is only for mask the dataLayer... in this case, dr.Reader is a SqlDataReader....

Ok, i understood ... but if the method Read() executes and return
"true"... It means that the row pointer sucessfuly point to a row...Or not ?


-- BradleyPeter --


This is what msdn says about the SqlDataReader:

"The default position of the SqlDataReader is prior to the first record.
Therefore, you must call Read to begin accessing any data."

I assume it will be the same for all other DataReaders as well.

The first call to Read() makes the row pointer point to the first row
(index 0).

HTH
Peter

-----Original Message-----
From: DotNetDevelopment
Re: DataReader Problem
Yes... I develop the Abit.Comex.Data.DataReader object.... and it is only for mask the dataLayer... in this case, dr.Reader is a SqlDataReader....

Ok, i understood ... but if the method Read() executes and return
"true"... It means that the row pointer sucessfuly point to a row...Or not ?


-- gnalin --


So.... in this case... My code is correct ! isn't ? I'm only access the values of columns of the DataReader inside the Read() While....

-- BradleyPeter --

Dunno, because things are a bit complicated as you're passing your wrapper around as a parameter. What I'd do is check it out in a debugger to see whether or not the row pointer is at the correct place, and also to check that the object you passed in still looks the same as it did when it was passed.

Or you could just call Read() on dr.Reader and see if the problem goes away ...

HTH
Peter
-----Original Message-----
From: DotNetDevelopment on behalf of gnalin Sent: Wed 7/12/2006 5:01 PM To: DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting Cc:
Subject:
Re: DataReader Problem
So.... in this case... My code is correct ! isn't ? I'm only access the values of columns of the DataReader inside the Read() While....



-- jackytinh --


im sorry. i cant read english.im frome vietnam.

[Submit Comment]Home