Resolved CS1061 Error - missing definition

kwhelchel

Well-known member
Joined
Feb 28, 2020
Messages
53
Programming Experience
Beginner
I am getting an error CS1061 on the cd.Closeout with in this button click event to load data to a datagrid. Been trying to figure this out but with no luck.

Error as listed
Severity Code Description Project File Line Suppression State
Error CS1061 'MainWindow.CloseoutDataContext' does not contain a definition for 'Closeout' and no accessible extension method 'Closeout' accepting a first argument of type 'MainWindow.CloseoutDataContext' could be found (are you missing a using directive or an assembly reference?) ReportViewer C:\Users\keith\source\repos\ReportViewer\ReportViewer\MainWindow.xaml.cs 72 Active


I added a public class CloseoutDataContext to clear the error with the CloseoutDataContext cd = new CloseoutDataContext(); line

C#:
      private void ClosButton_Click(object sender, RoutedEventArgs e)
        {
            LoadCloseout();
        }
        private void LoadCloseout()
        {
            CloseoutDataContext cd = new CloseoutDataContext();
            var closeout = (from p in cd.Closeout
                            select p).Take(10);
            ReportFilter.ItemsSource = closeout;
            ClosButton.Content = "CloseOuts Loaded";
        }
 
Why would you think that you could create an instance of a type that didn't exist? Why would you think that you could create a type with that name and then access a member that doesn't exist? Why are you writing this code in the first place? I suspect that you are copying something off the internet and either not following their instructions correctly or else they haven't provided adequate instructions. Find out which one it is. Based on the name, it would appear that that code is supposed to be using either Entity Framework or LINQ to SQL. CloseoutDataContext is the data context and Closeout is a collection that represents one of your database tables in that context. You probably need to work out which data access technology it is and then learn how to use it.
 
What I am trying to do is load a datagrid from a button click event. There will be multiple buttons loading different data tables from sql express
 
You seem to have missed the 3 implied questions you have been asked.

What is the point in passing event data to a method that doesn't do anything with that data? What are you "Routing" through all things a click event?
 
What I trying to do is with in this layout first list box on left will have a selection of user reports to select and run. the middle will have the user select and area to pull up filters in the right hand side datagrid to be able to selt from so when the report is run it knows what areas to filter by. So the center text box will reflect the data that is selected for each area into the right hand side datagrid.
using linq
reportviewer.PNG
to sql
 
So you are using LINQ to SQL then? Personally, I'd recommend going with Entity Framework if you want to use a Microsoft ORM but L2S will still work. In that case, have you generated the appropriate L2S types? If so, that CloseoutDataContext should already exist because it is the centre of your app's L2S universe. It seems that you either haven't generated your L2S context or you used a different name. I suggest that you need to do a bit more reading on L2S because the question you have asked doesn't relate to it specifically. You're just trying to use a type that doesn't exist so you need to create it but you also need to create it in the appropriate way, so you ned to learn what that is.
 
My only qualm I have with John's advice is only the recommendation to use EF (Entity Frameworks). EF is riddled with bugs, bugs which have existed for years, and they have never been fixed. EF also suffers greatly from memory leaks and none of these have ever been patched. Needless to say it's not a software I would recommend personally. However, that said; if you really must use a ORM (Object Relational Mapper), then maybe you would be best using Dapper. There are others like NHibernate which is also wrote in C#, although I don't have any experience with that product personally, but i would avoid EF and use Dapper instead.
 
I've only ever used the old version of NHibernate which was practically a port of the Java based Hibernate ORM. At that time it carried over all the Java-esque ways of doing things and didn't feel like a native .NET Framework, much less a C# specific library. I used it because like @Sheepings, I found that Microsoft's Entity Framework early versions was just a piece of sh*t -- which is saying much because at the time I was trying this out, I was working for the Evil Empire. NHibernate, even though it felt foreign, was lightyears better than EF.

Both NHibernate and EF have matured since that time many years ago, but I still feel that NHibernate is a much better library than EF. Like @Sheepings, I would also recommend Dapper as the first thing that you should try out, especially if your needs are simple. It's got a much better learning curve than either of the other two despite there being a lot more material being produced about the other two.
 
Back
Top Bottom