4

So I'm trying to fill a DataTable with data from a MSSQL query, but for some reason it completely refuses to work and it's making me very annoyed.

When I iterate through the results with a SqlDataReader and Read(), I get the results and even when I attempt to fill the DataTable with the SqlDataAdapter, the query appears on the SQL Profiler and yet doesn't return any data.

I have no idea what has possessed my code, but maybe you can figure it out:

            try
            {
                // Global variables
                var connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
                var textString = "Pasākums {0} sākas pēc {1}!";
                var linkString = @"/Event/Index/{0}";

                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    // Set variables
                    var findIn24HrsEventsCmd = new SqlCommand(@"
                        SELECT adm.UserID, adm.EventID FROM [dbo].[EventAdmissions] AS adm WHERE EventID IN
                        (
                        SELECT EventID FROM [dbo].[Events]
                        WHERE DATEDIFF(hour, @date, StartTime) BETWEEN 0 AND 24
                        )
                        AND 
                        (
                        SELECT COUNT(URL) FROM [dbo].[Notifications]
                        WHERE Type = 1 AND UserID = adm.UserID
                        AND URL LIKE '/Event/Index/'+CAST(adm.EventID AS VARCHAR(36))
                        ) = 0", conn);
                    findIn24HrsEventsCmd.Parameters.Add(new SqlParameter("date", "2015-05-31 02:17:28.727"));

                    var test = new SqlCommand(@"SELECT * FROM [dbo].[EventAdmissions]", conn);

                    var findIn1HrEventsCmd = new SqlCommand(@"
                        SELECT adm.UserID, adm.EventID FROM [dbo].[EventAdmissions] AS adm WHERE EventID IN
                        (
                        SELECT EventID FROM [dbo].[Events]
                        WHERE DATEDIFF(minute, @date, StartTime) BETWEEN 0 AND 60
                        )
                        AND 
                        (
                        SELECT COUNT(URL) FROM [dbo].[Notifications]
                        WHERE Type = 1 AND UserID = adm.UserID
                        AND URL LIKE '/Event/Index/'+CAST(adm.EventID AS VARCHAR(36))
                        ) < 2", conn);
                    findIn1HrEventsCmd.Parameters.Add(new SqlParameter("date", "2015-05-31 02:17:28.727"));
                    var t = findIn1HrEventsCmd.CommandTimeout;

                    // Retrieve data
                    conn.Open();
                    log.Debug("Starting with the events that are on in an hour.");

                    // Do it first for evens within an hour
                    var oneHrDataAdapter = new SqlDataAdapter(test);
                    var oneHrDt = new DataTable();

                    oneHrDataAdapter.Fill(oneHrDt);

                    findIn1HrEventsCmd.Dispose();
                    findIn24HrsEventsCmd.Dispose();
                    oneHrDataAdapter.Dispose();
                }
            } catch (Exception e)
            {
                log.Fatal("Fatal error!" + e.Message);
            }

Note how I've replaced the complex queries for a very simple test query that definitely returns results in Management Studio and with the DataReader, but doesn't work with a DataTable for some reason. Note that it isn't timing out, the server is located on the same machine and the query runs for maybe like 1-2 seconds at most.

The connection works, because as I mentioned before the DataReader approach works and also there are no exceptions thrown.

3
  • are u getting any error? Commented May 30, 2015 at 4:54
  • no i am not getting any errors Commented May 30, 2015 at 11:08
  • try running your query in database engine. it's possible ur query not returning any data from database or possible that your database table does not returning data against your query Commented May 30, 2015 at 11:28

1 Answer 1

9

God damn, I never bothered to check the Rows property of the DataTable, turns out it did work.

I thought it didn't because while in debugging mode Visual Studio is very misleading because when you hover over the datatable variable it just shows "{}" which usually would mean that the thing is empty.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes super deceiving. I don't use this data adapter often, nailed me too.
What the actual what!? This is not just deceiving, it's criminal.
Wow... Just wow... I have wasted days on this problem. Thank you for the solution!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.