• Hello and welcome to our new forums. We upgraded our forum sites to a more robust and modern system which we hope you will enjoy. Be sure to check out your profile by clicking the button on the top right and configure your preferences, signature, time zone, avatar, etc. as you wish. If you need help with using this new forum'ware try the help link on the bottom right.

    Click here to review your account now.

Sequence contains no elements

ssabc

Well-known member
Joined
Dec 11, 2017
Messages
63
Programming Experience
10+
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!
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,166
Location
Sydney, Australia
Programming Experience
10+
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.
 

ssabc

Well-known member
Joined
Dec 11, 2017
Messages
63
Programming Experience
10+
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.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,166
Location
Sydney, Australia
Programming Experience
10+
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
 

ssabc

Well-known member
Joined
Dec 11, 2017
Messages
63
Programming Experience
10+
Same error with Enforce Constraints set to True!

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

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,166
Location
Sydney, Australia
Programming Experience
10+
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,
 

ssabc

Well-known member
Joined
Dec 11, 2017
Messages
63
Programming Experience
10+
I wrote that backwards. It should have said Same error with Enforce Constraints set to False!
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,166
Location
Sydney, Australia
Programming Experience
10+
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?
 
Top Bottom