views:

501

answers:

4

I'm writing an ASP.NET(C#) application in Visual Studio 2008 and connecting to SQLExpress 2005.

While trying to update a FormView control bound to an SqlDataSource by using a parameterized stored procedure, I constantly get an error screen saying "too many arguments specified".

I have tried clearing the list and adding all the parameters manually before calling the DataSource.Update() method. I have tested with a breakpoint and immediately before the Update method fires, the UpdateParameters collection holds the 8 arguments I have specified in my stored procedure so I know my collection conforms to what I asked for.

Passing in update commands of type="text" that contain an EXEC statement will work but I need it to work by calling the procedure itself.

Has anyone else run into these "extra arguments" or am I playing EPR and chasing imaginary variables?

CREATE PROC spUpdateUserProfile
 @UserNameVar nvarchar(256),
 @DisplayNameVar varchar(30),
 @FNameVar varchar(20),
 @LNameVar varchar(20),
 @EmailVar varchar(30)=NULL,
 @LocationVar varchar(100)=NULL,
 @BirthdateVar smalldatetime=NULL,
 @BiographyVar varchar(2000)=NULL

AS

UPDATE UserProfile
SET UserDisplayName = @DisplayNameVar,
 UserFName = @FNameVar,
 UserLName = @LNameVar,
 UserSharedEmail = @EmailVar,
 UserLocation = @LocationVar,
 UserDOB = @BirthdateVar,
 UserBiography = @BiographyVar
WHERE UserProfile.UserID = 
(SELECT UserProfile.UserID FROM UserProfile
JOIN aspnet_Users ON UserProfile.UserID = aspnet_Users.UserId
WHERE aspnet_Users.UserName = @UserNameVar)
A: 

Just a shot in the dark until we can see some code like James asked, but are you settings the DataKeyNames attribute? When I was getting started with FormView and GridView I manually added the primary key value using a hidden field and had the DataKeyNames attribute sent and I think that caused the value to be sent to the stored procedure twice instead of once.

Just a guess

EDIT: Have you tried

    UPDATE UserProfile
    SET UserDisplayName = @DisplayNameVar,
      UserFName = @FNameVar,
      UserLName = @LNameVar,
      UserSharedEmail = @EmailVar,
      UserLocation = @LocationVar,
      UserDOB = @BirthdateVar,
      UserBiography = @BiographyVar
    WHERE UserProfile.UserID = aspnet_Users.UserId
      AND aspnet_Users.UserName = @UserNameVar
Justin C
I've looked into that field and have tried making sure it is blank. I've even cleared in manually in codebehind and tried permutations where I put the value in it and left it out of my other declarations. No avail.
hqrsie
A: 

change line: WHERE UserProfile.UserID =
for: WHERE UserProfile.UserID IN

Jack
that provides the same response and is essentially the same statement. I've tested my SP directly in SQL Management studio and it behaves as expected. The breakdown occurs passing the command from the ASP.NET application
hqrsie
A: 

I've just encountered this as well but I have managed to sort it.

My update was from a grid.
My grid was populated from another stored procedure. In that Select stored procedure I changed the field names to more user friendly ones e.g.

select AU.UserName [Member],

Later, when I checked, the Update command was passing all the parameters required by the update stored procedure and extra ones corrsponding to the re-named fields.

I removed the re-naming from the Select procedure and updated the SqlDataSource. Now only the correct number of fields get passed.

Then I just renamed the HeaderText tag of the GridBoundColumn.e.g.

HeaderText="Member"

I am using a RadGrid rather than the standard GridView but it seems to work there as well.

I checked the number of passed parameters using:

protected void  SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
    for (int x = 0; x <= e.Command.Parameters.Count - 1;x++ )
    {
        string Type = e.Command.Parameters[x].GetType().ToString();
        string Value = e.Command.Parameters[x].ToString();
    }
}

Hope this helps

Richard210363
A: 

@Justin C

OMG you just saved my....well you did me a great service.

The Datakey (it turns out) was screwing up my update SP and I couldn't figure it out... I never would have thought of the datakey as the issue but having read your tip I removed the Datakey and bingo...all is well and I can finish this silly project!!

THANK YOU!!!

RandomDude
RandomDude, glad to hear that the answer helped! Feel free to vote it up once you have the reputation points.
Justin C