c# - Creating a smart save button -
i'm designing desktop app using c#.net , mssql. app contains form user can change values in text boxes , press button save them in database. following code calls method update values , insert them database beside shows message box confirms new changes. problem that, user has not change value , press save button message box appear again . tried compare values of text boxes before , after change dose not work. way overcome problem ?
private void btnsave_click(object sender, eventargs e) { int price = convert.toint32(txtprice.text); classes.prices prices = new classes.prices(combobox1.selectedvalue.tostring(), convert.toint32(txtprice.text), convert.toint32(txtcarbohydrate.text), convert.toint32(txtprotein.text), convert.toint32(txtfat.text), convert.toint32(txthumidity.text), convert.toint32(txtminerals.text)); try { prices.updatematerialprice(); //this.combobox1.refresh(); this.txtprice.refresh(); int price2 = convert.toint32(txtprice.text); if (price2 != price) { messagebox.show("new price saved!"); } } catch(exception ex) { throw ex; } foreach (control ctrl in this.panel1.controls) { if (ctrl textbox) { textbox textbx = ctrl textbox; if (textbx.readonly == false) { textbx.readonly = true; textbx.backcolor = color.lightblue; } } }
the problem price , price2 always have same value since capturing them both @ moment user presses "save" button you'll message box, goes this: user changes price text box user presses "save" button , "btnsave_click" method run, @ point txtprice has value user typed.
i recommend having separate layer data access, use entity framework , ask entity if of values has changed. check these, might you:
entity framework: check if there changes saved specific entity
http://www.codeproject.com/articles/615499/models-poco-entity-framework-and-data-patterns
Comments
Post a Comment