You asked for it:
Here is the stack trace for a typical error:
Type: System.Data.SqlClient.SqlException
Source: .Net SqlClient Data Provider
Target Site: System.Data.SqlClient.SqlDataReader
ExecuteReader(System.Data.CommandBehavior,
System.Data.SqlClient.RunBehavior, Boolean)
Description: General network error. Check your network
documentation.
Stack Trace: at
System.Data.SqlClient.SqlCommand.ExecuteReader
(CommandBehavior cmdBehavior, RunBehavior runBehavior,
Boolean returnStream)
at System.Data.SqlClient.SqlCommand.ExecuteScalar()
at data.SearchRequest.Execute() in
DataAccessXml\Search.cs:line 1965
This points to the following code:
try
{
Debug.WriteLineIf(ts.TraceInfo,
LogMessages.FormatMessage("DATA0128I"),
ErrorCategories.Info);
if(startRow == 0)
{
int totalRecords = 0;
Debug.Indent();
Debug.WriteLineIf(ts.TraceInfo,
LogMessages.FormatMessage("DATA0129I"),
ErrorCategories.Info);
using(SqlConnection connection =
new SqlConnection(connectString))
{
connection.Open();
SqlCommand countCommand =
connection.CreateCommand();
countCommand.CommandType =
CommandType.Text;
countCommand.CommandText =
searchCriteria.CountQuery;
totalRecords = (int)
countCommand.ExecuteScalar();
Debug.WriteLineIf
(ts.TraceInfo, LogMessages.FormatMessage("DATA0130I",
totalRecords), ErrorCategories.Info);
reply.TotalRows =
Convert.ToString(totalRecords);
Debug.WriteLineIf
(ts.TraceInfo, LogMessages.FormatMessage("DATA0133I"),
ErrorCategories.Info);
searchCriteria.HistoryInitialization(ref countCommand);
countCommand.ExecuteNonQuery();
connection.Close();
}
Debug.Unindent();
}
using(SqlConnection
connection = new SqlConnection(connectString))
{
connection.Open();
SqlCommand
queryCommand = connection.CreateCommand();
queryCommand.CommandType = CommandType.Text;
queryCommand.CommandText = searchCriteria.Query;
using
(SqlDataReader reader = queryCommand.ExecuteReader())
{
// skip
over the rows until start
bool
moreRows = true;
for(int i
= 0; i < startRow && moreRows == true; i++)
{
moreRows = reader.Read();
}
int
rowCount = 0;
ArrayList
rows = new ArrayList();
while
(reader.Read())
{
//
Copy the results
Column[] results = new Column[reader.FieldCount];
for
(int i = 0; i < reader.FieldCount; i++)
{
results
= new Column();
results.Id = reader.GetName(i);
results.ColumnValue = reader.GetValue(i);
if(Convert.IsDBNull(results.ColumnValue))
results.ColumnValue = null;
}
Row row = new Row();
row.Index = rows.Count;
row.Columns = results;
rows.Add(row);
rowCount++;
if
(rowCount >= maxRows)
break;
}
reply.SearchRows = (Row[])rows.ToArray(typeof
(Row));
}
}
Debug.WriteLineIf
(ts.TraceInfo, LogMessages.FormatMessage("DATA0134I"),
ErrorCategories.Info);
return reply;
}
Specificially the exception is thrown when I am "counting"
the rows with the ExecuteScalar call above.
I don't know if this helps or makes things more unclear.
Kevin
-----Original Message-----
Kevin,
I think we would all be very interested to see the portion of code that you
are talking about here.
In your statement,
My question would be, how are you 'counting' the rows, then 'reading only
the required rows', then 'writing a record to the database'. With this in
mind, and without seeing the code, I would say that there is no way to
'count the rows', then go back to read the proper rows, and I would say to
make sure you have a reader.close() after your read loop, and check to see
if you are reusing the same connection to execute the database write that
your reader is using?
.