question

galmor-3499 avatar image
0 Votes"
galmor-3499 asked XuDongPeng-MSFT commented

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

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.

dotnet-aspnet-generaldotnet-aspnet-webforms
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You've asked this question several times and several approaches have been explained with source code. Read your previous threads.

0 Votes 0 ·

I know what i've posted before and the replies werent as useful + I tried to make it clearer in a new thread, so... thanks anyways

0 Votes 0 ·

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.

Use the primary key of the record you want to update.


I know what i've posted before and the replies werent as useful + I tried to make it clearer in a new thread, so... thanks anyways

It is difficult for the community to help when you have not explained why the many previous approaches are not useful? Have you even tried the suggestions?

Why is using the database to manage state not useful?
Why is using Session not useful?
Why is using inputs not useful?
Why is using standard Web Forms state management (ViewState) not useful?
Why is creating a layered design not useful?
Why is using class properties to detect value updated not useful?
why is taking advantage of entity Framework not useful?

It seems you are looking for a very specific answer. Can you tell us what you are looking for? Perhaps you are looking for a design pattern like the repository or unit of work?

Can you show us the approaches you've tried and explain why each does not meet your expectations?






0 Votes 0 ·
Show more comments
AgaveJoe avatar image
0 Votes"
AgaveJoe answered galmor-3499 commented

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AgaveJoe avatar image
0 Votes"
AgaveJoe answered galmor-3499 commented

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());
         }
     }
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

thanks for taking your time to reply!

0 Votes 0 ·
XuDongPeng-MSFT avatar image
0 Votes"
XuDongPeng-MSFT answered XuDongPeng-MSFT commented

Hi @galmor-3499,

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.

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

To be honest I had a vision that I have my gridview /datatable saved and inserted to a list of objects.
lets say class Exc has properties ID.col1.col2
and in addition to that, the first object in the list is : ID=1,col1="col1",col2="col2".
now lets say I wanted to iterate both ObjListBeforeChange and ObjListAfterChange, and then to
check if ObjListBeforeChange.col1/col2 =/!= ObjListAfterChange.col1/col2 then to perform a my desired db queries, and from there moving on to the next object of both new and old list to keep doing the same..

0 Votes 0 ·

Hi @galmor-3499 ,

Well, according to your description, I think what you want to achieve is a uniform update of the GridView data, not every change to update the GridView, right? If this is the case, I think what you need to do is to encapsulate the obtained set of control values into the class object you define, and finally compare the saved object with the object that has just been encapsulated. You just need to override the Equals method to determine whether the properties in these two objects are the same. Something like this:

public class CustomeClass
{
    public int ID { get; set; }
    public string Col1 { get; set; }
    public string Col2 { get; set; }
    public override bool Equals(object obj)
    {
        // If the passed object is null, return False
        if (obj == null)
        {
            return false;
        }
        // If the passed object is not Customer Type, return False
        if (!(obj is CustomeClass))
        {
            return false;
        }
        return (this.Col1 == ((CustomeClass)obj).Col1)
            && (this.Col2 == ((CustomeClass)obj).Col2);
    }
}

In addition, if the record in the GridView is added or deleted, then you also need to consider the primary key mentioned above, which is the key to ensuring that the data is updated correctly.

0 Votes 0 ·

I think what you want to achieve is a uniform update of the GridView data, not every change to update the GridView, right?

I appreciate your comment but no this isnt exactly what I need.

all the fuss is about making it more generic. Imagine I have 10,000records in my table which is represented in the gridview.
now 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.
I need to save it in some kind of a list/dictionary and then to access the update method when passing it the exact place I need to change.
I hope I made it even more clear now. and again, thanks much!

0 Votes 0 ·
Show more comments