未提供asp.net sqldatasource参数

时间:2014-06-28 16:48:57

标签: c# asp.net sql-server parameters sqldatasource

目标:获取删除已存储的程序

您好,

我在asp.net框架4中创建了一个非常简单的CRUD(CREATE,READ,UPDATE,DELETE)测试程序,它需要有一个表id作为唯一标识符和主键。我决定让SQLServer创建唯一标识符。

程序在Webforms页面中使用SQLDatasource和FormView,可通过存储过程访问。

我可以创建,读取和更新数据但不删除数据。尝试删除时收到以下错误:

"程序或功能' DeleteTestJobID'期望参数' @ TestJobID',这是未提供的。"

我已经包含了标记,描述,异常,源错误,堆栈跟踪以及SQLTable&存储过程。

我在msdn网站上查看了过去48小时的广泛内容,但无法找到解决方案。

我将非常感谢您提供的任何意见,并提前感谢您的帮助。

-------- ASP.NET -------------------------

WEBFORMS页面:

   <asp:SqlDataSource ID="testjobSqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:TestJobSiteConnectionString %>" 
        DeleteCommand="DeleteTestJobID" DeleteCommandType="StoredProcedure" 
        InsertCommand="AddTestJobID" InsertCommandType="StoredProcedure" 
        SelectCommand="SelectAllTestJob" SelectCommandType="StoredProcedure" 
        UpdateCommand="UpdateTestJob" UpdateCommandType="StoredProcedure">
        <DeleteParameters>
            <asp:QueryStringParameter Name="TestJobID" QueryStringField="TestJobID" 
               Type="String" />
        </DeleteParameters>
        <InsertParameters>
            <asp:ControlParameter ControlID="testjobFormView1" Name="Name" 
                PropertyName="SelectedValue" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:QueryStringParameter Name="TestJobID" QueryStringField="TestJobID" 
                Type="String" />
            <asp:ControlParameter ControlID="testjobFormView1" Name="Name" 
                PropertyName="SelectedValue" Type="String" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:FormView ID="testjobFormView1" runat="server" 
        DataSourceID="testjobSqlDataSource1">
        <EditItemTemplate>
            TestJobID:
            <asp:TextBox ID="TestJobIDTextBox" runat="server" 
                Text='<%# Bind("TestJobID") %>' />
            <br />
            Name:
            <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
            <br />
            <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" 
                CommandName="Update" Text="Update" />
            &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" 
                CausesValidation="False" CommandName="Cancel" Text="Cancel" />
        </EditItemTemplate>
        <InsertItemTemplate>

            Name:
            <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
            <br />
            <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
                CommandName="Insert" Text="Insert" />
            &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" 
                CausesValidation="False" CommandName="Cancel" Text="Cancel" />
        </InsertItemTemplate>
        <ItemTemplate>
            TestJobID:
            <asp:Label ID="TestJobIDLabel" runat="server" Text='<%# Bind("TestJobID") %>' />
            <br />
            Name:
            <asp:Label ID="NameLabel" runat="server" Text='<%# Bind("Name") %>' />
            <br />
            <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" 
                CommandName="Edit" Text="Edit" />
            &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" 
                CommandName="Delete" Text="Delete" />
            &nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" 
                CommandName="New" Text="New" />
        </ItemTemplate>
    </asp:FormView>

错误消息:

  

描述:执行期间发生了未处理的异常   当前的网络请求。请查看堆栈跟踪了解更多信息   有关错误的信息以及它在代码中的起源。

     

异常详细信息:System.Data.SqlClient.SqlException:过程或   功能&#39; DeleteTestJobID&#39;期望参数&#39; @ TestJobID&#39;,这是   没提供。

     

来源错误:

     

执行期间生成了未处理的异常   当前的网络请求。有关的来源和位置的信息   可以使用下面的异常堆栈跟踪来识别异常。

     

[SqlException(0x80131904):过程或函数&#39; DeleteTestJobID&#39;   期望参数&#39; @ TestJobID&#39;,这是未提供的。]
  System.Data.SqlClient.SqlConnection.OnError(SqlException异常,   Boolean breakConnection,Action 1 wrapCloseInAction) +1767866
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction)   +5352418 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject   stateObj,Boolean callerHasConnectionLock,Boolean asyncClose)+244
  System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,   SqlCommand cmdHandler,SqlDataReader dataStream,   BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject   stateObj,布尔&amp; dataReady)+1691
  System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,   RunBehavior runBehavior,String resetOptionsString)+269
  System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(的CommandBehavior   cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean   async,Int32超时,任务&amp; task,Boolean asyncWrite,SqlDataReader   ds)+1406
  System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior   cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String   方法,TaskCompletionSource 1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +177
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource
1   completion,String methodName,Boolean sendToPipe,Int32 timeout,   Boolean asyncWrite)+205
  System.Data.SqlClient.SqlCommand.ExecuteNonQuery()+160
  System.Web.UI.WebControls.SqlDataSourceView.ExecuteDbCommand(的DbCommand   命令,DataSourceOperation操作)+380
  System.Web.UI.WebControls.SqlDataSourceView.ExecuteDelete(IDictionary的   key,IDictionary oldValues)+568
  System.Web.UI.DataSourceView.Delete(IDictionary keys,IDictionary   oldValues,DataSourceViewOperationCallback回调)+84
  System.Web.UI.WebControls.FormView.HandleDelete(String commandArg)   +793 System.Web.UI.WebControls.FormView.HandleEvent(EventArgs e,Boolean causeValidation,String validationGroup)+519
  System.Web.UI.WebControls.FormView.OnBubbleEvent(Object source,   EventArgs e)+89 System.Web.UI.Control.RaiseBubbleEvent(Object   来源,EventArgs args)+37
  System.Web.UI.WebControls.FormViewRow.OnBubbleEvent(Object source,   EventArgs e)+80 System.Web.UI.Control.RaiseBubbleEvent(Object   来源,EventArgs args)+37
  System.Web.UI.WebControls.LinkBut​​ton.OnCommand(CommandEventArgs e)   +121 System.Web.UI.WebControls.LinkBut​​ton.RaisePostBackEvent(String eventArgument)+156
  System.Web.UI.WebControls.LinkBut​​ton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串   eventArgument)+10
  System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler   sourceControl,String eventArgument)+13
  System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)   +9528682 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)   1724

---------- SQLSERVER -------

---------- CREATE TABLE ----

USE [TestJobSite]
GO

