Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
|
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 19th, 2008, 06:05 AM
Registered User
 
Join Date: Jun 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Why doesn't this Insert Procedure work.

As someone new brand new to asp.net I am trying to do something relatively simple to avoid inserting duplicate records.

I have a stored procedure - code at the end - that checks whether a department of the same name exists in the database and if not creates a new row with the new departmental name (the departmentID is set as the pk and increments automatically).

But when I call the stored procedure via the InsertCommand within a gridview I recieve the following message:

System.Data.SqlClient.SqlException: Procedure or function InsertDepartment has too many arguments specified.

I'm sure I'm missing something obvious. Am I making mistakes in how I am specifying the stored proc or missing command add parameter?

Any help would be very much appreciated.

Cheers

Page mark up

<asp:DetailsView ID="DetailsView2" runat="server"
                AutoGenerateInsertButton="True" AutoGenerateRows="False"
                DataSourceID="SqlDataSource1" DefaultMode="Insert" Height="50px"
                Width="329px">
                <Fields>
                    <asp:BoundField DataField="Department" HeaderText="Department"
                        SortExpression="Department" />
                </Fields>
            </asp:DetailsView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                ConnectionString="<%$ ConnectionStrings:LinwoodSchoolWorksConnectionStri ng %>"
                SelectCommand="SELECT Department FROM Departments order by department asc"
                InsertCommand="InsertDepartment" InsertCommandType="StoredProcedure" >

                <InsertParameters>
                    <asp:Parameter Name="DepartmentName" Type="String" />
                </InsertParameters>
            </asp:SqlDataSource>

Stored Procedure

ALTER PROCEDURE InsertDepartment
(
  @DepartmentName varchar(50)
)
AS
IF EXISTS(SELECT 'True' FROM Departments WHERE Department = @DepartmentName)
BEGIN
  SELECT 'This record already exists!'
END
ELSE
BEGIN
  SELECT 'Record Added'
  INSERT into Departments(Department) VALUES(@DepartmentName)
END

 
Old June 19th, 2008, 09:21 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

This looks OK to me, but I'm not an expert on using the SqlDataSource.

Can you try doing a SQL trace to see what the exact call to SQL is from the program? This may shed some light on the problem.

-Peter
compiledthoughts.com
 
Old June 19th, 2008, 09:59 AM
Registered User
 
Join Date: Jun 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default


javascript:insertsmilie(':D')

Thanks for the suggestion, but Duh! I believed the answer was straight forward. I managed to solve the problem by renaming the boundfield associated with the insert command in the details view like

<asp:DetailsView ID="DetailsView2" runat="server"
                AutoGenerateInsertButton="True" AutoGenerateRows="False"
                DataSourceID="SqlDataSource1" DefaultMode="Insert" Height="50px"
                Width="329px">
                <Fields>
                    <asp:BoundField DataField="DepartmentName" HeaderText="Department" />

                </Fields>
            </asp:DetailsView>

 This is same name as the input parameter in the stored proc, rather than the name of the column in the database ie. Department. This appears to resolve the problem of the "too many arguments passed to the procedure" error message although I'm not sure why, especially given number of parameters in the select and insert commands are the same. Mmmm!



David






Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert won't work Tod1 Access VBA 1 December 18th, 2006 10:50 PM
Insert Procedure cf2006 BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 4 November 6th, 2006 01:58 PM
Insert method doesnot work shoakat Classic ASP Databases 3 November 22nd, 2004 07:46 PM
How do I insert stored procedure through C# nishim.attreja VB How-To 1 September 18th, 2004 01:24 AM
Using an insert stored procedure morpheus VB How-To 3 August 7th, 2003 08:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.