Parameterized stored procedure returning "too many arguments specified"
I'm writing an ASP.NET(C#) application in Visual Studio 2008 and connecting to SQLExpress 2005.
While trying to update a FormView control b开发者_运维百科ound 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)
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
change line:
WHERE UserProfile.UserID =
for:
WHERE UserProfile.UserID IN
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
精彩评论