/****** Object:  Table [dbo].[TestJob]    Script Date: 28/06/2014 17:27:55 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[TestJob](
    [TestJobID] [uniqueidentifier] NULL,
    [Name] [nchar](100) NOT NULL
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[TestJob] ADD  CONSTRAINT [DF_TestJob_TestJobID]  DEFAULT (newid()) FOR [TestJobID]
GO

----------- STORED PROC ----- ----------- ---------- DELETE

USE [TestJobSite]
GO

/****** Object:  StoredProcedure [dbo].[DeleteA]    Script Date: 27/06/2014 18:19:28 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

        ALTER PROCEDURE [dbo].[DeleteTestJobID]
        @TestJobID nchar(100)
        AS

        DELETE FROM
        TestJob
        WHERE
        TestJobID=@TestJobID

GO

---------存储过程------ --------- ADD ENTRY -------------

USE [TestJobSite]
GO

/****** Object:  StoredProcedure [dbo].[AddAgency]    Script Date: 27/06/2014 18:02:03 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

        ALTER PROCEDURE [dbo].[AddTestJobID]
        -- Add the parameters for the stored procedure here
        @Name nchar(100)

        AS
        BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;

        -- Insert statements for procedure here
        INSERT INTO TestJob (Name)

        VALUES (@Name);
        END

GO

---------存储过程----- --------- READ -----------------

USE [TestJobSite]
GO

/****** Object:  StoredProcedure [dbo].[SelectAllTestJob]    Script Date: 27/06/2014 18:22:46 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

        CREATE PROCEDURE [dbo].[SelectAllTestJob]
        AS

        SELECT *  FROM
        TestJob

GO

--------- STORED PROC --- --------- -------- UPDATE

/****** Object:  StoredProcedure [dbo].[UpdateTestJob]    Script Date: 27/06/2014 18:24:49 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

        CREATE PROCEDURE [dbo].[UpdateTestJob]
        @TestJobID nchar(100),
        @Name nchar(100)

        AS

        UPDATE TestJob

        SET
        Name=@Name

        WHERE 
        TestJobID=@TestJobID;
GO

1 个答案:

答案 0 :(得分:0)

已解决:编辑问题是否存在问题?删除功能

  1. 编辑功能不起作用!原因是SELECT STORED PROCEDURE不正确;它将第一条记录放在表格中。如果更改URL中的uniqueidentifier,它将始终显示表中的第一条记录,然后将您在(名称)文本框中输入的内容更新为此。
  2. 要解决这个问题,只需将select stored proc更改为:

    SET ANSI_NULLS ON
    GO
    
    SET QUOTED_IDENTIFIER ON
    GO
    
    
            CREATE PROCEDURE [dbo].[SelectTestJob]
            @TestJobID nchar(100)
            AS
    
            SELECT *  FROM
            TestJob
            WHERE
            TestJobID=@TestJobID
    
    
    GO
    

    这将从表格中仅选择一条记录。确保sqldatasource中的标记现在看起来像这样;注意SELECT语句及其类型,然后检查SELECT PARAMETER。参数必须是查询字符串;因为它正在从URL中读取。

    <asp:SqlDataSource ID="TestJobSqlDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:TestJobSiteConnectionString %>" 
        DeleteCommand="DeleteTestJob" DeleteCommandType="StoredProcedure" 
        InsertCommand="AddTestJobID" InsertCommandType="StoredProcedure" 
        SelectCommand="SelectTestJob" SelectCommandType="StoredProcedure" 
        UpdateCommand="UpdateTestJob" UpdateCommandType="StoredProcedure">
        <DeleteParameters>
            <asp:QueryStringParameter Name="TestJobID" QueryStringField="TestJobID" 
                Type="String" />
        </DeleteParameters>
        <InsertParameters>
            <asp:ControlParameter ControlID="testjobFormView1" Name="Name" 
                PropertyName="SelectedValue" Type="String" />
        </InsertParameters>
        <SelectParameters>
            <asp:QueryStringParameter Name="TestJobID" QueryStringField="TestJobID" 
                Type="String" />
        </SelectParameters>
        <UpdateParameters>
            <asp:QueryStringParameter Name="TestJobID" QueryStringField="TestJobID" 
                Type="String" />
            <asp:ControlParameter ControlID="testjobFormView1" Name="Name" 
                PropertyName="SelectedValue" Type="String" />
        </UpdateParameters>
    </asp:SqlDataSource>
    
    1. DELETE程序没有拿起@TestJobID,直到我在FormView中检查了ItemTemplate之后我才弄明白。有2个引用(表单标签和URL);我只是删除了在表单中显示为标签的TestJobID,保存并重建并且它有效!修改后的表格现在显示在下面。
    2. <ItemTemplate>
          TestJobID:
      
          <br />
          Name:
          <asp:Label ID="NameLabel" runat="server" Text='<%# Bind("Name") %>' />
          <br />
          <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" 
              CommandName="Edit" Text="Edit" />
          &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" 
              CommandName="Delete" Text="Delete" />
          &nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" 
              CommandName="New" Text="New" />
      </ItemTemplate>
      

      感谢Steve Wellens和StackOverflow的帮助。我希望这个现在正在运行的小CRUD测试程序对那些热衷于使用uniqueidentifiers的asp.net开发人员有用。