我在SQL服務器的查詢,如下:如何在asp.net中執行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>
現在我改變了我這樣的查詢受理日期參數:
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
從那裏你需要開始日期和結束日期參數的值???有沒有文本框?如果sp是startdate,那麼sp只返回一個column_date,那麼enddate是哪裏? – 2012-08-10 19:53:43
是的我有一個.html文件,其中有兩個文本框名稱startdate和enddate。 我添加了由mosaklevN給出的代碼,但是當我在文本框中輸入日期並提交時,只有該行顯示在網頁'GridView示例:執行StoredProcedure'它不顯示錶格中的數據。 – user1449596 2012-08-11 05:42:53
我編輯兩個文本框中的代碼,見下文。 – moskalevN 2012-08-11 06:58:40