-1

I have been trying to bind a variable to WPF. I am using LINQ to get the data. The criteria is a unique int type and returns a single row of data that has 30 columns, of which there are several different types that need to be returned.

I am getting this error InvalidOperationException Class, but I can't figure out how to fix this?

This is what I have thus far,

internal class DatabaseQueries
   {
       public static IEnumerable<int> ModValues(DatabaseDataContext database, int staffNo)
            {
                return database.Staff_Mod_TBLs
                    .Where(staff => staff.Staff_No == staffNo).Cast<int>().ToList();

            }
   }

This is the code that set the variable,

int staffNumber = 192356;
            var modTblValue = DatabaseQueries.ModValues(sql, staffNumber); 

And the XAML, (for some reason I can't post all the XAML, so this is an abbreviated snip of code.)

<DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock FontWeight="Normal" Text="{Binding Path=modTblValue, UpdateSourceTrigger=PropertyChanged}" />
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
7
  • 4
    Bind your modTBlValue as a property. binding only works with properties. Commented Jun 30, 2016 at 14:19
  • @AbinMathew, I am not sure I follow? Sorry, still learning all this. Commented Jun 30, 2016 at 14:20
  • 1
    .Cast<int>() - where did you see that? Go and learn Select. Select, Where are pretty basic things, you can't use LINQ without knowing at least the basic constructs. Commented Jun 30, 2016 at 14:25
  • @IvanStoev, I believe that is called The process of learning something you don't already know. If I knew everything there was to know about LINQ, I am sure I would not be in the pickle. I am sure you can relate to when you wrote your first Hello world line of code? So you suggest using select? I followelled the error message that said I was missing a cast, that is why that is in there. I have literally searched dozens of pages to get where I am right now. Commented Jun 30, 2016 at 14:32
  • @IvanStoev, how do I use select to get 30 columns of data? Commented Jun 30, 2016 at 14:34

1 Answer 1

4

Create a property like below,

private string _modTblValue;
public string modTblValue 
    {
        get { return _modTblValue ; }
        set { modTblValue = value; NotifyPropertyChanged(); }
    }

Assign the value you get from DataBase to modTblValue

 modTblValue = DatabaseQueries.ModValues(sql, staffNumber); 

Bind your property modTblValue with UI

<TextBlock FontWeight="Normal" Text="{Binding Path=modTblValue, UpdateSourceTrigger=PropertyChanged}" />

If you need a collection of your property to bind to a DataGrid then create a class which contain your Properties, and make a property of list of your class to bind as ItemSource of your DataGrid

Check this too.

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

7 Comments

Thanks for the help, that clears up a part of all this that I was not doing correctly. But I still get an error with the query.
Yes, the LINQ query seems to be the problem. As Ivan stated in the comments, I need to use select. But, I can't get past the combine error when making one with select. InvalidOperationException Class error
Your return type is of IEnumerable<int> and consider reading this
I have been over that page many times, but I can't get the syntax correct to put the query into its own class???
Hey, I still can't get this syntax correct. I am going to mark this answer as the best answer. Thanks for the help.
|

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.