Trying to check if my Obj(class).prop were changed asp.net

gal mor 141 Reputation points
2022-11-07T09:30:53.93+00:00

so I've been asked to save my gridview in an object list and change/updates values if the obj.prop was changed..
My code now is catching gridview values(old values) and .findcontrol values which are the new ones, and comparing between them both,
The way it is now is not optimal because Im counting on the selected index and I know that the change has occured in a specific row.
what if the user was able to change few rows/columns at a time? Im trying to iterate through the gridview and check if my Object.Properties were changed compared to their original state which is the Datatable's values my gridview shows and knows already when it was binded on page_load.

Is what Im asking making any sense to you?
Thanks, any help will be appreciated.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,253 questions
{count} votes

Accepted answer
  1. AgaveJoe 26,201 Reputation points
    2022-11-08T14:38:39.193+00:00

    if I wanted to change value no 9,999 , id either have to go to the 1000th page and press EDIT, then sent to editindex and rowupdating eventually, or either have to sort the table by ID and get the 9,999id as a second record. this is not useful at all.

    Confused.... this is always a problem when the number of record exceeds what a human can naturally handle. The standard solution is adding a search feature to filters the data into a more manageable result set. There are many approaches to filtering data; free formed entry, category, date, starts with, etc. The approach depends on the data. I'm sure you have experienced filtering while surfing the internet.

    Sorting, paging, and filtering data with model binding and web forms

    I need to save it on some kind of a list/dictionary and then to access the update method when passing it the exact place I need to change.

    This is essentially the example I provided. If you are looking for a generic approach then refactor the code above to take advantage of C# generics and maybe an interface. However, I still question building a generic UPDATE statement. What's the benefit?

    The go to approach is a stored procedure. At least with a stored proc (or parameter query) you get type checking. If you build a SQL generator then you also need to be mindful of type checking.

    Generic classes and methods
    interface (C# Reference)

    As far as I can tell, the approach you are attempting involves a making a second copy of data which that already exists in the database. This causes synchronizing issues. If you really need a second copy then you should use a standard caching pattern where the cache expires at some frequency but I don't think caching is what you're after.

    Frankly, I think you are over complicating the approach and you're not listening to the community's advice.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. AgaveJoe 26,201 Reputation points
    2022-11-07T16:00:37.467+00:00

    I have a list of objects that match my needs, but I have no idea how to detect the changes based on the properties. and before you say, I did google.

    The community cannot see this code! However, I shared example code that detects when a property value changes in a previous thread of yours.

    I'd rather stick to what ppl have done before me.

    The community has not idea how the existing code works. Perhaps get help from your team.

    Below is a basic example to detect changes. It's not what I would do exactly but I think it gets to the heart of what you're asking. Keep in mind, this is NOT production code. Feel free to modify the code however you like.

        public class Person  
        {  
            private string setFirst;  
            private string setLast;  
            private string setEmail;  
      
            private string _firstName;  
            private string _lastName;  
            private string _email;  
            public int PersonId { get; set; }  
            public string FirstName  
            {  
                get { return _firstName; }  
                set  
                {  
                    if (_firstName != value & !string.IsNullOrEmpty(_firstName))  
                    {  
                        setFirst = $"FirstName = '{value}'";  
                    }  
      
                    _firstName = value;  
                }  
            }  
      
            public string LastName  
            {  
                get { return _lastName; }  
                set  
                {  
                    if (_lastName != value & !string.IsNullOrEmpty(_lastName))  
                    {  
                        setLast = $"LastName = '{value}'";  
                    }  
      
                    _lastName = value;  
                }  
            }  
      
            public string Email  
            {  
                get { return _email; }  
                set  
                {  
                    if (_email != value & !string.IsNullOrEmpty(_email))  
                    {  
                        setEmail = $"Email = '{value}'";  
                    }  
      
                    _email = value;  
                }  
            }  
      
            public string UpdateSQL()  
            {  
      
                if (string.IsNullOrWhiteSpace(setFirst) & string.IsNullOrWhiteSpace(setLast) & string.IsNullOrWhiteSpace(setEmail))  
                {  
                    return string.Empty;  
                }  
      
                StringBuilder sql = new StringBuilder();  
                List<string> items = new List<string>();  
      
                sql.AppendLine("UPDATE dbo.Person");  
                sql.Append("SET ");  
      
                if (!string.IsNullOrWhiteSpace(setFirst))  
                {  
                    items.Add(setFirst);  
                }  
      
                if (!string.IsNullOrWhiteSpace(setLast))  
                {  
                    items.Add(setLast);  
                }  
      
                if (!string.IsNullOrWhiteSpace(setEmail))  
                {  
                    items.Add(setEmail);  
      
                }  
                string set = string.Join(",\r\n", items);  
                sql.AppendLine(set);  
                sql.AppendLine($"WHERE PersonId = {PersonId}");  
      
                return sql.ToString();  
      
            }  
        }  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                Person p = new Person() { PersonId = 1, FirstName = "Hello", LastName = "World", Email = "Hello@email.com" };  
      
                //Should be empty  
                Console.WriteLine(p.UpdateSQL());  
      
                //Make a change  
                p.FirstName = "Foo";  
                p.LastName = "Bar";  
                p.Email = "Blah@email.com";  
                Console.WriteLine(p.UpdateSQL());  
            }  
        }
    

  2. XuDong Peng-MSFT 10,096 Reputation points Microsoft Vendor
    2022-11-08T06:16:46.9+00:00

    Hi @gal mor ,

    so I've been asked to save my gridview in an object list and change/updates values if the obj.prop was changed..

    Based on your description, if you want to implement editing and updating of records in GridView control, why not use the built-in events? For example, GridView_RowEditing, GridView_RowUpdating, etc.

    As already mentioned by community member, even if you want to get the new value inside the control compared to the old value, you need to get the primary key of the specific row(record) to implement the update operation.

    what if the user was able to change few rows/columns at a time? Im trying to iterate through the gridview and check if my Object.

    In addition, how is editing/updating multiple lines that you mentioned achieved? Did you add a checkbox to each row of records, and then check the checkbox to determine which row to edit/update? If this is the case, you can simply refer to the answer in this thread. Or any other way? If possible, please describe your question more clearly, if you can provide a minimal code example that reproduces the problem, it will help to solve the problem, thank you for your understanding.

    Best regards,
    Xudong Peng


    If the answer is the right solution, please click "Accept Answer" and kindly upvote. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.