Keep ASP.NET ViewState without being passed via QueryString - asp.net

I have a form with its method being "get" that passes the variables and their values to the query string, respectively. However, it also passes the viewstate variable in the query string. Now, I have a very long viewstate value on the given page, and if passed in the query string, the viewstate variable will cause the page to error out, due to "too long of a query string" which happens to also be too long of a url.
I cannot merely remove the viewstate variable - I need it. But I need to pass the viewstate variable along via some method other than get when the form is submitted, while the other inputs of the form (the non-hidden inputs) are appended to the query string. Is there any way to accomplish this?

Can you change the method to "post" and then use request.form to get your variables?
Otherwise, you might be able to use the session object...
...or a serializable class object
...or a temp table in a database
Several different options

Related

Should you use required named parameters when making a data model in flutter?

Is it good practice to require named parameters for data models in flutter when you know that the data should always have a specific field like an id or username? I think this would remove unnecessary null checking, but I've always seen model classes just use named parameters with fromJson and toJson methods especially if the data is being pulled from a database like Firebase. So is there a reason not to require fields?
Also when would it be a good idea to require a String field and initially populate it with an empty String vs making it nullable and initializing it with a null value?
Below is my opinion, i'm not sure it's true for all but it works for me
Reason 1: it's too long and unnecessary to write the whole thing out, some Jsons can be very complicated to do this, moreover when you get the data in JSON you'll want to simply initialize var A = A.fromJson(jsonData) instead of having to pass it one by one into the Model, moreover this can be repeated many times.
Reason 2: String with null(String?) and String with empty value(String) is very different. With me, I use String? to determine that "it has not received value yet", if the value is non-null then it has received a value including empty. In contrast to a String whose initial value is empty, I cannot distinguish whether it has received empty or has not received data.
Example: when i assign empty value to text2, it cannot distinguish whether it received the value before. This can be used to calculate the display of the Indicators before the variable actually gets a value.

SQL Server connection-specific variables

I'm upgrading my application to use stored procedures rather than dynamic SQL as it is now. What I'd like to do is call some procedure, for example setUser(id), and then for that ID to be carried forward for the duration of the current connection. I have a UserVariables table which stores important data related to the current user (I'm not using the session to store this data as the session only lasts for so long; this way the users data is persisted across logins). I want to select data, such as the ID of the client they're currently viewing, without having to pass the user ID into each stored procedure. Call it laziness, if you like!
I've searched for this quite a bit, and looked at various articles, but they're all about either global variables (which we can't change) or something unrelated.
Basically what I want to do is set the user ID at the beginning of the page load (may even move this into the session_start method at some point) and then access this variable during all subsequent stored procedure calls or queries.
Edit: What I'm after is like when you set a variable at the beginning of an asp page (my application is written in good ol' classic asp) and you then use it throughout the page and any includes. Think of the asp page representing the connection and the includes representing the stored procedures, if you like.
I have found a suitable alternative to setting connection-specific variables. I have a function which takes the sproc name, an array of parameter names, an array of parameter values and a variable to return (i.e. a byref variable). I have modified this function so that, when the parameters are refreshed, it checks for a userid parameter and sets it automatically if it exists. It then returns the 'retval' parameter if it exists, otherwise sets the return variable to myCmd.execute.

Saving a selected value in gridview to viewstate

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"]);
}

How can I change field values to be inserted in the asp.net membership creation

I want to insert user name as unique mail ID and for the mail field in the registration process I want to use the same mail id to be inserted in the asp.net membership user table but by now it was creating user it's own but now I want to change it how can I achieve this ?
I am writing on page load
MembershipCreateStatus creatStatus;
Membership.CreateUser(RegisterUser.UserName, RegisterUser.Password,
RegisterUser.UserName, RegisterUser.Question,
RegisterUser.Password,
true, MembershipCreateStatus.ProviderError);
// true, creatStatus);
Is it correct? But it throws error invalid argument.
What should I do for last two arguments?
First, the last parameter is an out parameter. This means that CreateUser will set the variable you specify, you can't pass in a status enumeration like you are doing there.
Instead, you have to do it like the code you commented out, except you have to specify out creatStatus (including the out keyword).
Second, invalid argument typically means one of the values is null.

ASP.NET Site architecture issue

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

Resources