473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

DataReader.GetChars bug when using SequentialAccess ?

Hi all,

Using : .NET 1.1, SQL Server 2000, Win 2K.

Could anyone confirm if there is a bug in the SqlDataReader.GetChars()
method when using CommandBehavior.SequentialAccess to get a BLOB
string from the database ? I have searched all over, and found many
threads with the same problem as I, but no word from the MS folks that
this is a bug. This problem only seems to occur when using GetChars,
not with GetBytes. But since it is a ntext column I am querying, I
wanted to use GetChars, since that is the recommended way.

The Situation : I am trying to retrieve 3 columns from the database,
the last of which is an ntext datatype. It may contain text upto 10,000
characters. I need it to be ntext, since it would possibly contain
HTML/XML markup.

The Exception : The exception message is
"System.InvalidOperationException: Invalid attempt to read from column
ordinal '2'. With CommandBehavior.SequentialAccess, you may only read
from column ordinal '3' or greater. "

I have checked the KB info for this exception at
"http://support.microsoft.com/kb/q308614". To my knowledge I am
accessing the columns in the order they were retrieved by the
DataReader.

My code is as follows :
---------------------------------

// string ItemID is retrieved from a Session object on Page Load.

private void GetData()
{
try
{
SqlConnection conn = new
SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

string strSQL = "SELECT ItemName, ItemImgUrl, ItemDesc FROM Items
WHERE ItemID = " + ItemID;
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strSQL;

conn.Open();
SqlDataReader dr =
cmd.ExecuteReader(CommandBehavior.SequentialAccess );

// The following line works fine even with GetChars(),
//but I *do* want to use SequentialAccess because of it's
performance benefits.
// Plus it streams the data directly from the database.
//SqlDataReader dr = cmd.ExecuteReader();

// Read only first row.
dr.Read();
// Read first two columns.
ItemName = dr.GetString(0);
ItemImgUrl = dr.GetString(1);

// Read CLOB column (Col. ordinal is 2)
int bufLen = 100;
int startidx = 0;
StringBuilder sb = new StringBuilder(bufLen);
StringWriter sw = new StringWriter(sb);
char[] buf = new Char[bufLen];
long clob = dr.GetChars(2,startidx,buf,0,bufLen);
while (clob == bufLen)
{
sw.Write(buf);
sw.Flush();
// Reposition the startindex
startidx += bufLen;

// Exception here, because the DataReader seems to have
progressed to the next column.
clob = dr.GetChars(2,startidx,buf,0,bufLen);
}

sw.Close();
ItemDesc = sb.ToString();

conn.Close();
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
---------------------------------

Note that if I change the "// Read CLOB column here" part to use
GetBytes as follows, it seems to work.

---------------------------------
// Read CLOB column
int bufLen = 100;
int startidx = 0;
StringBuilder sb = new StringBuilder(bufLen);
UTF8Encoding utf = new UTF8Encoding();
byte[] buf = new byte[bufLen];
long blob = dr.GetBytes(2,startidx,buf,0,bufLen);
while (blob == bufLen)
{
sb.Append(utf.GetString(buf));
startidx += bufLen;
blob = dr.GetBytes(2,startidx,buf,0,bufLen);
}

ItemDesc = sb.ToString();
---------------------------------

Any help will be greatly appreciated.
TIA,

Cerebrus.

Sep 12 '06 #1
1 5654
Update :

GetBytes only *seems* to work. I get back all my data, but each letter
has a space appended to it, so my data looks like :

M y D a t a l o o k s l i k e t h i s .

Someone please help !

Sep 12 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Yasutaka Ito | last post by:
Hi, My friend had a little confusion about the working of DataReader after reading an article from MSDN. Following is a message from him... <!-- Message starts --> I was going thru DataReader...
6
by: Ravi | last post by:
Hi, I am not able to understand why a datareader needs a connection to the DB all the time. Here is what I tried. Sqlcommand cmd = ("select * from table1",con) // where con is the connection...
6
by: Grant | last post by:
I am connecting to an access database using a datareader in C#. I get results when I run a certain query from Access but when I run it from Code it does not retrieve any results. I have put a stop...
20
by: Mark | last post by:
Hi all, quick question , a DataView is memory resident "view" of data in a data table therefore once populated you can close the connection to the database. Garbage collection can then be used to...
14
by: Bihn | last post by:
I was reading about datareader which is said to be slimmer & faster then dataset. Since the datareader have to go fetching the dat from the database every time it need it, the data it gets then...
4
by: Shapper | last post by:
Hello, I have created a datareader function in a asp.net/vb web site. The datareader returns only one record with 2 fields: and In the same aspx.vb I want to use this values as follows:...
17
by: Alan Silver | last post by:
Hello, I have a generic method in a utility class that grabs an sqldatareader and returns it. Due to the fact that (AFAIK), you can't close the database connection before you've read the data,...
10
by: jimmy | last post by:
Hi again, sorry for posting two questions so close together but im working on a school project which is due in soon and running into some difficulties implementing the database parts. I have the...
3
by: Johnny Jörgensen | last post by:
I've got an error that I simply cannot locate: I've got a form in which I use a datareader object to read information from a db. After the read, I close the reader and I dispose of both the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.