locked
Getting a property setting error and I am not sure how to fix it... RRS feed

  • Question

  • User2130491911 posted

    Hi all,

    I am trying to use a view model to set a default value for a property so that I can move linq data to a table so that the table cab be downloaded. Not even If I am going about this the correct way.


    public class ViewModel_XXX_Class
    { [DisplayName("Speaker Count")] public Nullable<int> CONTACT_SPEAKER_COUNT { get { return CONTACT_SPEAKER_COUNT; } set { if (value == null) { throw new ArgumentNullException("value"); } CONTACT_SPEAKER_COUNT = value; } }

    I think the idea here is that if the field is null it will return something so that the property will not make an error.

    Here is the function that passes data (this needs to be the view model , can you query a view model?

            public void Export_CampaignData(int CampaignID)
            {
                var data = from p in db.VUE_MAIN_C4P where p.C4P_TYPE_ID == CampaignID // Odb is the object of edmx file  
    
                select new
                {
                  // Fields To Export  
                    p.CONTACT_SPEAKER_COUNT,
                    ...
                };
    
    //f(x) Call to make datatable from Data CreateDataTable(data); return; }

    Here are the Functions that will make the data table for export

            public static DataTable CreateDataTable<T>(IEnumerable<T> entities)
            {
                var dt = new DataTable();
                                     
    
                //creating columns
                foreach (var prop in typeof(T).GetProperties())
                {
                    dt.Columns.Add(prop.Name, prop.PropertyType);
                }
    
                //creating rows
                foreach (var entity in entities)
                {
                    var values = GetObjectValues(entity);
                    dt.Rows.Add(values);
                }
    
    
                return dt;
            }
    
    
            public static object[] GetObjectValues<T>(T entity)
            {
                var values = new List<object>();
    
                
    
                foreach (var prop in typeof(T).GetProperties())
                {
                    values.Add(prop.GetValue(entity));
                }
    
                return values.ToArray();
            }
    
    

    There is where I get the error for setting a null item for a property. 

    I am not sure what I need to do so that It can build a data table from a link table

    Thanks for any help in advance!

    Also when I step through it I can see the value that it is trying to pass that is indeed null as it 

    has to be as that is how the SQL server table is set up.

    Wednesday, June 3, 2020 10:00 PM

All replies

  • User303363814 posted

    Your code says

    "If you set CONTACT_SPEAKER_COUNT to null then throw an Exception"

    If you don't want this to happen then remove the code which throws the Exception.

    Your getter and setter are both infinite loops - you might want to fix that as well.  Perhaps change the property definition to

    [DisplayName("Speaker Count")]
    public Nullable<int> CONTACT_SPEAKER_COUNT { get; set; }
     


    Your call to create the data table

    CreateDataTable(data);

    discards the resulting data table.  Perhaps you want to do something with the table you create?

    var createdTable = CreateDataTable(data);
    // Now do something with the table that has been created???

    Thursday, June 4, 2020 5:18 AM
  • User2130491911 posted

    Thank you for the info but I am confused.

    The whole get and set thing I changed to add the logic so that when the applications is processing the linq data that it would fill something in for values that were null when building the data table.

    If I change it back to what you listed how would this allow the application to ignore null fields when trying to set the property? 

    Ahh thanks on that create table thing... this stuff is all new to me in that I am not a full time programmer so I miss alot of things like that.

    The error that I get :

    System.NotSupportedException: 'DataSet does not support System.Nullable<>.'

    At this part of the application:

    foreach (var prop in typeof(T).GetProperties())
                {
                    dt.Columns.Add(prop.Name, prop.PropertyType);
                }
    



    Thursday, June 4, 2020 2:51 PM
  • User303363814 posted

    Sounds like it might be design time.

    would fill something in for values that were null
    So what value are you filling in?  I can only see that you are throwing an Exception.

    how would this allow the application to ignore null fields when trying to set the property
    This is a direct contradiction of the previous sentence.  Do you want a default value or do you want to ignore?

    Either way, at the moment the code throws an Exception.  An Exception is a way to jump up and down, yell, scream, wave a red flag and crash a program.  Neither ignoring nor a default value.

    Back to the design drawing board.  You need to understand the data you have and the results you want to achieve.  If you want to achieve a default value then you need to write code which provides a default value under the circumstances that you want one.  If you want to 'ignore null' then you need to work out what that means.

    BTW - Did you understand my explanation of your two, very tight, infinite loops?  This code is never going to execute.  You will not get right or wrong results.  It will just crash having consumed all of it's stack space.

    Thursday, June 4, 2020 11:25 PM
  • User2130491911 posted

    BTW - Did you understand my explanation of your two, very tight, infinite loops?  This code is never going to execute.  You will not get right or wrong results.  It will just crash havinn9g consumed all of it's stack space.

    Sadly I did not understand that and just turned the project over to a more senior developer  at one of our other sites who has a better understanding of programming. 

    But thanks! 

    Friday, June 5, 2020 6:08 PM