2016-07-24 123 views
0
SqlConnection conn = getConnection(); 
SqlCommand cmd = new SqlCommand(); 

cmd.CommandType = CommandType.StoredProcedure; 
cmd.CommandText = "SP_PSLA_SEARCH"; //The stored procedure gets added 
cmd.CommandTimeout = 0; 
cmd.Connection = conn; 

// Start adding the parameters of the stored procedure 
cmd.Parameters.AddWithValue("@usrnm", thisUser.Username); 

int constit = 0; 

if (thisUser.Constituencies.Count > 0) 
{ 
    foreach (KeyValuePair<int, string> kp in thisUser.Constituencies) 
    { 
     if (kp.Value == ddlConstituency.SelectedValue.ToString()) 
     { 
      constit = kp.Key; 
      break; 
     } 
    } 
} 

cmd.Parameters.AddWithValue("@cnstncy", constit); 

string pdval = null; 
int valtype = 0; 

if (rbsearchradios.SelectedIndex == 0) 
{ 
    try 
    { 
     pdval = searchVal; 
     cmd.Parameters.AddWithValue("@Search", DBNull.Value); 
     cmd.Parameters.AddWithValue("@pd", int.Parse(pdval)); 
     cmd.Parameters.AddWithValue("@type", valtype); 
    } 
    catch 
    { 
     System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "stop", "alert('Invalid PD Number Supplied! Please Provide A Valid Submission.');", true); 
     return; 
    } 
} 
else 
{ 
    valtype = 1; 
    cmd.Parameters.AddWithValue("@Search", searchVal); 
    cmd.Parameters.AddWithValue("@pd", DBNull.Value); 
    cmd.Parameters.AddWithValue("@type", valtype); 
} 

cmd.Parameters.AddWithValue("@app", 1); 

conn.Open();       

// Creates Dataadapter for execution 
SqlDataAdapter dp2 = new SqlDataAdapter(cmd); 
DataSet ds = new DataSet(); 

dp2.Fill(ds, "name"); 

我想存儲過程的參數和有這個存儲過程執行並獲得這個結果到一個數據集,但我什麼也沒有得到..字面意思。沒有例外,只是沒有存儲過程的結果。爲什麼執行時返回沒有結果?

這是存儲過程:

DECLARE @return_value int 

EXEC @return_value = [dbo].[SP_PSLA_SEARCH] 
      @usrnm = N'tstone', 
      @cnstncy = 55, 
      @Search = N'primary', 
      @pd = NULL, 
      @type = 1, 
      @app = 1 

SELECT 'Return Value' = @return_value 
GO 
+0

檢查從內部到外部的對象。你正在打電話給USP,但是它能工作嗎?如果使用的謂詞沒有匹配,則不會得到任何結果,也不會發布錯誤。它只是沒有匹配的行。 –

+0

您可以查看從sql profiller發送的sql語句以瞭解哪些參數值,您可能會發現錯誤在哪裏。參數值基於if。嘗試在dp2.fill後寫入參數值 –

回答

0

要解決:

  1. 確保什麼樣的價值觀有各參數,並直接對SQL Server Management Studio中的數據庫執行相同的查詢。
  2. 檢查,如果你正確使用結果從數據集(目前還不清楚從代碼)

在一般情況下,你也可以儘量簡化,使你的代碼更加清晰:

  • 的塊返回並且if (rbsearchradios.SelectedIndex == 0)可以在開始時移動,創建SqlCommand然後中斷
  • 如果SP只返回單個值是沒有意義的,則可以使用ExecuteScalar()方法,該方法更快更簡單。
相關問題