Link to home
Start Free TrialLog in
Avatar of dhtml
dhtml

asked on

Error with insert using stored procedure with GridView and SqlDataSource controls

Hello!

I'm having problems adding a record using a stored procedure with the GridView and SQlDataSource controls. When I click the "Add" button, I get the following error:

Procedure or Function 'spAddCompetitor' expects parameter '@name', which was not supplied.

I have provided the source code below.

Any thanks is much appreciated.

Simon

------------
ASPX FILE:
-------------

        <div id="Content">
            <h1>Players</h1>
            <asp:GridView ID="grdPlayers" runat="server" AutoGenerateColumns="False" DataSourceID="TournamentPlayers" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" DataKeyNames="CompetitorID" >           
            <Columns>                
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField Visible="False" DataField="CompetitorID" />
            </Columns>
           
            </asp:GridView>
            <h2>Add Player</h2>
            <asp:SqlDataSource ID="TournamentPlayers" runat="server" ConnectionString="<%$ ConnectionStrings:TournamentsConnectionString %>"
                SelectCommand="SELECT [CompetitorID], [Name] FROM [Competitor]"
                InsertCommand="spAddCompetitor"                
                InsertCommandType="StoredProcedure">
                <InsertParameters>                    
                    <asp:FormParameter FormField="txtPlayerName" Name="@name" Type="string" Size="50" />                  
                </InsertParameters>
            </asp:SqlDataSource>
            <table>
                <tr>
                    <td>Name:</td>
                    <td><asp:TextBox runat="server" ID="txtPlayerName"/></td>
                    <td><asp:Button runat="server" ID="btnAddPlayer" Text="Add" OnClick="btnAddPlayer_Click"/></td>
                </tr>
            </table>
        </div>  

-------------------------
STORED PROCEDURE:
-------------------------

ALTER PROCEDURE dbo.spAddCompetitor       
      @name varchar(50)
AS
      SET NOCOUNT ON
      
      insert into Competitor([Name])
      values (@name)
      
      RETURN

---------------------------------
"ADD" BUTTON CLICK CODE:
---------------------------------

protected void btnAddPlayer_Click(object sender, EventArgs e)
    {        
        TournamentPlayers.Insert();        
    }
ASKER CERTIFIED SOLUTION
Avatar of manicsquirrel
manicsquirrel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dhtml
dhtml

ASKER

Thanks for that - I knew it would be something simple.

Unfortunately, I've got another problem now (grid doesn't show the new row unless I re-visit the page), but that's a separate issue so I'll post that question if I can't find the answer myself.

Simon