2012-09-25 21 views
1

我有一個存儲過程,我正在調用哪個將從表中返回數據。當調用存儲過程時,.hasRows一直保持「false」

但是,當我嘗試用數據填充.aspx時,它跳過我的方法,因爲我的方法基於讀取器是否檢測到行。

這裏是我的方法:

private void editExhibit(int expenseID)//new 
{ 
    saveExhibitBtn.Text = "Update Exhibit"; 

    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["OSCIDConnectionString"].ToString()); 
    SqlCommand cmd = new SqlCommand("p_CaseFiles_Exhibits_RetrieveExhibitDetails", conn); 
    cmd.CommandType = CommandType.StoredProcedure; 
    //cmd.Parameters.AddWithValue("@ExhibitID", expenseID); 
    cmd.Parameters.Add(new SqlParameter("@ExhibitID", SqlDbType.Int)); 
    cmd.Parameters["@ExhibitID"].Value = expenseID; 

    bool hasAttachments = false; 
    string investigatorID = ""; 
    //bool alreadyInvoiced = false; 
    bool isExpenseOwner = false; 
    string fileID = "-1"; 

    try 
    { 
     conn.Open(); 
     var reader = cmd.ExecuteReader(); 
     if (reader.HasRows)//////////////////////craps out here bcause hasRows is false.... 
     { 
      reader.Read(); 
      fileID = reader["FileID"].ToString(); 
      ddlCaseFiles.SelectedValue = fileID; 
      ddlCaseFiles.Enabled = false; 
      // retrieve exhibit details here 
      hasAttachments = (bool)reader["HasAttachments"]; 
      investigatorID = reader["InvestigatorID"].ToString(); 
      if (Session["InvestigatorID"].ToString() == investigatorID) 
      { 
       isExpenseOwner = true; 
      } 
      txtDateReceived.Value = reader["SeizeDate"].ToString(); 
      ddlReceivedBy.SelectedValue = reader["SeizedByInvestigatorID"].ToString(); 
      txtTimeReceived.Value = reader["SeizeTime"].ToString(); 
      txtWhyHowReceived.Value = reader["SeizeAuthority"].ToString(); 
      txtReceivedLocation.Value = reader["SeizeLocation"].ToString(); 
      txtOurItemNum.Value = reader["NewExhibitOutItemNumber"].ToString();//////////// 
      txtTheirItemNum.Value = reader["ClientItemNum"].ToString(); 
      txtBagNum.Value = reader["BagNumber"].ToString(); 
      txtBoxNum.Value = reader["BoxNumber"].ToString(); 
      txtComment.Value = reader["ExhibitDecriptionPlainText"].ToString(); 
     } 
    } 
    catch (SqlException ex) 
    { 
     ErrorLogger.Log(ex.Number, "NewExhibit.aspx - editExhibit - Retrieve Details", ex.Message); 
    } 

這裏是我的存儲過程:

SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
ALTER procedure [dbo].[p_CaseFiles_Exhibits_RetrieveExhibitDetails] 
    @FilterField nvarchar(max)=null 
, @FilterQuery nvarchar(max)=null 
, @SortName nvarchar(max)='SeizeDate, SeizeTime ' 
, @SortOrder nvarchar(max)='desc' 
, @ExhibitID int 
as 
SET CONCAT_NULL_YIELDS_NULL OFF 
declare @Command nvarchar(max) 
Select @Command = 'select E.ExhibitID,convert(nvarchar,SeizeDate,111) as ''SeizeDate'',SeizeTime,ExhDesc,E.InvestigatorID as ''EnteredBy'' 
    ,E.SeizedByInvestigatoID as ''SeizedBy'',SBI.ActiveInvestigator, SBI.FName+'' '' + SBI.LName as ''SeizedByName'', E.FileID,[FileName] 
    ,Investigators.FName,Investigators.LName,SzAuthority,Location,ItemID,SubItemID1,SubItemID2,SubItemID3,PageSerial,ClientItemNum,Privileged 
    ,Private,E.HasAttachments,ItemEntryGradeID,BagNumber,BoxNumber,PL.PropertyId,P.PropertyTypeID,P.PropertyMakeID,P.PropertyModelID,SerialNumber,ColorID 
    ,cast(ItemID as varchar)+''-''+cast(SubItemID1 as varchar)+''-''+cast(SubItemID2 as varchar)+''-''+cast(SubItemID3 as varchar) as ''ItemNumber'',StoredLocally 
    from CaseFileExhibits E 
    join Investigators on E.InvestigatorID = Investigators.InvestigatorID 
    join Investigators SBI on SBI.InvestigatorID=E.SeizedByInvestigatoID 
    join CaseFiles on E.FileID = CaseFiles.FileID 
    left join CaseFileExhibitPropertyLink PL on E.ExhibitID=PL.ExhibitID 
    left join Element09a_Properties P on P.PropertyID=PL.PropertyId 
    left join ElementPropertyTypes PT on PT.PropertyTypeID=P.PropertyTypeID 
    left join ElementPropertyMakes PM on PM.PropertyMakeID=P.PropertyMakeID 
    left join ElementPropertyModels PMD on PMD.PropertyModelID=P.PropertyModelID 
    where E.ExhibitID='+convert(nvarchar,@ExhibitID); 
if(@FilterQuery is not null) 
begin 
    select @Command+=' and '[email protected]+ ' like '''[email protected]+''' '; 
end 
select @Command+=' order by '[email protected]+' '[email protected] 

所以根據存儲過程我只需要在exhibitID傳遞,我做到了。

+1

愚蠢的問題中找到,但使用SQL Management Studio中有結果運行StoredProcedure的? – tomasmcguinness

+0

hehe謝謝,我有。是的,它的確如此 – user1084319

+0

請告訴你如何在sql管理工作室中調用存儲過程。您可能必須使用命名參數調用它,如:exec p_CaseFiles_Exhibits_RetrieveExhibitDetails @ExhibitID ='SOMETHING'。另外,請參閱下面的我的回答 –

回答

1

您的存儲過程看起來不完整。您可能需要添加

exec sp_executesql @command; 

最後讓它返回您的行。

有關sp_executesql的信息,可以在http://msdn.microsoft.com/en-us/library/ms188001.aspx

+0

我會嘗試 – user1084319

+0

工作。你能解釋一下SP_ExecuteSQL嗎?我必須等6分鐘才能將答案標記爲答案 – user1084319

+1

總之,您的sql有一個字符串,在您的情況下,所有SP_executesql都會執行保存在該字符串中的sql。我將MSDN參考添加到了我的答案中 –