Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hallo everyone,

I am getting an exception when I try to save information into a database. I don't think I lost the concept or logic but it is getting me crazy.

The code iterates through a DataTable to get each row and passes the value contained in each row into a stored procedure. It works for a single row but it fails when the number of rows are more than one.

The Exception is "Procedure or function sp_XXX has too many arguments specified.";

Here is the sample code:

C#
public string Create_XXXInformation(DataTable dt, string OwnerID)
        {
            int row_counter = 0;
            string Result = String.Empty;
            SqlCommand command = new SqlCommand();
            command.CommandText = "sp_XXX";
            command.CommandType = CommandType.StoredProcedure;

            foreach (DataRow row in dt.Rows)
            {
                command.Connection = TrnsObj();               
                
                command.Parameters.Add("@OwnerID", SqlDbType.Int).Value = Convert.ToInt32(OwnerID); // Foreignkey at...
                command.Parameters.Add("@XXXID", SqlDbType.Int).Value = Convert.ToInt32(row["XXXID"].ToString());
                command.Parameters.Add("@YYYID", SqlDbType.Int).Value = Convert.ToInt32(row["YYYID"].ToString());
                
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    //dt.Rows.RemoveAt(0);
                    TrnsObj().Close();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            if (reader["ReturnCode"].ToString() == "0")
                            {
                                row_counter++;
                                Result = row_counter + " new rows inserted.";
                            }
                        }
                    }
                }
            }
            return Result;
        }


SQL
ALTER PROCEDURE [dbo].[sp_XXX] 
		@YYYID Int,
		@XXXID Int,
		@OwnerID Int
		
AS
BEGIN
	If(@OwnerID <> 0 And @YYYID <> 0 And @XXXID <> 0)
		Begin;
			-- Insert statements for procedure here
			Insert InTo Table_XXX(YYYID, XXXID, OwnerID)
			Values(@YYYID, @XXXID, @OwnerID);
			
			Select '0' As ReturnCode, 'Success' As Message;
		End;	
	Else
		Begin;
			Select '-1' As ReturnCode, 'Failure' As Message;
		End;
END


To tell you the truth I did a little modification to the original Stored Procedure, actually I renamed and decrease the number of the parameters, these are the only changes.
Please let me know, if there is any thing I can more get elaborated.
Posted
Updated 2-Sep-11 23:36pm
v3

On every iteration you add new parameters for the statement. Instead add the parameters only once and in the loop change the values. Something like:

C#
...
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@OwnerID", SqlDbType.Int); 
command.Parameters.Add("@XXXID", SqlDbType.Int);
command.Parameters.Add("@YYYID", SqlDbType.Int);
command.Connection = TrnsObj();

foreach (DataRow row in dt.Rows)
{
   command.Parameters["@OwnerID"].Value = Convert.ToInt32(OwnerID); 
   command.Parameters["@XXXID"].Value = Convert.ToInt32(row["XXXID"].ToString()); 
   command.Parameters["@YYYID"].Value = Convert.ToInt32(row["YYYID"].ToString()); 

   using (SqlDataReader reader = command.ExecuteReader())
   {
   ...
 
Share this answer
 
Comments
Yonathan1111 3-Sep-11 4:56am    
It worked for me.
I thank you very much.
Wendelius 3-Sep-11 4:57am    
Glad it helped :)
Firstly, don't use Parameters.Add, use Parameters.AddWithValue:
C#
command.Parameters.AddWithValue("@OwnerID", OwnerID); // Foreignkey at...
command.Parameters.Add("@XXXID",(int) row["XXXID"]);
command.Parameters.Add("@YYYID",(int) row["YYYID"]);

Secondly, we would need the definition of the stored procedure to be sure what the error is exactly.
 
Share this answer
 
Comments
Yonathan1111 3-Sep-11 5:01am    
1. I would like to know the reason why I shouldn't use Parameter.Add.
2. Can you please state what you meant by the definition of the stored procedure?
I am happy to discuss even if the the problem got resolved.

Thank you
OriginalGriff 3-Sep-11 5:10am    
1a) Because it was depreciated in favor of AddWithValue in .NET 2.0 Further support for it is not guaranteed.
1b) Because it is a whole lot easier to read.
2) At some point you have defined the procedure sp_XXX in the database: it is helpful if you show us what that expects to receive...
Yonathan1111 3-Sep-11 5:21am    
I accepted both points you depicted.
I will modify my question: I am going to add the stored procedure definition.

Thanks
Yonathan1111 4-Sep-11 5:39am    
Hi, wasn't that helpful? I have tried to improve my original question. Please let me know if there is any problem.
OriginalGriff 4-Sep-11 5:49am    
That is a lot better - It's good to know you fixed the problem!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900