Results 1 to 8 of 8

Thread: Sequence contains no elements

  1. #1
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2017
    Posts
    63
    Rep Power
    16

    Sequence contains no elements

    Hello:

    I am using EF6 with VS 2017.

    I have used this logic before without issues, but here is the code:
    Code:
            private void cmdAddData_Click(object sender, EventArgs e)
            {
                DataTable dt_TimeData = timeDataSet.Tables["TimeData"];
                var IDVal = from a in dt_TimeData.AsEnumerable()
                            where (a.Field<Int32>("ID") != null)
                            select (a.Field<Int32>("ID"));
                int idVal_Int = IDVal.Max() + 1;
                    
                Add_dgvTime(idVal_Int);
    
    
            }
    From the behavior below, we are not seeing the data, but it is there!

    Screen Shot 06-06-18 at 03.16 PM.PNG

    There is data in the database:
    Screen Shot 06-06-18 at 03.57 PM.PNG

    What am I missing?

    Thanks!

  2. #2
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,979
    Rep Power
    154
    This makes no sense:
    a.Field<Int32>("ID") != null

    Looking at the logic of that code, you're getting an Int32 value from a column and comparing it to null. That is nonsensical because Int32 is a value type so it can't possibly be null under any circumstances. It also appears to be the primary key for the table so it's doubly nonsensical. If you actually needed to check whether a that column was NULL then you'd do this:
    !a.IsNull("ID")

    There's no point doing that though because if it's the PK then it's not nullable (if you have configured things properly) so it must have a value.

    I would question what you're doing in its entirety though. Calculating a PK yourself like that is dodgy. SQL Server supports both identities and, in newer versions, sequences so you should almost certainly be using one of those. Generally speaking, you should use an identity and let the database generate the value when you save. Your DataTable can then automatically generate temporary values that are perfectly usable and can be automatically updated when you save if needed. Thousands of developers the world over do it this way without issue and you should too. If you need the final PK value before saving, which can happen, then that's where you should use a sequence instead of an identity.

  3. #3
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2017
    Posts
    63
    Rep Power
    16
    Makes sense. This is what I had to do in my last question to get data in, as the dgv had to be populated ahead of time, and I have no idea why??

    The issue now is in this picture, and I cannot seem to fill the Table Adapter.

    Screen Shot 06-07-18 at 09.28 AM.PNG

    And the data is here:
    Screen Shot 06-07-18 at 09.31 AM.PNG

    Sorry for being such a novice at this...

    Please let me know what I'm missing.

  4. #4
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,979
    Rep Power
    154
    You can configure your DataSet beforehand to not enforce constraints and that will enable you to populate the DataTable and then examine the data to determine which of the three possible situations mentioned in the error message is occurring. I wouldn't have thought that it would be the foreign key because you're populating the parent table first, but I wouldn't have thought that it could be either of the other two either, unless your DataSet schema is out of sync with the database schema. Have you made changes to the database since generating the DataSet? If so, there's a button on the toolbar in the Data Sources window that will re-run the wizard and pull any new schema information from the database.

    https://msdn.microsoft.com/en-us/library/s3bxwk8b.aspx

  5. #5
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2017
    Posts
    63
    Rep Power
    16
    Same error with Enforce Constraints set to True!

    I can run the query and see the data, no problem.

  6. #6
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,979
    Rep Power
    154
    Quote Originally Posted by ssabc View Post
    Same error with Enforce Constraints set to True!
    Of course there is, because that's the default. I said to NOT enforce constraints. That will enable you to load the data successfully so that you examine it and determine what data is violating what constraint. The error message tells you what the three possibilities are so it will be easy to find the offending data. You just have to look for a null value in a non-nullable column, a duplicate value in a column required to be unique or value in the foreign key column that is not in the primary key column of the parent table,

  7. #7
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2017
    Posts
    63
    Rep Power
    16
    I wrote that backwards. It should have said Same error with Enforce Constraints set to False!

  8. #8
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,979
    Rep Power
    154
    Try calling GetData instead of Fill. That will return a DataTable that is not part of a DataSet and thus not subject to foreign key constraints. That will at least let you know whether its foreign keys that are the issue. By the way, you haven't changed any of the default queries for the table adapters have you?

Similar Threads

  1. How to search sequence of bytes of a bin file?
    By Gokuland in forum C# General Discussion
    Replies: 3
    Last Post: 02-10-2018, 1:00 AM
  2. print number of array elements
    By x1402 in forum C# General Discussion
    Replies: 4
    Last Post: 10-06-2017, 1:49 AM
  3. Question Pairing The Elements of 2 Array For Once
    By MangaTotti in forum Console Application
    Replies: 2
    Last Post: 05-08-2016, 6:54 AM
  4. Replies: 1
    Last Post: 08-28-2015, 2:50 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •