Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > very strange bug

Reply
Thread Tools

very strange bug

 
 
André Freitas
Guest
Posts: n/a
 
      02-02-2010
I got a namespace, with a class, with a constructor, who have the follow
method:

public DataTable ExecuteDataTable(string transactSQL, bool isProcedure,
List<DbParameter> parameterList)
{
DbConnection dbConnection = CreateConnection();
dbConnection.Open();
DbCommand dbcommand = dbConnection.CreateCommand();
dbcommand.CommandText = transactSQL;
if (isProcedure)
{
dbcommand.CommandType = CommandType.StoredProcedure;
}
if (parameterList != null)
{
dbcommand.Parameters.AddRange(parameterList.ToArra y());
}
DbDataReader dbDataReader = dbcommand.ExecuteReader();
DataTable dataTable = new DataTable();
dataTable.Load(dbDataReader);
dbDataReader.Close();
dbDataReader.Dispose();
dbcommand.Dispose();
dbConnection.Close();
dbConnection.Dispose();
return dataTable;
}

In a .cs page, I have a instance, calling the method:

Database database = new Database("databasename");
List<DbParameter> list = new List<DbParameter>();
list.Add(database.CreateParameter("storeId", 154));
DataTable departaments = database.ExecuteDataTable("SELECT a FROM b WHERE
lojaid = @storeId", false, list);
DataTable sections = database.ExecuteDataTable("SELECT a FROM c WHERE lojaid
= @storeId", false, list);

I got a bug, telling me "another SqlParameterCollection already have a
SqlParameter", when the method is called for the second time. (???)
After some deployment, i discover the error here:

dbcommand.Parameters.AddRange(parameterList.ToArra y());

I think its very strange, beucase im using a istance. Some time later,
recreating list before each execute, it works well.
A hour later I found a solution:

dbcommand.Parameters.Clear();
dbcommand.Dispose();

It means, when I dispose the dbcommand, something hapens with the list.
More: in debug mode, the list seems very well in the second time.

Anyone knows why?


 
Reply With Quote
 
 
 
 
pers
Guest
Posts: n/a
 
      02-03-2010
Check your dbConnection.CreateCommand();
I think CreateCommand returns same reference for all.

"André Freitas" <andrefreitas> wrote in message
news:(E-Mail Removed)...
>I got a namespace, with a class, with a constructor, who have the follow
>method:
>
> public DataTable ExecuteDataTable(string transactSQL, bool isProcedure,
> List<DbParameter> parameterList)
> {
> DbConnection dbConnection = CreateConnection();
> dbConnection.Open();
> DbCommand dbcommand = dbConnection.CreateCommand();
> dbcommand.CommandText = transactSQL;
> if (isProcedure)
> {
> dbcommand.CommandType = CommandType.StoredProcedure;
> }
> if (parameterList != null)
> {
> dbcommand.Parameters.AddRange(parameterList.ToArra y());
> }
> DbDataReader dbDataReader = dbcommand.ExecuteReader();
> DataTable dataTable = new DataTable();
> dataTable.Load(dbDataReader);
> dbDataReader.Close();
> dbDataReader.Dispose();
> dbcommand.Dispose();
> dbConnection.Close();
> dbConnection.Dispose();
> return dataTable;
> }
>
> In a .cs page, I have a instance, calling the method:
>
> Database database = new Database("databasename");
> List<DbParameter> list = new List<DbParameter>();
> list.Add(database.CreateParameter("storeId", 154));
> DataTable departaments = database.ExecuteDataTable("SELECT a FROM b WHERE
> lojaid = @storeId", false, list);
> DataTable sections = database.ExecuteDataTable("SELECT a FROM c WHERE
> lojaid = @storeId", false, list);
>
> I got a bug, telling me "another SqlParameterCollection already have a
> SqlParameter", when the method is called for the second time. (???)
> After some deployment, i discover the error here:
>
> dbcommand.Parameters.AddRange(parameterList.ToArra y());
>
> I think its very strange, beucase im using a istance. Some time later,
> recreating list before each execute, it works well.
> A hour later I found a solution:
>
> dbcommand.Parameters.Clear();
> dbcommand.Dispose();
>
> It means, when I dispose the dbcommand, something hapens with the list.
> More: in debug mode, the list seems very well in the second time.
>
> Anyone knows why?
>
>



 
Reply With Quote
 
 
 
 
André Freitas
Guest
Posts: n/a
 
      02-03-2010
> Check your dbConnection.CreateCommand();
> I think CreateCommand returns same reference for all.
>


>>I got a namespace, with a class, with a constructor, who have the follow
>>method:
>>
>> public DataTable ExecuteDataTable(string transactSQL, bool isProcedure,
>> List<DbParameter> parameterList)
>> {
>> DbConnection dbConnection = CreateConnection();
>> dbConnection.Open();
>> DbCommand dbcommand = dbConnection.CreateCommand();
>> dbcommand.CommandText = transactSQL;
>> if (isProcedure)
>> {
>> dbcommand.CommandType = CommandType.StoredProcedure;
>> }
>> if (parameterList != null)
>> {
>> dbcommand.Parameters.AddRange(parameterList.ToArra y());
>> }
>> DbDataReader dbDataReader = dbcommand.ExecuteReader();
>> DataTable dataTable = new DataTable();
>> dataTable.Load(dbDataReader);
>> dbDataReader.Close();
>> dbDataReader.Dispose();
>> dbcommand.Dispose();
>> dbConnection.Close();
>> dbConnection.Dispose();
>> return dataTable;
>> }
>>
>> In a .cs page, I have a instance, calling the method:
>>
>> Database database = new Database("databasename");
>> List<DbParameter> list = new List<DbParameter>();
>> list.Add(database.CreateParameter("storeId", 154));
>> DataTable departaments = database.ExecuteDataTable("SELECT a FROM b WHERE
>> lojaid = @storeId", false, list);
>> DataTable sections = database.ExecuteDataTable("SELECT a FROM c WHERE
>> lojaid = @storeId", false, list);
>>
>> I got a bug, telling me "another SqlParameterCollection already have a
>> SqlParameter", when the method is called for the second time. (???)
>> After some deployment, i discover the error here:
>>
>> dbcommand.Parameters.AddRange(parameterList.ToArra y());
>>
>> I think its very strange, beucase im using a istance. Some time later,
>> recreating list before each execute, it works well.
>> A hour later I found a solution:
>>
>> dbcommand.Parameters.Clear();
>> dbcommand.Dispose();
>>
>> It means, when I dispose the dbcommand, something hapens with the list.
>> More: in debug mode, the list seems very well in the second time.
>>
>> Anyone knows why?


Nop:

private DbConnection CreateConnection(string connectionString)
{
DbConnection dbConnection = new SqlConnection(connectionString);
return dbConnection;
}


 
Reply With Quote
 
Alvin Bruney - ASP.NET MVP
Guest
Posts: n/a
 
      02-14-2010
This behavior is by design. The DBparamater retains state information. You
need to first call clear on it if you intend to reuse it.

--
Vapordan
Shameless Author Plug
ASP.NET 4 by Example only $20
OWC Blackbook www.lulu.com/owc

"André Freitas" <andrefreitas> wrote in message
news:(E-Mail Removed)...
> I got a namespace, with a class, with a constructor, who have the follow
> method:
>
> public DataTable ExecuteDataTable(string transactSQL, bool isProcedure,
> List<DbParameter> parameterList)
> {
> DbConnection dbConnection = CreateConnection();
> dbConnection.Open();
> DbCommand dbcommand = dbConnection.CreateCommand();
> dbcommand.CommandText = transactSQL;
> if (isProcedure)
> {
> dbcommand.CommandType = CommandType.StoredProcedure;
> }
> if (parameterList != null)
> {
> dbcommand.Parameters.AddRange(parameterList.ToArra y());
> }
> DbDataReader dbDataReader = dbcommand.ExecuteReader();
> DataTable dataTable = new DataTable();
> dataTable.Load(dbDataReader);
> dbDataReader.Close();
> dbDataReader.Dispose();
> dbcommand.Dispose();
> dbConnection.Close();
> dbConnection.Dispose();
> return dataTable;
> }
>
> In a .cs page, I have a instance, calling the method:
>
> Database database = new Database("databasename");
> List<DbParameter> list = new List<DbParameter>();
> list.Add(database.CreateParameter("storeId", 154));
> DataTable departaments = database.ExecuteDataTable("SELECT a FROM b WHERE
> lojaid = @storeId", false, list);
> DataTable sections = database.ExecuteDataTable("SELECT a FROM c WHERE
> lojaid = @storeId", false, list);
>
> I got a bug, telling me "another SqlParameterCollection already have a
> SqlParameter", when the method is called for the second time. (???)
> After some deployment, i discover the error here:
>
> dbcommand.Parameters.AddRange(parameterList.ToArra y());
>
> I think its very strange, beucase im using a istance. Some time later,
> recreating list before each execute, it works well.
> A hour later I found a solution:
>
> dbcommand.Parameters.Clear();
> dbcommand.Dispose();
>
> It means, when I dispose the dbcommand, something hapens with the list.
> More: in debug mode, the list seems very well in the second time.
>
> Anyone knows why?
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
very very very long integer shanx__=|;- C Programming 19 10-19-2004 03:55 PM
very very very long integer Abhishek Jha C Programming 4 10-17-2004 08:19 AM
Quick Book file access very very very slow Thomas Reed Computer Support 7 04-09-2004 08:09 PM
Very very strange. Major bug? Shawn ASP .Net 4 02-04-2004 07:58 AM
very Very VERY dumb Question About The new Set( ) 's Raymond Arthur St. Marie II of III Python 4 07-27-2003 12:09 AM



Advertisments