2012-08-01 115 views
0

我宣佈2個參數爲我的存儲過程是這樣的:存儲過程導致錯誤

ALTER procedure [dbo].[paging_select] 
    @startrowindex int, 
    @maximumrows int 
as 
begin 
    select username,firstname,lastname from crudtable ; 
end 

只是傳遞價值爲以下但在執行的時候,它是導致錯誤:

SqlConnection con = new SqlConnection(getconnectionstring()); 
con.Open(); 

DataTable dt = new DataTable(); 
SqlCommand cmd = new SqlCommand(); 
SqlDataAdapter sda = new SqlDataAdapter("paging_select", con); 
cmd.CommandType = CommandType.StoredProcedure; 
cmd.Parameters.AddWithValue("@startrowindex", 1); 
cmd.Parameters.AddWithValue("@maximumrows", 3); 
// cmd.Parameters.AddWithValue("@totalrows", 1); 

cmd.Connection = con; 
sda.Fill(dt); 
sda.Dispose(); 
gridview.DataSource = dt; 
gridview.DataBind(); 
con.Close(); 

錯誤是:

Procedure or function 'paging_select' expects parameter '@startrowindex', which was not supplied.

幫助請。

回答

3

您必須將命令對象傳遞給SqlDatAdapter

SqlCommand cmd = new SqlCommand(); 
cmd.CommandText="paging_select"; 
cmd.Connection=con; 
SqlDataAdapter sda = new SqlDataAdapter(cmd); 

PS:始終使用using塊來自動處置對象(IDisposable的)。

using(SqlConnection cn=new SqlConnection()) 
{ 
    // 
} 
+0

Thanks.its working now。 你能解釋爲什麼它以前不工作嗎? – user1544975 2012-08-01 07:15:54

+1

@ user1544975:你有一個'SqlDataAdapter',但是**沒有SELECT命令**分配給那個適配器,以便實際上在調用'.Fill()'...時獲取數據。 – 2012-08-01 07:25:54