调用存储过程时,.hasRows保持“假”

时间:2012-09-25 16:56:58

标签: asp.net sql sql-server tsql stored-procedures

我有一个我正在调用的存储过程,它将从表中返回数据。

但是当我尝试使用数据填充.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 '+@FilterField+ ' like '''+@FilterQuery+''' ';
end
select @Command+=' order by '+@SortName+' '+@SortOrder

所以根据存储过程我只需要传递exhibitID,我做了。

1 个答案:

答案 0 :(得分:1)

您的存储过程看起来不完整。您可能需要添加

exec sp_executesql @command;

最后让它返回你的行。

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

找到