I'm new to writing stored procedures. I want to write a stored procedure in SQL Server for multiple inserts from a table in ASP.net.
This is what I have so far:
Stored procedure:
ALTER procedure [dbo].[spUser]
@userName VARCHAR(50),
@membershipnr INT,
@email VARCHAR(50)
as
begin
DECLARE @returnvalue INT
IF (SELECT COUNT(*) FROM User WHERE Membershipnr = @membershipnr) > 0
BEGIN
SET @returnvalue = -1
END
ELSE
BEGIN
SET @returnvalue = 1
INSERT INTO User(Name, Membershipnr, Email)
VALUES(@userName, @membershipnr, @email)
END
SELECT @returnvalue
end
C# code:
SqlCommand cmd = new SqlCommand("spUser", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlCommand com = new SqlCommand("spTeam", conn);
com.CommandType = CommandType.StoredProcedure;
if (ddl1.SelectedValue == "2" && rb.SelectedValue == "M" && ddl2.SelectedValue == "1")
{
cmd.Parameters.AddWithValue("@userName", txtName.Text);
cmd.Parameters.AddWithValue("@userName", txtName2.Text);
cmd.Parameters.AddWithValue("@membershipnr", txtMembershipnr.Text);
cmd.Parameters.AddWithValue("@membershipnr", txtMembershipnr2.Text);
cmd.Parameters.AddWithValue("@email", txtEmail.Text);
cmd.Parameters.AddWithValue("@email", txtEmail2.Text);
com.Parameters.AddWithValue("@clubID", "1");
com.Parameters.AddWithValue("@class", "2xM");
com.Parameters.AddWithValue("@teamName", txtTeam.Text);
conn.Open();
int answer = (int)cmd.ExecuteNonQuery();
}
I get an error when I try to insert 2 columns. The error I get is:
Procedure or function spUser HAS too many arguments specified.
I think the problem is my stored procedure. But I don't know how I can specify more inserts in the procedure. I searched the internet but I couldn't find the answer.
Does somebody know how I can fix this so the error goes away and my insert will work?
Thanks in advance!
UPDATE:
I'm using a 3 table with the texboxes.
One of the tables:
<asp:Table ID="tblTwo" runat="server" class="table">
<asp:TableHeaderRow>
<asp:TableHeaderCell>Name</asp:TableHeaderCell>
<asp:TableHeaderCell>Email</asp:TableHeaderCell>
<asp:TableHeaderCell>Membershipnr</asp:TableHeaderCell>
</asp:TableHeaderRow>
<asp:TableRow>
<asp:TableCell>
<asp:TextBox ID="txtName" type="text" class="form-control" runat="server"></asp:TextBox>
</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtEmail" type="text" class="form-control" runat="server"></asp:TextBox>
</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtMembershipnr" type="text" class="form-control" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>
<asp:TextBox ID="txtName2" type="text" class="form-control" runat="server"></asp:TextBox>
</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtEmail2" type="text" class="form-control" runat="server"></asp:TextBox>
</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtMembershipnr2" type="text" class="form-control" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
Also I have a second stored procedure for CulbID
, Class
and Team
because this has to be filled into another SQL Server table.
Code of that stored procedure:
ALTER procedure [dbo].[spTeam]
@teamName VARCHAR (50)
as
BEGIN
INSERT INTO Ploeg(Name, RegistrationDate)
VALUES(@teamName, GETDATE())
END