locked
Is it fine to make a list static to maintain the list item during the postbacks RRS feed

  • Question

  • User1052662409 posted

    Hi All,

    On a button click, I am adding an item to the list. For every click, an item is adding to the list.

     oList.Add(dt.Rows[0]["file_id"].ToString());

    I need to use this list for my later action on the same page.

    My question is that I am making this list as a static global variable.

     public static List<string> oList = new List<string>();

    It is okay? or should I use a different approach? Although it works fine, still I need to clarify from you guys.

    Thanks

    Monday, April 20, 2020 3:13 PM

Answers

  • User-18289217 posted

    As mgebhard said, the static variables are not thread safe and you may be losing the static variables

    However, for State Management I usually use ViewState if the code is valid for the current page only or Session if I need some value to pass them into another web form. 

    Finally, if you really want to persist your data, you should store the data in a database

    HTH

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 20, 2020 3:49 PM

All replies

  • User475983607 posted

    The static key word creates a single memory location that every user has access.  Generally, a static variable not a good approach from a web application.  If the List<T> is read only then then it's fine.  If the List<T> can be different for each user then this is not a good solution.

    Monday, April 20, 2020 3:27 PM
  • User1052662409 posted
    For sure, the list is always different for every user.
    Please suggest, which approach I should use.
    Monday, April 20, 2020 3:48 PM
  • User-18289217 posted

    As mgebhard said, the static variables are not thread safe and you may be losing the static variables

    However, for State Management I usually use ViewState if the code is valid for the current page only or Session if I need some value to pass them into another web form. 

    Finally, if you really want to persist your data, you should store the data in a database

    HTH

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 20, 2020 3:49 PM
  • User1052662409 posted
    First, I want persist the data then I need to update table depending on the id in the list.
    Monday, April 20, 2020 3:53 PM
  • User475983607 posted

    demoninside9

    For sure, the list is always different for every user.
    Please suggest, which approach I should use.

    You have not explained the use case so can't make a recommendation.   User data is commonly stored in a database table, fetched on demand, and filtered by the user.  A cache can be implemented if the data does not change often.

    Monday, April 20, 2020 3:57 PM
  • User-18289217 posted

    In this specific case, I would use ViewState. Just store and then get the values when you need 'em. e.g. 

    You can of course store List<T> within and cast when you want to update the list or to grab the values at the end. e.g. 

    ADDING ITEMS TO THE LIST:

    if(ViewState["mygenericlist"] != null)
    {
                    // ALREADY INITIALIZED
                    List<string> oList = ViewState["mygenericlist"] as List<string>;
                    oList.Add("NEWVALUEHERE");
                    ViewState["mygenericlist"] = oList;
    }
    else
    {
                    // NOT INITIALIZED
                    List<string> oList = new List<string>();
                    oList.Add("NEWVALUEHERE");
                    ViewState.Add("mygenericlist", oList);
    }

    GETTING ITEMS FROM THE LIST<T>

    if (ViewState["mygenericlist"] != null)
    {
        List<string> oList = ViewState["mygenericlist"] as List<string>;
        foreach(var item in oList)
        {
           //-- DO SOMETHING
        }
    }

    HTH

    Monday, April 20, 2020 4:02 PM
  • User1052662409 posted

    You have not explained the use case so can't make a recommendation.

    In an initial form, first I need to insert some document names before inserting the user id.

    I am just inserting document names (in a table) with a null user id. After inserting every document I am returning all auto-incremented id (using SCOPE_IDENTITY()) in a list (oList object in my previous code).

    Now I am updating all documents with the user id. 

    That is why I want first to persist the ids then updating user id in the documents for every document id.

    Monday, April 20, 2020 6:30 PM
  • User475983607 posted

    It is much easier to perform two inserts (or an insert and update) in one SQL script than two.  Then there is no need to maintain the document Ids on the web server.

    Monday, April 20, 2020 7:51 PM
  • User288213138 posted

    Hi demoninside9,

    demoninside9

    In an initial form, first I need to insert some document names before inserting the user id.

    I am just inserting document names (in a table) with a null user id. After inserting every document I am returning all auto-incremented id (using SCOPE_IDENTITY()) in a list (oList object in my previous code).

    Now I am updating all documents with the user id. 

    That is why I want first to persist the ids then updating user id in the documents for every document id.

    demoninside9

    oList.Add(dt.Rows[0]["file_id"].ToString());

    According to your description, I still couldn’t understand your requirement clearly.

    In your description, I found that you said many ids, are they the same?

    Do you want to insert document names first according to an id, and then update according to this id?

    If this is your requirement, I also suggest you could try to use the TSQL.

    If I misunderstand your requirement, please post more details information about your requirement.

    Best regards,

    Sam

    Tuesday, April 21, 2020 2:45 AM
  • User1052662409 posted

    samwu

    Do you want to insert document names first according to an id, and then update according to this id?

    yes, this is what I want.

    At the initial stage, both the user_table and document table are blank.

    First, I need to upload the document than on the final submit I need to update the user id in that document table.

    Tuesday, April 21, 2020 4:23 AM
  • User288213138 posted

    Hi demoninside9,

    yes, this is what I want.

    You can do it in the sql, similar to the following sql statement.

    insert into {tablename} values({file_name}); update {tablename} set file_name = file_name+user_id where file_name = {file_name}

    Best regards,

    Sam

    Tuesday, April 21, 2020 8:11 AM
  • User1052662409 posted

    samwu

    insert into {tablename} values({file_name});

    samwu

    update {tablename} set file_name = file_name+user_id where file_name = {file_name}

    CREATE TABLE [dbo].[tblEmployeeFiles](
    	[file_id] [int] IDENTITY(1,1) NOT NULL,
    	[user_id] [nvarchar](50) NULL,
    	[type] [nvarchar](50) NULL,
    	[file_name] [nvarchar](max) NULL,
    	[upload_date] [datetime] NULL
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

    above is my table structure. I want to update user_id on behalf of file_id

    the user_id will come from the below table's autogenerated column Sno

    CREATE TABLE [dbo].[tbl_Employee](
    	[Sno] [int] IDENTITY(1,1) NOT NULL,
    .......

    I want o update user_id in tblEmployeeFiles with the value of Sno (from tbl_Employee) on behalf file_id  generated in tblEmployeeFiles( there may be multiple file_id for the same user_id)

    There may be multiple files for one user and tare may be many clients using the form at the same time.

    I hope now it is understandable

    Tuesday, April 21, 2020 9:16 AM
  • User475983607 posted

    You are over complicating a very simple task.   INSERT the document and INSERT into the tblEmployeeFiles within a single script as suggested several times above.

    Tuesday, April 21, 2020 10:35 AM
  • User1052662409 posted

    mgebhard

     INSERT the document and INSERT into the tblEmployeeFiles within a single script

    Sorry mgebhard. but I am not clear on this.

    Files have been inserted I need to update user_id (LATER once the files' names have been saved in a table)

    There are multiple files, so how it can be possible with a single script?. at least I need a loop over there for every file_id

    Tuesday, April 21, 2020 11:14 AM
  • User475983607 posted

    Files have been inserted I need to update user_id (LATER once the files' names have been saved in a table)

    There are multiple files, so how it can be possible with a single script?. at least I need a loop over there for every file_id

    Your code seems overly complex for a simple one-to-many or many-to-many insert.  Why do you need to update the file name by concatenating the user_id in a table?  You already have a unique identifier.  Actually you have two in the tables shown which break normalization rules.   Are you trying to make the file name unique too or assigned to the user because you already have this information as well.?

    It will be a lot easier to provide assistance if you explain the use case rather than the solution.   

    Tuesday, April 21, 2020 11:31 AM
  • User1052662409 posted

    mgebhard

    Why do you need to update the file name by concatenating the user_id in a table?

    I am not updating the file name. Where did you see that I am updating the file name?

    Let me explain.

    I have a form where are some user basic info along with the file upload.

    I need to fill the user info and then after one by one, I am uploading files (inserting file name in the database, not user-nameas I don't have it).

    On final submit (now the user name has been generated, I need to update the user name in the table where the file's name has been inserted before.)

    mgebhard

    if you explain the use case rather than the solution. 

    On another hand, through this web form, I am registering (inserting) the employee with their documents. For both purposes, I am having two tables. At the same time, multiple people may use this form to register the employees.

    [ Adding more to this (technically) 

    AL last

    First I am doing

     INSERT INTO [dbo].[tblEmployeeFiles] (type,file_name,upload_date)
    		   VALUES (@type,@file_name,getdate())

    Then I need to update the same table with user_name within same time

    UPDATE tblEmployeeFiles SET user_name=@user_name
       WHERE file_id=@file_id --(not file name only user_name needs to be updated)

    ]

    Tuesday, April 21, 2020 11:47 AM
  • User475983607 posted

    I am not updating the file name. Where did you see that I am updating the file name?

    Your previous post...

    update {tablename} set file_name = file_name+user_id where file_name = {file_name}

    The design seem overly complex.  IMHO, you have not clarified the use case.   Why does the design allow a user to upload documents before the user's identity is known?  Can you explain how the security works?  

    Tuesday, April 21, 2020 12:15 PM
  • User1052662409 posted

    Your previous post...

    update {tablename} set file_name = file_name+user_id where file_name = {file_name}

    This is not my post. It was posted by  samwu.

    Why does the design allow a user to upload documents before the user's identity is known?

    I want to do all work on the final submit button (inserting files, generation user_name and update files table with generated user_name).

    Tuesday, April 21, 2020 1:12 PM
  • User475983607 posted

    I want to do all work on the final submit button (inserting files, generation user_name and update files table with generated user_name).

    I still do not understand why you can't insert the user first to get the userId.  

    Tuesday, April 21, 2020 2:02 PM
  • User1052662409 posted
    Sir, this is the requirements of client, that he wants all submission on one submit button.
    Is it impossible? To do in same the way what I have asked?
    Tuesday, April 21, 2020 2:12 PM
  • User475983607 posted

    demoninside9

    Sir, this is the requirements of client, that he wants all submission on one submit button.
    Is it impossible? To do in same the way what I have asked?

    I do not understand the problem.  If all the data is sent in one submission then why do you need to maintain a list during post backs in the first place?  It seems to me the first thing you need to do is get the userId.  At this point, I'm not sure if the user has logged in or if you are creating a new user account.  If you are creating a new user then insert the user and return the userId.  In the same Action, loop over the file collection and execute an insert script with all the required file parameters and the userId.  You'll have two insert statements In one SQL script.  First, insert the file record, get the new file identity, then insert into the tblEmployeeFiles.  You should have all the data you need tomake this work

    Tuesday, April 21, 2020 3:04 PM