Duplicate record with wrong exception

G

Guest

I've found my program got exception as follows:-

----------------------------------------------
Exception: System.ArgumentException
Message: The SqlParameter with ParameterName 'XXXXX' is already contained by
another SqlParameterCollection.
Source: System.Data
at System.Data.SqlClient.SqlParameterCollection.Validate(Int32 index,
SqlParameter value)
at
System.Data.SqlClient.SqlParameterCollection.AddWithoutEvents(SqlParameter
value)
at System.Data.SqlClient.SqlParameterCollection.Add(SqlParameter value)
at System.Data.SqlClient.SqlParameterCollection.Add(Object value)
at GotDotNet.ApplicationBlocks.Data.AdoHelper.AttachParameters(IDbCommand
command, IDataParameter[] commandParameters)
at GotDotNet.ApplicationBlocks.Data.AdoHelper.PrepareCommand(IDbCommand
command, IDbConnection connection, IDbTransaction transaction, CommandType
commandType, String commandText, IDataParameter[] commandParameters, Boolean&
mustCloseConnection)
at
GotDotNet.ApplicationBlocks.Data.AdoHelper.ExecuteNonQuery(IDbConnection
connection, CommandType commandType, String commandText, IDataParameter[]
commandParameters)
at GotDotNet.ApplicationBlocks.Data.AdoHelper.ExecuteNonQuery(String
connectionString, CommandType commandType, String commandText,
IDataParameter[] commandParameters)
at GotDotNet.ApplicationBlocks.Data.AdoHelper.ExecuteNonQuery(String
connectionString, String spName, Object[] parameterValues)
....
----------------------------------------------

when I find out the problem I've found that my database already has that
record. So I wondor that why it does not throw exception like another
duplicate record something like ".... PK_XXXX ...." (duplication key). I've
got this exception in sometimes (the most is SqlException with violation key).

Someone may be wonder that why I do not post this to the board at GotDotNet.
Because I've already posted it and may be someone in this place can figure
out of my problem.

Thanks in advance,
Thana N.
 
G

Guest

Message: The SqlParameter with ParameterName 'XXXXX' is already contained by
another SqlParameterCollection.

It looks to me like you're adding a parameter with a conflicting name to the
parameter collection. I did that once, now I use Parameters.Clear() before
reusing a Command.
 
G

Guest

Sorry for my uncleared information. The program already has the line you told.
If I save the message that make the exception as a parameter of the function
to do
a testing. The exception always throw everytime this message is passed thru
the function. Event if the first time the program starts. But I cannot send
you the code, message, sp and database layout.

Thanks for your reply.
 
G

Guest

OK, the following code causes the same error. Notice I'm adding an instance
of a parameter to two commands or one command twice.

namespace Template
{
class Template
{
[System.STAThread]
static void Main(string[] args)
{
System.Data.SqlClient.SqlConnection con = new
System.Data.SqlClient.SqlConnection() ;
System.Data.SqlClient.SqlCommand cm1 = new
System.Data.SqlClient.SqlCommand ( "hi" , con ) ;
System.Data.SqlClient.SqlCommand cm2 = new
System.Data.SqlClient.SqlCommand ( "hi" , con ) ;

System.Data.SqlClient.SqlParameter prm = new
System.Data.SqlClient.SqlParameter() ;

cm1.Parameters.Add ( prm ) ;

// Either of these lines will cause the error
cm1.Parameters.Add ( prm ) ;
cm2.Parameters.Add ( prm ) ;

return ;
}
}
}

C:\>pt

Unhandled Exception: System.ArgumentException: The SqlParameter with
ParameterName 'Parameter1' is already contained by another
SqlParameterCollection.
at System.Data.SqlClient.SqlParameterCollection.Validate(Int32 index,
SqlParameter value)
at
System.Data.SqlClient.SqlParameterCollection.AddWithoutEvents(SqlParameter
value)
at System.Data.SqlClient.SqlParameterCollection.Add(SqlParameter value)
at Template.Template.Main(String[] args)
 
G

Guest

Ok, I see. But my program injects the data like this about
2000 messages/day. And I've got the exception just a few,
the other sucess the job. Other infomation you should know
is I run this program in 2 computers to inject data from 2
sources (main, backup). So if you're right, how can I check
the duplication you've mentioned.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top