I am trying to use a SqlDataAdapter
to fill a DataTable
, which I use as a data source to a DataGrid
. The query is fine, at least when I run it manually in SSMSE. When I do the fill operation, the table gets the right columns, but no rows. The most frustrating part of this is that the code is identical (with a different query) on a different page. I cannot post the query, but my initialization code looks like this:
SqlCommand areaDAC = new SqlCommand (areaQuery, connection);
areaDAC.Parameters.Add (new SqlParameter ("@param",
System.Data.SqlDbType.NVarChar, 50));
m_areaDataAdapter = new SqlDataAdapter (areaDAC);
Then to use it:
m_areaDataAdapter.SelectCommand.Parameters["@param"].Value = "Filter Val";
DataTable table = new DataTable ();
m_areaDataAdapter.Fill (table);
At this point, table has the right number of columns and no rows. I know the parameters are being added correctly, I know that data exists for the given query.
Update (as provided by Nik in a comment):
SELECT * FROM
(SELECT
ROW_NUMBER() OVER(ORDER BY DateAndTime DESC) AS rowNum,
areaName, stationName, lineName, DateAndTime,
Element, Description
FROM
sdrReportArea, sdrReportStation, sdrReportTLine,
SDRSequenceEvents, SDRSequenceStates
WHERE
sdrReportArea.areaID = sdrReportStation.stationID
AND sdrReportStation.stationID = sdrReportTLine.stationID
AND sdrReportTLine.lineID = SDRSequenceEvents.LineID
AND SDRSequenceEvents.StateID = SDRSequenceStates.StateID
AND DateAndTime >= @startDate AND DateAndTime <= @endDate
AND areaName = @area) AS TempTbl
WHERE
rowNum BETWEEN @startRow AND @endRow;
7条答案
按热度按时间y3bcpkx11#
rfbsl7qr2#
One thing you could try might be to run ExecuteReader and iterate through the results.
This might help you to see if the problem is with your command, or with your DataTable.
I know the parameters are being added correctly, I know that data exists for the given query
When you're debugging, you should question all your assumptions, including this one. Note also that when executing queries manually using SSMSE, your connection may be configured differently (e.g. ANSI NULLS, ANSI PADDING) and therefore give different results.
EDIT
In response to Nik's comment, here are some suggestions for debugging this.
btxsgosb3#
I was just having a similar problem with
OracleDataAdapter
. I was trying to even do a simpleselect * from table
and it wasn't working. I ended up setting the
DbCommand.CommandTimeout
property and it began to work. I'm still confused, though, because I have previously never had to set these properties.
zour9fqk4#
Are you accessing the query through a webservice that executes the ADO.NET call? If the server's timezone is different, the values for @StartDate and @EndDate may not be what you expect.
new9mtju5#
There may be problem between your join by Multiple WHERE Clause. You have to find out that whatever table are join on specific ID is correct or not as per your database table and data.
kcugc4gi6#
I had a similar issue, though I don't know if it addresses the OP's question, but may help someone:
I had an issue where I was getting no rows returned and an exception
Cannot find table 0
, using similar code to use the adapter to fill a dataset (which automatically creates a table in the dataset).Relevant snippet of code (doesn't show OracleCommand, connection opening, for simplicity):
I knew the query I was sending via an OracleCommand should have done the fill and gotten results - or so I thought. When I ran the query in Oracle SQL Developer, I got the real exception, which was that I had the wrong field name in my query for one of the fields. When I corrected my query, all came through fine.
polhcujo7#
12 years later but I hit the problem when the sql does several steps and does a select at the end.
If you do
da.Fill(dataset)
it works, whereasda.Fill(datatable)
did not work.In my case the sql created a temp table via select..into, then it does a few updates, then it finally does a select statement to return the data at the end. This did not work directly into a datatable, but it works fine into a dataset.
Hope that helps someone :)