checking text box for textchanged


Hello,
Using c#. How do you check to see if the value in a textbox has changed. I want to only update my db if the text has changed, for say in a qty box, and I thought I could do if(_myControl.TextChanged) but I get and error saying it can only be used on a certain side of some operators.

thanks in advance!!
animated405Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Timbo87Commented:
TextChanged is an event that you can assign an event handler to, but I don't think that's quite what you want. What I would do is store the initial value in a variable, then when a button is clicked or the form is closed (I'm not sure if this is in WinForms or ASP.NET), check the new value against the original value and update the database accordingly.
0
sajayCommented:
you mean you want to save into the database only if the value in the text box has changed from the intial value when user clicks save or soemthing

asp.net or winforms
0
iboutchkineCommented:
Update database on a text change event is not a good idea. Just imagine that you are typing 5 letter word. The text change event will fire 5 times. Are going to update db 5 times? I would create a separate button to update db or on the textbox-leave event
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

animated405Author Commented:

Thanks for the replies. This is ASP.NET, using C#. What I have is a cart where the customer makes the changes to the qty field and then clicks and update button which fires and event that updates each sku with the qty regardless of whether or not the value has changed. This is obvioulsy not the best practive for such an event. I know I could store the values and reference them when the page is posted back to see if they've changed, however somehow I thought that the control knows whehter or not the value is different from when it was originally populated when the page has been posted back.

If I have to go the route of holding onto this information, storing it in the session immediately comes to mind and I could kill it when I'm finished. I'm using a datagrid to display the items, I could loop through it and check against the same information in the array then only update if a change has been made. Just seems like there is a cleaner way than this...

thanks again
0
atxondoCommented:
Why dont you try with keypress event??

txbox.KeyPress += new...

You can now wich is the key that has been pressed, and the new text.
0
animated405Author Commented:

thanks atxondo. Wouldn't that imply that an event handler would get fired as soon as that action occurs, on each textbox? what I have is several textboxes on a form that when a button is clicked then go through them all and if the value has changed submit to the database.

thanks again
0
atxondoCommented:
Hi,
 It´s true an event will be thrown when a button is pushed. But you can use a single function to recieve all the events.
textBox1.TextChanged += new System.EventHandler(this.txtChanged);
textBox2.TextChanged += new System.EventHandler(this.txtChanged);
....
textBoxn.TextChanged += new System.EventHandler(this.txtChanged);
And you can use two event types to know the text  before and later the text has changed. You can use this event to know the text whe it has changed.

With keyPress you can know the text of the textbox that is going to change and the new text with:

private void txtChanged(object sender, System.EventArgs e)
{
      TextBox txtBx = (TextBox) sender;
      string s = txtBx.Text;
}

It´s true that you must save the text of the textboxes to know where it has changed.
I hope this is interesting for you.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.