0

What is happening in this code is that the drv variable is being null while the count of items in the listview is 3. Can any one tell me what i'm doing wrong please?

private void btn_save_Click(object sender, RoutedEventArgs e)
{
    DataRowView drv;
    bool valueToCheck;
    List<Common.Rule> lst = new List<Common.Rule>();
    Common.Rule ru = new Common.Rule();
    lst = rr.GetAllRules().ToList();//getting all rules from database
    for (int i = 0; i < ltv_rules.Items.Count; i++)
    {
        drv = ltv_rules.Items.GetItemAt(i) as DataRowView;
        valueToCheck = Convert.ToBoolean(drv["IsSet"]);// to get the value of the combobox isSet from the list view
        ru = lst[i];//getting the value of the isSet before it was binded to the list view.
        if (ru.IsSet != valueToCheck)//comparing the original value (before it was binded) to the value that i get from the listview
        {
            ru.IsSet = valueToCheck;
            bool updated = rr.UpdateRule(ru);
        }
    }
}
Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
  • `Can any one tell me what i'm doing wrong please?` - **Everything**. First of all, Don't use `System.Data.DataTable`. It's just a glorified `Dictionary` which forces you into a bunch of horrible casting, `Convert.ToMyGranny()` and magic-string based stuff. Second, create a proper Data Model to hold your data. Third, if this is WPF, create a proper ViewModel and all your problems will magically disappear. – Federico Berasategui Feb 05 '14 at 16:24
  • Forth, learn to use the `var` keyword in order to Not Repeat Yourself. fifth, learn to use LINQ in order to do 99% of Collection-related operations in C#, rather than traditional procedural `for` loops and the like. – Federico Berasategui Feb 05 '14 at 16:27
  • @HighCore, I see you writing this about the `DataTable` class all the time. The type is a valid and useful way to get and manipulate data quickly, esp. when working with databases. It is certianly not correct to say it is a 'glorified `Dictionary` this is _not_ true... To construct a generic class that operates like a `DataTable` would take a LOT of work. I agree with the view model assertion, however. – MoonKnight Feb 05 '14 at 16:28
  • @Killercam yeah, I'm pretty sure that this: `Convert.ToBoolean(dr["SomeBool"])` is somehow more efficient that creating a proper strongly typed Data Model and doing `model.MyBool` instead of using a bunch of horrible [magic-string-based](http://c2.com/cgi/wiki?StringlyTyped) stuff where evething is `object` and you need to cast everything constantly. Please... If you need to query a DataBase, use Entity Framework, it's not 1990 anymore. – Federico Berasategui Feb 05 '14 at 16:32
  • @Killercam BTW, that stuff is pre .Net 2.0 when there was no generics. You can't even LINQ it. `DataTable.Rows` is an `IEnumerable` instead of being an `IEnumerable`. You can't be serious. – Federico Berasategui Feb 05 '14 at 16:35
  • @HighCore firstly, you can access `DataRow`s using indexes. Second all/most data adapters (including `SqlDataAdapter`) support `Fill` which is a massively useful method that can be used to fill bboth `DataTable`s and `DataSet`s. All I am saying is this class has a place and that to get your own data structure of simalar working capacity is not as trivial as saying 'create a proper Data Model to hold your data'... – MoonKnight Feb 05 '14 at 16:36
  • @Killercam creating a class with 3 properties is not trivial? what is this java? If you don't like the `Code-First` approach, use an EDMX. If you don't like EDMX, use a `DataBase-First` approach. `DataTable` is useless. period. – Federico Berasategui Feb 05 '14 at 16:38
  • Fair enough. But take the case of reading an unknown amount of data from an Excel file (for example). Say I want to do this in a library and for unknown file types, populating and returning a `DataTable` in this case is easy, and provides the caller with a known and manageable return type. How would you propose this be done using a custom data structure (out of interest)? Not returning an `object` ... – MoonKnight Feb 05 '14 at 16:42
  • 1
    @Killercam [`LINQ To Excel`](http://code.google.com/p/linqtoexcel/) dude. Wake up, it's not 1990 anymore. C# is not java. – Federico Berasategui Feb 05 '14 at 16:47

1 Answers1

0

You are casting using as. This says to the compiler, 'I am not sure about this cast so if it is invalid return a null'. In some cases this is what you want, but in most cases you want an exception to be thrown on invalid casts. In the latter case you cast using (SomeType)myValue, so that if the cast is not valid, you will get an InvalidCastException.

I short, you are performing an invalid cast on the line:

drv = ltv_rules.Items.GetItemAt(i) as DataRowView;

I hope this helps.

MoonKnight
  • 23,214
  • 40
  • 145
  • 277
  • The problem is happening when i'm casting to DataRowView because when i change this line "drv = ltv_rules.Items.GetItemAt(i) as DataRowView;" to this "object o = ltv_rules.Items.GetItemAt(i);" the correct object will be returned. – user3276023 Feb 05 '14 at 16:33