0

私は以下のようにSQLサーバーにクエリを持っています:

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> 

今、私はパラメータで日付を受け入れるためにこのようにクエリを変更しました:

Alter procedure testprocedure @startdate nchar(8), @enddate nchar(8)
As
Begin
select column_date, sum(qty1), sum(qty2), sum(qty3) from table1
where column_date between @startdate and @enddate
End
Go

私の日付列のデータ型は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
4

1 に答える 1

0
<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>
于 2012-08-10T19:38:19.150 に答える