Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在asp.net中执行sql server存储过程输入参数?_Asp.net_Sql Server 2008 - Fatal编程技术网

如何在asp.net中执行sql server存储过程输入参数?

如何在asp.net中执行sql server存储过程输入参数?,asp.net,sql-server-2008,Asp.net,Sql Server 2008,我在sql server中有一个查询,如下所示: Alter procedure testprocedure As Begin select column_date, sum(qty1), sum(qty2), sum(qty3) from table1 End Go 我使用下面的代码访问asp.net中的存储过程。即使我从另一个来源复制了这段代码,但它对我有效 <%@ Page Language="C#" AutoEventWireup="true" %> <!DOC

我在sql server中有一个查询,如下所示:

Alter procedure testprocedure
As
Begin
select column_date, sum(qty1), sum(qty2), sum(qty3) from table1
End
Go
我使用下面的代码访问asp.net中的存储过程。即使我从另一个来源复制了这段代码,但它对我有效

<%@ Page Language="C#" AutoEventWireup="true" %>  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<script runat="server">  

</script>  
<html xmlns="http://www.w3.org/1999/xhtml" >  
<head id="Head1" runat="server">  
</head>  
<body>  
<form id="form1" runat="server">  
<div>  
    <h2 style="color:Navy; font-style:italic;">GridView Example: Execute StoredProcedure</h2>  
    <asp:SqlDataSource   
        ID="SqlDataSource1"  
        runat="server"  
        ConnectionString="<%$ ConnectionStrings:databasename %>"  
        SelectCommand="testprocedure"  
        SelectCommandType="StoredProcedure"  
        >  
    </asp:SqlDataSource>  
    <asp:GridView   
        ID="GridView1"  
        runat="server"  
        DataSourceID="SqlDataSource1"  
        AutoGenerateColumns="true"  
        AllowPaging="true"  
        PageSize="31"  
        BorderColor="Salmon"  
        Font-Names="Comic Sans MS"  
        Width="650"  
        >  
        <HeaderStyle BackColor="IndianRed" ForeColor="Snow" Height="45"/>  
        <RowStyle BackColor="DarkSalmon" ForeColor="Snow" Font-Italic="true" />  
        <PagerStyle   
            Height="45"   
            HorizontalAlign="Right"   
            BackColor="RosyBrown"  
            Font-Bold="true"  
            Font-Size="X-Large"  
            ForeColor="Snow"  
            />  
        <PagerSettings Mode="Numeric" />  
    </asp:GridView>  
</div>  
</form>  
</body>  
</html> 
请注意,“我的日期”列的数据类型为nchar(8)。 现在,我想修改上面发布的asp.net代码,以接受此日期参数。我不知道如何编辑代码,因为我是非常新的asp.net并且还在学习

错误:

 Could not find control 'textboxStartDate' in ControlParameter 'startdate'.

 Description: An unhandled exception occurred during the execution of the current web request.
 Please review the stack trace for more information about the error and where it originated in 
 the code. 

 Exception Details: System.InvalidOperationException: Could not find control 'textboxStartDate'      
 in ControlParameter 'startdate'.

 Source Error: 

 An unhandled exception was generated during the execution of the current web request. 
 Information regarding the origin and location of the exception can be identified using the   
 exception stack trace below.

  Stack Trace: 


 [InvalidOperationException: Could not find control 'textboxStartDate' in ControlParameter          
 'startdate'.]
  System.Web.UI.WebControls.ControlParameter.Evaluate(HttpContext context, Control control)        
 +2106934
 System.Web.UI.WebControls.Parameter.UpdateValue(HttpContext context, Control control) +50
  System.Web.UI.WebControls.ParameterCollection.UpdateValues(HttpContext context, Control
 control) +113
 System.Web.UI.WebControls.SqlDataSource.LoadCompleteEventHandler(Object sender, EventArgs e) +46
 System.Web.UI.Page.OnLoadComplete(EventArgs e) +9008578
 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean 
  includeStagesAfterAsyncPoint) +2350


FYI startdate和enddate列不在gridview中。请更正您的代码。如何在gridview中添加它们?从何处获取startdate和enddate参数的值???有文本框吗?如果是startdate,则sp只返回一个列的日期,那么enddate在哪里???是的,我有一个.html文件,其中有两个文本框名为startdate和enddate。我添加了mosaklevN给出的代码,但当我在文本框中输入日期并提交时,网页
GridView示例:Execute StoredProcess
中只显示这一行,它不显示表中的数据。我编辑两个文本框中的代码,请看下面。谢谢moskalevN,但现在我得到了错误,因为我在上面添加了你的新代码后,只需添加两个名为textboxStartDate和textboxEndDate的文本框。我相信你可以
 Could not find control 'textboxStartDate' in ControlParameter 'startdate'.

 Description: An unhandled exception occurred during the execution of the current web request.
 Please review the stack trace for more information about the error and where it originated in 
 the code. 

 Exception Details: System.InvalidOperationException: Could not find control 'textboxStartDate'      
 in ControlParameter 'startdate'.

 Source Error: 

 An unhandled exception was generated during the execution of the current web request. 
 Information regarding the origin and location of the exception can be identified using the   
 exception stack trace below.

  Stack Trace: 


 [InvalidOperationException: Could not find control 'textboxStartDate' in ControlParameter          
 'startdate'.]
  System.Web.UI.WebControls.ControlParameter.Evaluate(HttpContext context, Control control)        
 +2106934
 System.Web.UI.WebControls.Parameter.UpdateValue(HttpContext context, Control control) +50
  System.Web.UI.WebControls.ParameterCollection.UpdateValues(HttpContext context, Control
 control) +113
 System.Web.UI.WebControls.SqlDataSource.LoadCompleteEventHandler(Object sender, EventArgs e) +46
 System.Web.UI.Page.OnLoadComplete(EventArgs e) +9008578
 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean 
  includeStagesAfterAsyncPoint) +2350
<asp:SqlDataSource   
        ID="SqlDataSource1"  
        runat="server"  
        ConnectionString="<%$ ConnectionStrings:databasename %>"  
        SelectCommand="testprocedure"  
        SelectCommandType="StoredProcedure">
  <SelectParameters>
     <--for hardcode values  -->
     <asp:Parameter Name="startdate" Type="String" />
     <asp:Parameter Name="enddate" Type="String"/>
     <!-- or values from controls  -->
      <asp:ControlParameter Name="startdate" ControlID="textboxStartDate" PropertyName="Text" /> 
     <asp:ControlParameter Name="enddate" ControlID="textboxEndDate" PropertyName="Text" />               
 </SelectParameters>
</asp:SqlDataSource>

  <asp:TextBox ID="textboxStartDate" runat="server"></asp:TextBox>
    <asp:TextBox ID="textboxEndDate" runat="server"></asp:TextBox>