How to save textbox value in the database
(Textbox value populated based on other textbox input value)
Solutions proposed in comments to the question seem to be reasonable, but I would offer one more if you don't mind. If TotalRate field's value is somehow important/vulnerable then I would never store it in database to avoid possible malicious manipulations from app users. In case there is some good reason/requirement to store calculated value in database I would go with calculation on the server side and handle it in onBeforeCreate and onBeforeSave model events:
// onBeforeCreate/onBeforeSave event handlers
record.TotalRate = record.PerHourRate * record.HoursNumber;
Related
I have users table containing all the user account details including a IsLoggedIn field with bit data type. I want to update any old rows with IsLoggedIn = 1 when a user starts new session before previous session expires. What is the best way to achieve this? Is it possible to do this with Trigger?
Thanks in advance..
You will have two things for sure.. 1) User ID and 2) Session Id in database.
Now when new session start (session_Start) event gets fire.. you can call one store procedure with logic and the logic will be
1) If there is not records with same userID do not do any update.
2) If there are any records with same userID update all of them with IsLoggedIn = 1.
You can extend this logic with TRIGGER as well and band it as per your need,.
Basically you need to track session_Start event and perform DB operations.
Trigger won't be the suggested solution as you need to handle login from UI. If there is going to be only a single login then Application_Start can be used. If there can be multiple logins then Session_Start is preferable. From there you can give a call to database and perform the respective actions.
Difference between the two is explained at : http://forums.asp.net/t/1230163.aspx/1?What+s+the+difference+between+Application_Start+and+Session_Start+in+Global+aspx+
I am trying to capture and save the selected value in a gridview to a variable in viewstate which i would like to use to pass as a query string parameter to a differant page
Where would i save the ViewState Variable and how?
Iam very new and not usre whether i have provided enough information
Thanks
Values stored in ViewState work differently than session. Unlike Session, a value stored in ViewState of one page cannot be retrieved from another. Actually ViewState is nothing but a system managed hidden encrypted field in the markup produced. How do you save a value in ViewState? like this:
ViewState["MyValue"]=GrideView1.SelectedValue.ToString();
Later you can retrieve that value like this
if(ViewState["MyValue"]!=null)
{
Response.Redirect("SecondPage.aspx?param="+ViewState["MyValue"]);
}
Due to the nature of the way the website I am working on was designed I have an issue I now must resolve.
I will try to write this question as language-independent but the site is done in ASP.Net with C#.
Overview
The structure of our site looks like this for any given "object":
ASPX Page
Multiple UserControls (ascx pages)
Textboxes, Comboboxes, Labels,
Buttons, etc.
What this means is, if we have a User page and that page might have 3 UserControls. One for User Information (Name, email, etc.), one for Address (City, State, Zip, etc.) and one for Regional Settings (Time zone, language, etc.)
All of these fields on the 3 UserControls are all stored in the same table in the database.
Now, we are using DataBinding and EntityFrameworks to fill in the data and this means that when we save an object, we are actually calling save 3 times (1 per UserControl).
(Language-Agnostic: We don't actually
assign values like User.Name =
"Bob", it is handled by a Framework)
Because the UserControl only knows about the fields it contains, when the Databinding saves it "thinks" the other fields are now blank. (i.e. Address control saves and now "Email" and "Name" are null values). To "fix" this there is a method called "MapOldToNew" which goes out and grabs the old record and fills in the values on the New object that is about to be saved. This is generic, which means we don't have a method on the Address Usercontrol that says "go get the Name and Email fields and fill them in", instead it loops through all the properties of the Entity(object) and if a value is NULL on the New object but NOT NULL on the Old object (the one currently in the database about to be overwritten) it will make the New value equal to the Old value.
The Problem
Dates. We allow NULL dates in our database.
If a User fills in "Birthday" and saves, they now have a Birthday value in the field in the database.
If they go back and clear out the Birthday field on the web page and save, the MapOldToNew method grabs the old record, sees that Birthday is not null there and is Null on the New object about to be saved, it will override the New value (NULL) with the Old value ('7/23/1981' for example).
Solutions?
Rework our system to not use
UserControls but instead have all
controls on the same Page, thereby
reducing the number of times we need
to Save and not requiring
MapOldToNew. For now, let's
assume this is an impossible
solution (even though it is the one
I feel like should be done for
plenty of reasons).
Store all
dates as Strings and do conversion
on Load and Save. (Strings don't
have the same problem because once a
string has been modified it is now
an empty string, and not NULL)
Do not allow NULL dates in the
database and always store
DateTime.MinValue and do not
display it On Load.
Those are the ideas I have come up with off the top of my head. For arguments sake, let's assume #1 is not possible (because I have a feel management won't like the time it will take to accomplish).
How would you "fix" this?
Another Explanation
Here is the problem broken down more.
User Record has 2 fields. Name and Birthdate.
Assume that there is 1 ASPX Page with 2 UserControls (ascx).
UserControl1 has a single DatePicker DataBound to "BirthDate".
UserControl2 has a single TextBox DataBound to "Name".
When UserControl1 calls Update on the ObjectContext the User object it sends looks like this:
{ Name = NULL, BirthDate = 8/13/1980 }
MapOldToNew looks at the Database Record and sees that Name should be "Bob" so it fills the value in and the record saves.
UserControl2 calls Update now and the User object looks like this:
{ Name = "John", BirthDate = NULL }
MapOldToNew sees that BirthDate is NULL so it overwrites it with 8/13/1980 from the database and the record saves and the database has the correct values.
Now assume the user goes back in and deletes the BirthDate value. When UserControl2 calls MapOldToNew, it sees that BirthDate is NULL (which the user wants) and overwrites it from the Database.
If it DIDN'T overwrite it from the database, it would always save it as NULL and the user would never be able to set the BirthDate.
I would refactor MapOldToNew to understand nullable types. Im assuming you have a nullable date (for ex. datetime? or Nullable and if so - do not make the assumption of overwriting if null.
Also in the future consider MVC or for WebForms consider Dynamic Data because of the mapping of types to models.
http://aspnet.codeplex.com/wikipage?title=Dynamic%20Data
I have an object with multiple collections that is retrieved from a WCF service and stored in the session.
The collections are bound to ListViews on the page, with a final submit button at the bottom.
I want to be able to make changes to this object in the session (add/edit items in the collections), without persisting the changes until the final submit button is clicked.
The problem I'm having is that the ids are created in the db, so all newly added items will have an id of 0. I don't see a way to uniquely identify the collection items unless I add something like a clientID field to the datacontract. I feel like I'm missing something really obvious here.
If these are auto-generated identity values, you can insert additionally guids for each entry.
I have a gridview which contains 100 records i have set paging to 10. At page_load it fills the grid so the records are not going to change so i dont need to hit to database again.
There is a filter textbox availble at the top of 'Name' Column when user types some key it should filter the 100 records & should return the matched records (e.g using Contains filter).
It is not very difficult task if i user update panel. But, It takes time becoz i am fetching records on each key. Even i use viewstate it slows down the performance. Is there any alternative way to achieve this? I am wondering if u could use some javascript logic
If you are ok with implementing server side instead of with javascript, you could store the data in the cache object on first load, and then pull from cache on each subsequent hit.
A very simple example